Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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]
usemodperl slips on a banana skin

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2018-06-24 19:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.