doctormaze has asked for the wisdom of the Perl Monks concerning the following question:
In order to learn Perl, I've started fiddling around with text files. I have a file called-
siteperf.txt, Which contains this data-
A,5000,20 B,4500,15 C,4000,10
All, I want to do is total the values in the second column.
So far I've come up with this-
$sitePerf = 'siteperf.txt'; $tot = 0; open(F, "$sitePerf") || die ("Could not open $sitePerf!"); while (<F>) { ($siteID, $siteAge, $siteDistance) = (split/,/)[0,1]; $tot = $hash{$siteID} += $siteAge; print $tot; } close (F);
, Instead of added the data in the second column ($siteAge) together, the script justs concatenates the values and prints-
500045004000Your expertise is appreciated,. I've got lots of Perl books out and have been diligently trawling the internet and this is very frustrating!
Thanks
Edited by planetscape - removed code tags from around entire post, replaced with <p></p> tags as appropriate
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simple Totals Example (Help Needed)
by rhesa (Vicar) on Apr 02, 2006 at 22:54 UTC | |
by doctormaze (Initiate) on Apr 02, 2006 at 23:06 UTC | |
|
Re: Simple Totals Example (Help Needed)
by McDarren (Abbot) on Apr 02, 2006 at 23:46 UTC | |
|
Re: Simple Totals Example (Help Needed)
by davido (Cardinal) on Apr 03, 2006 at 06:13 UTC |