Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2016-04-30 18:04 GMT
Find Nodes?
    Voting Booth?
    :nehw tseb si esrever ni gnitirW

    Results (441 votes). Check out past polls.