Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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!!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047176]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2018-01-16 14:10 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (180 votes). Check out past polls.