in reply to Re: Transform Array Ref to Hash Ref - Where's my error?
in thread Transform Array Ref to Hash Ref - Where's my error?

No need (and probably shouldn't ... though i see that OP did) to modify the input w/the $$ary[1] = '0' statement ..
my %hash; foreach my $row ( @array ) { my $k = defined($row->[1]) ? $row->[1] : 0; push @{ $hash{$k} }, $row->[0]; }
or a more condensed version:
my %hash; push @{ hash{ defined($_->[1]) ? $_->[1] : 0 } }, $_->[0] for @array;

Replies are listed 'Best First'.
Re^3: Transform Array Ref to Hash Ref - Where's my error?
by ayrnieu (Beadle) on Feb 14, 2006 at 07:20 UTC
    Even more condensed:
    my %h; push @{$h{$_->[1]||0}}, $_->[0] for @a;