### Always use strict =) use strict; ### Set up a hash table ("associative array") to associate ### numbers with names my %totals; ### Open the file open my $fIN, " ) { ### We only care about this line if it's our special format. This ### will ignore lines that don't have valid data, such as blanks ### trailing blank lines in the file or something. ### matches 01/03/2008 yasmin 67 if( $_ =~ /($date) ($name) ($count)/ ) { ### The parenthases above captured the data in this line (if ### applicable). Now we can access the first, second, and third ### matches: my $this_date = $1; ### The first () field my $this_name = $2; ### The second () field my $this_count = $3; ### The third () field ### Do one thing if the user already has data, and something ### else if not. if( not exists $totals{$this_name} ) { ### If this person isn't already in the hash, add her with ### this data. $totals{$this_name} = $this_count; } else { ### This person is already in the hash, so just add to the ### existing totals count. $totals{$this_name} += $this_count; } } } ### Don't forget to close your file. close $fIN; ### That's it! Now you've got a hash full of your data (garaunteed ### not to have duplicates =) Now you can access someone's ### data directly: print 'yasmin has ' . $totals{'yasmin'} . ' posts.'; ### or with a loop foreach my $username (keys(%totals)) { print "User $username has " . $totals{$username} . " posts.\n"; }