Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to read a file and add value for same key

by Anonymous Monk
on Jul 31, 2013 at 06:29 UTC ( #1047176=note: print w/ replies, xml ) Need Help??


in reply to How to read a file and add value for same key

Hi tony ucdavis,
In case, you think those who commented before now really do not what to help, please think again. What you labour to do will not only be yours but would give you a kind of chip on your shoulder and a well of satisfaction.
However, if you are still in doubt the following solve your problem:

use warnings; use strict; my %h; chomp( my @arr = split /\s+?/, <DATA> ); my $key; for (@arr) { if ( !/[[:digit:]]/ ) { $key = $_; } else { if ( $h{$key} ) { $h{$key} += $_; } else { $h{$key} = $_ } } } print $_, q[ ], $h{$_}, q[ ], for sort keys %h; __DATA__ a 1 b 3 c 5 a 2
But seriously, you need to know, how all that came together. So, please follow the advise given before this post. You would be glad you did.
And welcome to the Monastery!!!


Comment on Re: How to read a file and add value for same key
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2014-07-30 08:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls