in reply to Parsing a text file

Hmm, your code is a bit chaotic. But here is my try to solve your problem:
#!/usr/bin/perl $file = 'msgcount.txt'; open (LOG, $file); my %records; # creat a hash while (<LOG>) { # each line is parsed by this regular expression: if (/^\S+ (\S+) (\d+)/) { # and the value for the name, which is now in $1 # is increased by the number in $2 $records{$1} += $2; } } # then we go through all keys (the names) # in our %record hash for (keys %records) { # and print out their sum: print "$_: $records{$_}\n"; }
Hope this was useful?

Replies are listed 'Best First'.
Re^2: Parsing a text file
by nimajneb (Initiate) on Apr 16, 2008 at 14:38 UTC
    Thank you all for your replies, extraordinarily helpful, I'm looking into the use of hashes as we speak, as the rest of the application is bound to need them also. What a wonderful community.