Just to sum it up. Here (.*) takes both columns as * is a greedy quantifier
and takes as much as possible and this is until the number (\d+) comes into play.
while (<DATA>) { m/(.*)\s+(\d+)/; $hash{$1} += $2; #$1 is the fruit $2 the number } for my $fruit (keys %hash) { print "$fruit $hash{$fruit}\n"; } __END__ Apple Grape 100 Ginger Fry 200 Apple Grape 80 Ginger Banana 800 Ginger Fry 150 Ginger Banana 45
In reply to Re^3: Adding the duplicate data using arrays
by space_agent
in thread Adding the duplicate data using arrays
by changma_ha
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |