Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Adding the duplicate data using arrays

by space_agent (Acolyte)
on Aug 11, 2010 at 11:44 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2024-04-25 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found