PDA

View Full Version : Need a good language for scripts



TransformX
26th August 2004, 09:12
I need to find a good scripting language (Perl/Python/???) which is / does the following:

1. Free
2. Interpreted
3. No too steep learning curve
4. Interpreter is portable (better have it as a file / directory than as a full installation process)
5. Runs on M$ Windows

I need it to automate stuff I usually use excell and text editors to do - parse files. Later I might need it for other jobs but currently I need it to do stuff like this:

Open file X
jump x lines
read line
do string manipulations
put parts of string in different matrix cells
loop back till EOF
run some calculations
write matrix into a CSV file

spadnos
26th August 2004, 09:21
Originally posted by TransformX
I need to find a good scripting language (Perl/Python/???) which is / does the following:

1. Free
2. Interpreted
3. No too steep learning curve
4. Interpreter is portable (better have it as a file / directory than as a full installation process)
5. Runs on windows

I need it to automate stuff I usually use excell and text editors to do - parse files. Later I might need it for other jobs but currently I need it to do stuff like this:

Open file X
jump x lines
read line
do string manipulations
put parts of string in different matrix cells
loop back till EOF
run some calculations
write matrix into a CSV file When putting the pieces of the input strings into "matrix cells", do you have to put parts of a single line of text into multiple "lines" of the matrix?

ie, do you need to put data from a single input line into more than one line of the output CSV file?

If you're extracting data from a single line, and outputting it (reformatted, mangled, etc) to a single line of a different format, then awk will do what you want. If you need to combine data from different input lines for a single output line, then perl, python, or some java thing would probably be better.

As for the learning curve, perl seems pretty easy to learn to do easy things with. Awk is similarly easy, though a little weird to get used to (I run it using CygWin on Windows, I'm not sure if there's a native version). I haven't learned Python yet, so I can't say how easy it is.

- Steve

Jon P. Inghram
26th August 2004, 09:24
Just to toss out an odd-ball suggestion: Rexx?

TransformX
26th August 2004, 09:34
the idea is to create a 2D matrix, break a line into different cells in a matrix's line and add some extrapolations in the remaining cells of the line.
Then write the matrix lines as lines in a CSV file.

spadnos
26th August 2004, 12:28
Originally posted by TransformX
the idea is to create a 2D matrix, break a line into different cells in a matrix's line and add some extrapolations in the remaining cells of the line.
Then write the matrix lines as lines in a CSV file. OK. If I understand you correctly,you'll have lines of data in a file, with each line needing to have some other stuff calculated, and a longer line output to another file.

Awk should do this quite nicely (unless you need advanced math functions).

There is an awk for Windows here (http://cm.bell-labs.com/cm/cs/who/bwk/).

At its most basic, it runs a command (or group of commands) on each line of a file. There are also "BEGIN" and "END" blocks, to initialize or finalize things.

The commands can be stuff like:
print($1, $2, $3+$4, $5/($1+$2))
or other things. ($0 is the whole input line, $1 is field 1...)
Here is a short script to convert from one version of AVR assembly language to another. It shows:
how to choose an action depending on the format of the line (the ($x==xx) at the beginning of a line)
how to create / call a function (join)
comments (lines beginning with #)
overriding default parsing behavior (IGNORECASE, FS, OFS ...)

BEGIN {
IGNORECASE=1;
FS="[ \t\n]+"
OFS="\t"
# ORS=""
};
($1==".def" && $3=="=") { $3=$2; $2="#define" ; fred=join(2, NF, "\t", 1); $0=fred }
($1==".equ" && $3=="=") { $3="EQU"; fred=join(2, NF, "\t", 1); $0=fred }
($1==".set" && $3=="=") { $3="VAR"; fred=join(2, NF, "\t", 0); $0=fred }
($2==".byte") { $2 = "DS"; fred=join(1, NF, "\t", 0); $0=fred }
($2==".dw") { $2 = "DW"; fred=join(1, NF, "\t", 0); $0=fred }
($1==".dw") { $1 = "DW"; fred=join(1, NF, "\t", 0); $0=fred }
($2==".db") { $2 = "DB"; fred=join(1, NF, "\t", 0); $0=fred }
($1==".db") { $1 = "DB"; fred=join(1, NF, "\t", 0); $0=fred }
{ print $0 }

# make this take into account whatever the original separators were (spaces / tabs ...)

function join(start, end, sep, comments, result, i)
{
EndStr=""
if (sep == "")
sep = " "
else if (sep == SUBSEP) # magic value
sep = ""
result = $(start);
NextSep=sep;
for (i = start + 1; i <= end; i++) {
if (index($(i), ";") > 0) {
NextSep = " "
if (comments==1) {
sub(";", "/*", $(i))
EndStr=" */"
}
}
result = result sep $(i);
sep = NextSep;
}
result = result EndStr;
return result;
}

Google for awk, and you'll find loads of information about how to use it.

- Steve

TransformX
27th August 2004, 09:45
Thanks !