For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2007 > two simple scripts - help needed









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author two simple scripts - help needed
Thomas

2007-04-20, 6:56 pm

Hello I 'm writing two scripts, which code i paste below:
table:
#!/usr/bin/awk -f
BEGIN {
print "<table width= \"300\" border="1">"
print "<br>"
}

(NR==1 )
{
print "<caption>" $0 "</caption>"
print "<br>"

}


(NR==2){
j=1;
print "<thead><br>";
print "<tr>";
while(j<=NF){
print "<th>" $j "</th>";
j=j+1;
}
print "</tr>";
print "<br></thead>"
print "<tbody><br>"
}

(NR>2){
j=1;
print "<tr>";
while (j<=NR){
print "<td>" $j "</td>";
j=j+1;
}
print "</tr>";

}



END{

print "</tbody>";
print "</table>";
}
This one should read a file and generate a table on a basis of file
structer. Instead i've got a mess. I discovered that it tries to read every
single field in a record and than apply an action to it. Any ideas how to
improve that ?

calc:
#!/usr/bin/awk -f

{
i=0;
j=0;
while (i<=NF){
if($i=="+" || $i=="-" || $i=="*" || $i=="/"){

if(j<1){
print "error parsing file";
exit;
}

else {

if($i=="+") {
k=tablica[j]+tablica[j-1];
}

else if ($i=="-"){
k=tablica[j]-tablica[j-1];
}

else if ($i=="*"){
k=tablica[j]*tablica[j-1];
}

else if ($i=="/"){
k=tablica[j]/tablica[j-1];
}
j=j-1;
tablica[j]=k;
}
}

else {
tablica[j]=$i;
j=j+1;
}

i=i+1;
}
tablica[0]=3;
print tablica[0] ;
}
Second one is reverse polish notation calculator ( it should valuate
expression such as 3 4 + 5 * to 35), but it doesn't work neither. Tablica is
a table which I use as a stock, and j is a pointer. Sorry for my english,
cheers :D




Ed Morton

2007-04-20, 6:56 pm

Thomas wrote:
> Hello I 'm writing two scripts, which code i paste below:


Show us a small sample of the input you have and the output you want.

Ed.
Doug McClure

2007-04-20, 6:56 pm

This snippet doesn't do what you think it does:

(NR==1 )
{
print "<caption>" $0 "</caption>"
print "<br>"

}



It is VERY different from:

(NR==1 ) {
print "<caption>" $0 "</caption>"
print "<br>"

}

DKM

On Fri, 20 Apr 2007 18:46:13 +0200, "Thomas" <arabel9@o2.pl> wrote:

>Hello I 'm writing two scripts, which code i paste below:
>table:
>#!/usr/bin/awk -f
>BEGIN {
> print "<table width= \"300\" border="1">"
> print "<br>"
>}
>
>(NR==1 )
>{
> print "<caption>" $0 "</caption>"
> print "<br>"
>
>}
>
>
>(NR==2){
> j=1;
> print "<thead><br>";
> print "<tr>";
> while(j<=NF){
> print "<th>" $j "</th>";
> j=j+1;
> }
> print "</tr>";
> print "<br></thead>"
> print "<tbody><br>"
>}
>
>(NR>2){
> j=1;
> print "<tr>";
> while (j<=NR){
> print "<td>" $j "</td>";
> j=j+1;
> }
> print "</tr>";
>
>}
>
>
>
>END{
>
>print "</tbody>";
>print "</table>";
>}
>This one should read a file and generate a table on a basis of file
>structer. Instead i've got a mess. I discovered that it tries to read every
>single field in a record and than apply an action to it. Any ideas how to
>improve that ?
>
>calc:
>#!/usr/bin/awk -f
>
>{
>i=0;
>j=0;
>while (i<=NF){
>if($i=="+" || $i=="-" || $i=="*" || $i=="/"){
>
> if(j<1){
> print "error parsing file";
> exit;
> }
>
> else {
>
> if($i=="+") {
> k=tablica[j]+tablica[j-1];
> }
>
> else if ($i=="-"){
> k=tablica[j]-tablica[j-1];
> }
>
> else if ($i=="*"){
> k=tablica[j]*tablica[j-1];
> }
>
> else if ($i=="/"){
> k=tablica[j]/tablica[j-1];
> }
> j=j-1;
> tablica[j]=k;
> }
>}
>
>else {
>tablica[j]=$i;
>j=j+1;
>}
>
>i=i+1;
>}
>tablica[0]=3;
>print tablica[0] ;
>}
>Second one is reverse polish notation calculator ( it should valuate
>expression such as 3 4 + 5 * to 35), but it doesn't work neither. Tablica is
>a table which I use as a stock, and j is a pointer. Sorry for my english,
>cheers :D
>
>
>



