Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Adding the duplicate data using arrays

by space_agent (Acolyte)
on Aug 11, 2010 at 11:44 UTC ( #854332=note: print w/replies, xml ) Need Help??


in reply to Re^2: Adding the duplicate data using arrays
in thread Adding the duplicate data using arrays

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://854332]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-12-05 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?