Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, what is the best approach in solving this kind of problem. I am parsing a file that contains the following information:
Jack Monday 4 Eddie Tuesday 6 Jack Friday 7
I want to add the total for each user which appera at the end. somthing like this :
Jack 11 Eddie 6
I thank you in advance.

Replies are listed 'Best First'.
Re: complex regex
by davido (Cardinal) on Apr 21, 2004 at 05:36 UTC
    Here's one approach...

    Instead of using a regex within the m// operator, this uses a regex within a split function. Whatever approach you take, the regular expression involved needn't be terribly complex.

    my %counter; while ( <DATA> ) { my ( $name, $number ) = ( split /\s+/ )[0,2]; $counter{$name} += $number; } print "$_: $counter{$_}\n" foreach keys %counter; __DATA__ Jack Monday 4 Eddie Tuesday 6 Jack Friday 7

    Dave