in reply to Re^2: Converting a growing hash into an array of arrays
in thread Converting a growing hash into an array of arrays

If I'm understanding this correctly, you need to increment the value in the second sub array based on the value found in the corresponding position on the first sub array. If so, this is probably a little over-thinking it, but it should work:
my @data = ( ["1.6","2.2","3.4","3.6","5.4","6.2","7.1", "8.1", "9.0"], [ 1, 2, 5, 6, 3, 15, 4, 3, 4], [ sort { $a <=> $b } (1, 2, 5, 6, 3, 15, 4, 3, 4) ] ); print join(", ", @{$data[1]}), "\n"; map { $data[1]->[$_]++ if $data[0]->[$_] =~ /3.4/ } 0..$#{$data[0]}; print join(", ", @{$data[1]}), "\n"; --(0)> perl test.pl 1, 2, 5, 6, 3, 15, 4, 3, 4 1, 2, 6, 6, 3, 15, 4, 3, 4

Replies are listed 'Best First'.
Re^4: Converting a growing hash into an array of arrays
by holli (Abbot) on Jul 14, 2006 at 19:42 UTC
    or less magical
    my @data = ... xadd( \@data, "3.4", 1); sub xadd { my $data = shift; my $elem = shift; my $incr = shift || 1; for ( 0..$#{$data} ) { $data->[1]->[$_] += $incr, $i = 1, last if $data->[0]->[$_] eq $elem; } unless ( $i ) { push @{$data->[0]}, $elem; push @{$data->[1]}, $incr; $data->[2] = [ sort { $a <=> $b } @{$data->[1]}; } # comment this line out if you don't # need to re-sort the 3rd array $data->[2] = [ sort { $a <=> $b } @{$data->[1]}; }
    Update: After posting I read that the OP needs that the solution works with an empty array. I added the edge case.


    holli, /regexed monk/
Re^4: Converting a growing hash into an array of arrays
by madbombX (Hermit) on Jul 14, 2006 at 19:24 UTC
    That doesn't seem to work. When the program first begins to run, @data is empty and therefore generates errors with the join (since there is no data to be joined yet). Regardless, with the map statement above, nothing seems to get added to either of the arrays $data[0] or $data[1]. Note: I have been replacing (in this case) the 3.4 with $total because that's the variable name that holds the value of the hits that needs to be added or incremented within the array.

    Thanks.

    Eric