in reply to Pushing arrays into hashes

I'd be inclined to use grep or perhaps List::Util first.
my $party = (grep $_->[0] eq 'fish', @$pairs)[0]->[1];
use List::Util 'first'; my $party = (first {$_->[0] eq 'fish'} @$pairs)->[1];
though it's more a case of just being different ways to do it, rather than better ways.

What probably would be better is to make a function "as_hash" that would do the conversion to a hashref (the map), so that your code would be more self-documenting.

sub as_hash { my $aref = shift; return { map @$_, @$aref }; }

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Pushing arrays into hashes
by BiffBaker (Novice) on Jun 22, 2007 at 23:42 UTC
    Thank you! I will try this.