in reply to DBI fetchall_arrayref
I'd hold the consolidated date using a hash the following way
resulting in %hash == ( apple => [ a1, a2, a3 ], )my $array = [[apple, a1], [apple, a2], [apple, a3]]; my %hash = (); foreach (@{$array}) { $hash{$_->[0]} = [] unless defined($hash{$_->[0]}); push (@{$hash{$_->[0]}}, $_->[1]); }
Update:Another way to get your whished for result [[apple, a1, a2, a3 ]]:
use List::Util qw(reduce); my $array = [[apple, a1], [apple, a2], [apple, a3]]; my $result = []; unshift(@$array, []); push @$result, reduce { push(@$a, $b->[0]) unless exists $a->[0]; push(@$a, $b->[1]); $a; } @$array;
regards,
tomte
An intellectual is someone whose mind watches itself.
-- Albert Camus
|
|---|