On Tue, Apr 3, 2012 at 11:38 AM, Om Prakash <oomprak...@gmail.com> wrote:
> Hi all, > > I have some data which is like > > A:12 > B:13 > C: 14 > > Now the data is line by line and multiple line with A B C is common though > 12 13 14 is changing, i want to take this data column wise, and the the > value should come under A B C. Any help will be appreciated. > > Regards.../om > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > So basically you have an imput like this: A:12 B:13 C:14 B:2 C:54 A:1 A:34 etc... Right? And what you want it is a way to show: A - 35 B - 15 C - 68 Right? Ok lets just assume that that is what you are looking for as I am not 100% sure based on your email. Anyway what you are doing in that case is pulling them apart at first so you should be able to identify the two sides the letter and the number behind it. use strict; use warnings; my %hash; open $fh, '<', 'input.file' or die $!; while ( <$fh> ) { chomp; my ($letter, $number ) = split ( /:/, $_ ); # So now we have the letter and number split for every line in the file # lets now start counting using the earlier declared hash # I'm not bothering with any error handling that I'll leave up to you $hash{$letter} += $number; } # Now once having looped over all lines we can close the file handle and close ( $fh ); # To make my life easy I will simply spit out the whole hash in one go ans let you see if that is what you are looking for as I have no idea thi is actually correct use Data::Dumper; print Dumper %hash; The result should look kind like this (order of the data might vary) $VAR1 = { 'A' => 35, 'C' => 68, 'B' => 15, } Regards, Rob