in reply to Creating a hash from arrays
%hash = map { join( q{ }, $fname[$_], $lname[$_] ) => $state[$_] } 0 + .. $#fname;
Update: Though it's not necessarily better in this case, it is sometimes convenient to place the map inside the key brackets of a hash slice:
@hash{ map{ join q{ }, $fname[$_], $lname[$_] } 0 .. $#fname } += @state;
List::MoreUtils might be fun... let's see.....:
use List::MoreUtils qw( zip pairwise ); # ... %hash = zip @{[ pairwise { join q( ), $a, $b } @fname, @lname ]}, + @state;
Maybe that's just ridiculous. ;)
I should mention that if you didn't have the requirement of joining @fname and @lname stringwise, and instead were dealing with just a single list of keys, and a single list of values, the hash slice technique would be an ideal solution:
@hash{@names} = @states;
It doesn't get more succinct than that.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Creating a hash from arrays
by cspctec (Sexton) on Nov 02, 2013 at 22:24 UTC | |
by BrowserUk (Patriarch) on Nov 02, 2013 at 22:58 UTC | |
by cspctec (Sexton) on Nov 02, 2013 at 23:25 UTC | |
by AnomalousMonk (Archbishop) on Nov 02, 2013 at 23:38 UTC | |
by Laurent_R (Canon) on Nov 03, 2013 at 16:43 UTC | |
by marinersk (Priest) on Nov 03, 2013 at 01:31 UTC | |
by AnomalousMonk (Archbishop) on Nov 02, 2013 at 22:42 UTC | |
by cspctec (Sexton) on Nov 02, 2013 at 22:56 UTC | |
by AnomalousMonk (Archbishop) on Nov 02, 2013 at 23:30 UTC | |
by LanX (Saint) on Nov 02, 2013 at 23:32 UTC | |
by tangent (Parson) on Nov 03, 2013 at 16:53 UTC |