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
In reply to Simple Totals Example (Help Needed) by doctormaze
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |