Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Re-post of Awk (Aho, ...) page 68 question

Status
Not open for further replies.

AwkRookie

Programmer
Oct 15, 2002
10
US
I am using all-numeric data, tab-delimited, as input to the following code:

{for (i=1; i<=NF; i++)
sum+=$i
if(NF>maxfld)
maxfld=NF
}
END {for (i=1; i<=maxfld; i++) {
printf(&quot;%g&quot;,sum)
if (i<maxfld)
printf(&quot;\t&quot;)
else
printf(&quot;\n&quot;)
}
}

According to the Aho, Kernighan, Weinberger text p. 68, &quot;the program prints nothing if the input file is empty&quot;. I get no run-time or syntax errors and indeed am getting no output.

What could be wrong with my input (double-precision 8-column) or what is obviously missing from the above code in order to get it to run? And please, be patiet, I am an Awk newbie!
 
It seems likely to me that sum needs to be replaced by sum[ i ], but you should still be getting output. How are you running it? Put a {print} before your first statement to see what input it is getting. CaKiwi
 
sum += $i gives you an array of ascending values that correspond to the NF..
so to print out the value totals at NF 15 you would
access sum[15], etc...
I'm with cakiwi..the end action doesn't make a lot of sense without sum being an array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top