hallikpapa has asked for the wisdom of the Perl Monks concerning the following question:

Evening Monks! I have a bunch of data loaded into a hash and would like to read thru it and would like to sum $cols->{1} in groups. It will loop through a subtroutine that groups all "Lines" together under a "Device". So if there are 3 lines, the $numberOfLines should == 3 This is the routine I am working on, and finished all other parts, but just wondering the best way to loop through the hash and have individual counts.
foreach my $row ( keys %$rows ) { my $cols = $rows->{$row}; if ( $cols->{0} ) { $line_pointer{$cols->{1}} = $cols->{1} +; $line_pointer{$cols->{1}}{"numberOfLin +es"} = $counter++; } }
$line_pointer{$cols->{1}}{"numberOfLines"} Should contain
$VAR1 = { '1' => '2' };
But instead contains:
$VAR1 = { '1' => '1' };
Updated with my code, but apparently my looping counter sucks.

Replies are listed 'Best First'.
Re: Grouping values in a hash together (Updated
by siva kumar (Pilgrim) on Nov 26, 2007 at 07:45 UTC
    As far I understood your post, here is my suggestion but not sure whether I got your question right or not.
    use Data::Dumper; $rows = { 0 => {0 => "zero", 1=>"One", 2=>"kjh"} , 1 => {0 => "zero", 1=>"One", 2=>"sdfgsd"}, 2 => {0 => "zero", 1 =>"Two", 2=>"ety"}, 4 => {0 => "zero", 1 =>"Two", 2=>"dfgdfg"}, 5 => {0 => "zero", 1 =>"Two", 2=>"Test"}, }; print Dumper $rows; %line_pointer=(); foreach my $row ( keys %$rows ) { my $cols = $rows->{$row}; if ( $cols->{0} ) { $line_pointer{$cols->{1}}{"numberOfLines"} = $line_pointer{$cols->{ +1}}++; } } print Dumper \%line_pointer;
    Output :
    $VAR1 = { 'Two' => 3, 'One' => 2 };