To contact me directly, send EMAIL to (single letters all)
DEE_KAY_EMM AT EarthLink.net. [For example X_X_X@EarthLink.net.]
Patrick TJ McPhee

2007-04-22, 6:57 pm

In article <f0aqos$e42$5@inews.gazeta.pl>, Thomas <arabel9@o2.pl> wrote:
% Hello I 'm writing two scripts, which code i paste below:

[...]

% (NR==1 )
% {

You want this brace to be at the end of the previous line.

% while(j<=NF){
% print "<th>" $j "</th>";
% j=j+1;
% }


You might want to use
printf "<th>%s</th>", $j

The only difference is the newlines between the columns, which might
or might not be bothering you.

% print "<tbody><br>"

Incidentally, none of the <br>s in your example are valid in html (i.e.,
you can't put a <br> immediately after a <table>, </caption>, <thead>,
</tr>, or <tbody> tag).


% while (j<=NR){

This should be NF as in the previous case.

% #!/usr/bin/awk -f
%
% {
% i=0;
You want i to start from 1. You might want to use a for loop
for this sort of iteration (for i = 1; i <= NF; i++) { ...})
since it puts all the loop controls in one place.

% j=0;
% while (i<=NF){
% if($i=="+" || $i=="-" || $i=="*" || $i=="/"){
%
% if(j<1){
% print "error parsing file";
% exit;
% }

This should be j < 2. From a usability perspective, I'd think it's
better to treat this as an error rather than exiting at the first
sign of trouble. Another thing, each input record is clearing the
stack, which seems a bit annoying to me.


%
% else {
%
% if($i=="+") {
% k=tablica[j]+tablica[j-1];
% }

This should be t...[j-1] and [j-2]. j is the number of elements on the
stack. The most recent element is at index j-1 in your else clause below.

% j=j-1;
% tablica[j]=k;

And this is going into the wrong stack element. You want to overwrite
the j-2 element, then set j to j-1.

% else {
% tablica[j]=$i;
% j=j+1;
% }

This is OK.

% tablica[0]=3;
% print tablica[0] ;

I'm not sure what the point is here, but it doesn't seem very helpful.
You want commands to print the last element in the stack, and for debugging
you might want a command to print the entire stack.

% Second one is reverse polish notation calculator ( it should valuate
% expression such as 3 4 + 5 * to 35), but it doesn't work neither. Tablica is

Suppose we put this all on one line
3 4 + 5 *

and add some code to the top of the "i" loop to show the contents of the
stack:
print "i", i
for (k = 0; k < j ; k++) print tablica[k]

We get this:

i 0
i 1
3 4 + 5 *
i 2
3 4 + 5 *
3
i 3
3 4 + 5 *
3
4
i 4
3 4 + 5 *
3
i 5
3 4 + 5 *
3
5

We start i at 1, and we get this

i 1
i 2
3
i 3
3
4
i 4
3
i 5
3
5

Fix the binary operators so they handle the stack the same way as the normal
push operation:

i 1
i 2
3
i 3
3
4
i 4
7
i 5
7
5

If you print the stack at the end, it has one element, 35.
--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com