PDA

View Full Version : I am a programmer without a compiler, and a sh-tty one at that.


DavidC
11-08-2005, 07:27 PM
I like programming. I've used VB, java, perl, QBASIC (woot!), and that's about it.

However, my linux box is down, and I like the flexibility of programming using javascript, considering that windows seems to run it just fine with minimal effort on my part.

----------

So I'd like to know a few things about javascript programming.

1) Can they output to text files. *

2) Can they output to HTML files. *

3) How do I make arrays?

4) How do I gather user input and make a WHILE loop?

* easily, hopefully.

----------------------------------------------

link (http://www.pageresource.com/jscript/jarray.htm)

The following shows some code with the alert and prompt statements, which is part of what I need. It also shows a function (though no function variables are used), and a for loop. It shows arrays being used.

So basically, I need to know the while loop and how to export to text.

This shows a while loop (http://www.w3schools.com/js/tryit.asp?filename=tryjs_while)

---

Alright... so basically everything's been answered except the "how to output to text and html" thing... If I could get that, I'd be pretty happy. /images/graemlins/smile.gif

---

Edit:

function with an argument (http://www.w3schools.com/js/tryit.asp?filename=tryjs_while)

RatFink
11-08-2005, 08:46 PM
The javascript can produce text output as in:

document.write('hello world');

to make text into HTML just use HTML commands:

document.write('<h1>Hello World<h1>');

Not sure how you run your javascripts, but if you run them in your browser the output can be saved with a view-source save command. Or from a command line you can use the > operator after a command and give it a file to write the output to like: runme > output.txt

StevieG
11-08-2005, 09:27 PM
Out of curiosity, what did you find difficult about using Perl on Windows?

It would seem that for what you're looking for (quick text processing) Perl would be the thing.

DavidC
11-09-2005, 05:22 AM
[ QUOTE ]
The javascript can produce text output as in:

document.write('hello world');

to make text into HTML just use HTML commands:

document.write('<h1>Hello World<h1>');

Not sure how you run your javascripts, but if you run them in your browser the output can be saved with a view-source save command. Or from a command line you can use the > operator after a command and give it a file to write the output to like: runme > output.txt

[/ QUOTE ]

document.write(string) seems cool... ooc, how do I specify what text file it's printing to?

Would I have to use weird java-esque file names: c://foldertowriteto/file.txt or would a standard DOS filepath be okay?

I'd prefer to execute the file by double-clicking on it on my desktop or somesuch, so . /images/graemlins/smile.gif

FWIW, the stuff that I've found on the java document object model is very complex stuff. It's way more powerful than what I need to use it for (though it does appear to do what I need to use it for)...

Any idea where I could find a dummy's guide to this?

--Dave.

DavidC
11-09-2005, 05:24 AM
[ QUOTE ]
Out of curiosity, what did you find difficult about using Perl on Windows?

It would seem that for what you're looking for (quick text processing) Perl would be the thing.

[/ QUOTE ]

This would be for iterative bet-size calculations. Java should do okay for this purpose. But yes, perl totally pwns for... well... everything actually. /images/graemlins/cool.gif

I tried setting up perl on windows once and it was a huge hassle. On linux it was very easy (might have even been part of the core that I used).

OrcaDK
11-09-2005, 05:41 AM
Ok first of all we need to set things straight. Javascript is not an actual programming language, that's just scripting. Furthermore Javascript doesn't get compiled, and you'll not get Javascript to write files on your PC; it's simply just not made for taht purpose.

Why not use Java if you've used that before? Using Java you can make console apps if you want, and then you can get user input by making an infinite while loop.

Just download Java 1.5 from Sun's site, then just find your javac location, and you're set with a compiler and everything.

11-09-2005, 07:54 AM
You may want to check out ActivePerl (http://www.activestate.com/Products/ActivePerl/), a pretty good (from what I've heard, I don't do Perl) package with a Windows port. You might also want to consider Cygwin, which can be nice if you're familiar with Unix/Linux.

11-09-2005, 09:57 AM
[ QUOTE ]
[ QUOTE ]
The javascript can produce text output as in:

document.write('hello world');

to make text into HTML just use HTML commands:

document.write('<h1>Hello World<h1>');

Not sure how you run your javascripts, but if you run them in your browser the output can be saved with a view-source save command. Or from a command line you can use the > operator after a command and give it a file to write the output to like: runme > output.txt

[/ QUOTE ]

document.write(string) seems cool... ooc, how do I specify what text file it's printing to?

Would I have to use weird java-esque file names: <a href="c://foldertowriteto/file.txt" target="_blank">c://foldertowriteto/file.txt</a> or would a standard DOS filepath be okay?

I'd prefer to execute the file by double-clicking on it on my desktop or somesuch, so . /images/graemlins/smile.gif

FWIW, the stuff that I've found on the java document object model is very complex stuff. It's way more powerful than what I need to use it for (though it does appear to do what I need to use it for)...

Any idea where I could find a dummy's guide to this?

--Dave.

[/ QUOTE ]

The security around web browsers will prevent Javascript from writing directly to the hard drive. This is intended to prevent malicious code from affecting computers.

The best way to deal with a text file save is to write it to an html text field and then copy and paste the text into a new text file.

As for good JS resources, try javascript.internet.com. There are hundreds of scripts here that might point you in the right direction.

RatFink
11-09-2005, 11:48 AM
It can't write to a file. It can output to your browser which you can then save to a file. Or if you are somehow running it from the command line then you can "pipe" the output to a file using your operating system functions to redirect output to a file. But the language itself does not support any Input/Output commands.

I'm with the others, you will run into your boundaries soon with JavaScript. Perl or PHP isn't that huge of a leap to screw around with, but your upside capabilities to progress into more involved applications are significant.

SheridanCat
11-09-2005, 12:19 PM
[ QUOTE ]

I tried setting up perl on windows once and it was a huge hassle. On linux it was very easy (might have even been part of the core that I used).

[/ QUOTE ]

I will second Perl from ActiveState. It's prebuilt and comes with a nice installer which will also make the .pl file association for you.

While I could compile my own Perl on Windows, it's so much easier to use the ActiveState build. There is also a pretty comprehensive module repository, so you don't have to use CPAN directly (though you still can, if you wish). For information on that, look at Perl Package Manager aka "ppm" when you get the ActiveState installation done. If you don't have a compiler, that's the best way to install packages that aren't pure perl.

Regards,

T

DavidC
11-09-2005, 05:53 PM
[ QUOTE ]
[ QUOTE ]
[ QUOTE ]
The javascript can produce text output as in:

document.write('hello world');

to make text into HTML just use HTML commands:

document.write('<h1>Hello World<h1>');

Not sure how you run your javascripts, but if you run them in your browser the output can be saved with a view-source save command. Or from a command line you can use the > operator after a command and give it a file to write the output to like: runme > output.txt

[/ QUOTE ]

document.write(string) seems cool... ooc, how do I specify what text file it's printing to?

Would I have to use weird java-esque file names: <a href="c://foldertowriteto/file.txt" target="_blank">c://foldertowriteto/file.txt</a> or would a standard DOS filepath be okay?

I'd prefer to execute the file by double-clicking on it on my desktop or somesuch, so . /images/graemlins/smile.gif

FWIW, the stuff that I've found on the java document object model is very complex stuff. It's way more powerful than what I need to use it for (though it does appear to do what I need to use it for)...

Any idea where I could find a dummy's guide to this?

--Dave.

[/ QUOTE ]

The security around web browsers will prevent Javascript from writing directly to the hard drive. This is intended to prevent malicious code from affecting computers.

The best way to deal with a text file save is to write it to an html text field and then copy and paste the text into a new text file.

As for good JS resources, try javascript.internet.com. There are hundreds of scripts here that might point you in the right direction.

[/ QUOTE ]

------------

I'd heard about this, and to be honest, while I'm unhappy that I can't do what I want, this is probably still A Good Thing. However, does this still take effect even if I'm running off of a file in my c:\?

----

From the way this is going, I'm probably going to be stubbornly writing javascript until I have to make serious output: what I'm working on right now is just a step-through process that will choose bet sizes for me.

I'm sure I'll hit the wall eventually and need perl. When I do, it's ActiveState for me. /images/graemlins/smile.gif

Thanks guys.

Piping output though sounds great, as does outputting to html. It should be fun to try that out.

I'll be at javascript.internet.com for a while.

--Dave.

_dave_
11-09-2005, 07:25 PM
Hi DavidC,

you can indeed write (and read) text files from JavaScript, so long as you are not using a web browser to execute the scripts. Use the *.js method, as used for the PT scripts, and there is no security imposed on the code.

It looks like you've got arrays & functions, so here's a quick example:

This gets the names of all players in the chosen PTDB that are autorated as a fish, stores their screen names in an array, then writes the contents of the array to a text file, in the same folder as the script is run from.

Make sure there is not already an important file names "fish_list.txt" in your Poker Tracker directory, it will be overwritten.

Writing an Array to a Text File Example: fish_list.js
<font class="small">Code:</font><hr /><pre>
///// change settings here /////

var ptdb = 'ptrack11';
var fish_icon = 2;
var output_filename = "list_of_fish.txt";

////////////////////////////////

///// Globals /////////
var db = new ActiveXObject("ADODB.Recordset");
var cstring = "Driver={Microsoft Access Driver (*.mdb)};DBQ="+ptdb+".mdb";
var totalhands = 0;
var sql = '';

var output_array = new Array();

///////////////////////////

//// functions ///////

function outFile(arr, output_file) {
var c = arr.length;
var scriptfile = WScript.ScriptFullName;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFile(scriptfile);
var scriptpath = f.ParentFolder;
var outpath = scriptpath + "\\" + output_filename;
var a = fso.CreateTextFile(outpath, true);

for(var k=0; k&lt;c; k++) {
a.WriteLine(arr[k]);
}
a.Close();
}


//////////////////////


/// Retrieve the players from PokerTracker DB
/// Whom have been atorated with a "Fish" icon

sql = "SELECT screen_name FROM players"
+" WHERE treeview_icon="+fish_icon+";";


db.Open(sql, cstring, 1, 3);


// cycle through the recordset using MoveNext,
// add each name to the next slot of the array
for (var i=0; i &lt; db.RecordCount; i++)
{
output_array[output_array.length] = db(0) +"";
db.MoveNext();
}

db.Close();

outFile(output_array, output_filename);
WScript.Echo(output_filename +" Written");
</pre><hr />

Enjoy!

dave.

11-10-2005, 11:20 AM
[ QUOTE ]
I'd heard about this, and to be honest, while I'm unhappy that I can't do what I want, this is probably still A Good Thing. However, does this still take effect even if I'm running off of a file in my c:\?

[/ QUOTE ]

Yeah - I am afraid so. The browser doesnt care where the file resides (and all web pages exist on your hard drive in a cache anyway...)

Have you considered learning Java? Its not a ton more difficult than Javascript at the more basic levels and its free. Check out Sun's website. They have soem great beginners tutorials.

CORed
11-10-2005, 04:47 PM
You can use javascript(actually, microsoftt calls it Jscript, as they do in IE, because Javascript is a Netscape trademark, but it's essentially the same language) or VBscript (a stripped down Visual Basic) in the Windows scripting engine (which is installed by defaUlt on most versions of Windows). I have never done anything but "hello world" type examples with this, but you can definitely do file i/o with it. You can't do file I/O with Javascript in a browser, except by using Document.write() and then saving from the browser. Do a google on it and you can probably find some web sites to help you get started, of buy a book on it.

Also, ActiveState's perl is good and installs pretty easily in Windows and is free. Another post has a link to it. If you're comfortable with perl, this may be your best option. Unless you're using low level system calls, perl programs that work in Linux will work in Windows.

For C and C++ you can use cygwin or mingw. Cygwin by default links against a UNIX emulation library and can be very useful for porting programs written for UNIX/Linux to Windows. Mingw compiles native windows executables. You can also get the mingw headers an import libraries with cygwin, and use a command line option to build native windows executables. Of course, there are commercial C and C++ compilers available.

If you're using javascript in a browser, you can get user input by creating an HTML form. You will need to process the input in Javascript (with the onSubmit event, or attaching a function to &lt;button&gt; element), since you won't be submiting it to a server.

jba
11-10-2005, 05:12 PM
if you like perl on linux/unix, cygwin + perl &gt; activestate.

StevieG
11-10-2005, 05:50 PM
[ QUOTE ]
if you like perl on linux/unix, cygwin + perl &gt; activestate.

[/ QUOTE ]

That may be, but installing Active State is easier than Cygwin. Plus the extras of a Start menu program group with easy link to indexed HTML perl docs, and the Perl Package Manager make Active State a better choice for casual programmers.

DavidC
11-11-2005, 11:47 PM
[ QUOTE ]
This gets the names of all players in the chosen PTDB that are autorated as a fish, stores their screen names in an array, then writes the contents of the array to a text file, in the same folder as the script is run from.


[/ QUOTE ]

Sweetest idea ever! /images/graemlins/smile.gif If someone wrote a VBS that would take those from the .txt and put them into your buddy list that'd be awesome. /images/graemlins/smile.gif (Edit: and there's already a few buddy list scripts out there for vbs.)

DavidC
11-11-2005, 11:59 PM
var output_array = new Array();

...

I'm a bit of a newb when it comes to this, but we're making a dynamic array here, yes?

Just curious.

(fwiw, for what I'm attempting, dynamic arrays would be the best to work with anyways, so...)

Thanks a lot for this script, man! This is awesome!!

hehe.. I was just going to ask you how to find UBound Arr and LBound arr... but I see it's in the script. /images/graemlins/smile.gif

I don't understand this line, however:

[ QUOTE ]
var scriptfile = WScript.ScriptFullName;
...
var a = fso.CreateTextFile(outpath, true);


[/ QUOTE ]

I'm sure a little reading about these modules (sorry, perl term, I mean "objects", right? /images/graemlins/smile.gif ) will solve that, though.

Adding the +"" is pretty smooth, too, to make sure java knows it's a string. /images/graemlins/smile.gif

_dave_
11-12-2005, 01:11 AM
[ QUOTE ]

var output_array = new Array();

...

I'm a bit of a newb when it comes to this, but we're making a dynamic array here, yes?


[/ QUOTE ]

Yes, dynamic array. AFAIK, all arrays in JavaScript are dynamic.

The method arrayname.length returns an integer of the next "slot" of the array, therefore the line:
[ QUOTE ]
output_array[output_array.length] = db(0) +"";

[/ QUOTE ] adds the variable to the final "slot".

The [ QUOTE ]
+""

[/ QUOTE ] forces the variable returned from the database to be typed as a string, as you assumed, I think.

The ADODB recodset object is a strange thing, and the forced typing to a string causes the interpreter to copy the value of the db(0) variable into the array - if you don't do this, JavaScript interpreter writes reference in to the array, and the value therefore ends up the same for all elements of the array as you cycle through the recordset.


[ QUOTE ]

I don't understand this line, however:

[ QUOTE ]

var scriptfile = WScript.ScriptFullName;
...
var a = fso.CreateTextFile(outpath, true);


[/ QUOTE ]


[/ QUOTE ]


These two lines are making use of different objects. Firstly:
[ QUOTE ]

var scriptfile = WScript.ScriptFullName;


[/ QUOTE ]

This uses the WScript object, which is available to any script (JavaScript or VB) running under the "Windows Script Host" - WScript. This method returns the full path of the running script, e.g. "C:\Poker Tracker V2\somescript.js", as a string. run this from various locations to see what it does:

<font class="small">Code:</font><hr /><pre>
var out = "";
out = out + WScript.ScriptFullName;
WScript.Echo(out);
</pre><hr />

As you have seen, the WScript object also contains the "Echo" method, used often for output.

Upon further study, it seems this is not at all useful for the script as posted - must be a relic of my drunken copy &amp; paste skills /images/graemlins/smile.gif

The path for file output is built up using the other line you reference, the "fso" - Scripting.FileSystemObject.

[ QUOTE ]

var a = fso.CreateTextFile(outpath, true);


[/ QUOTE ]

This is an object that gives you access to the filesystem from where the sript is running: you can read/write files, and retrieve the current path - this is what is used here, so as to make sure the output is written to the same location as the script is run from.

[ QUOTE ]

var fso = new ActiveXObject("Scripting.FileSystemObject");


[/ QUOTE ]
Initialises the Scripting.FileSystemObject , names as "fso".
[ QUOTE ]

var f = fso.GetFile(scriptfile);


[/ QUOTE ]
Retrieves the currently executing script object, stored in variable "f".
[ QUOTE ]

var scriptpath = f.ParentFolder;


[/ QUOTE ]
Returns the path from which the scrpit is running.
[ QUOTE ]


var outpath = scriptpath + "\\" + output_filename;


[/ QUOTE ]
Here we build up a file path, source + (global) output_filename, double slashes because of escape chars.
[ QUOTE ]


var a = fso.CreateTextFile(outpath, true);


[/ QUOTE ]
The CreateTextFile method of the Scripting.FileSystemObject takes the output path as it's first argument, the second argument is whether to overwrite or not (I think, not sure on the second argument).


A good site that started me on learning all this:
WinScripter (http://www.winscripter.com/WSH/default.aspx)

I hope this helps,

dave.