knarf has asked for the wisdom of the Perl Monks concerning the following question:
Is there an elegant/efficient way to build a hash from 2 arrays :
Because of the list nature of hash, I thought I could do that with a zip-like function which unfortunately does not exists in the standard Perl dist. (or it's really well hidden).
I finally came up with this :
%h = (); @keys = qw/a b c/; @values = qw/foo bar baz/; $h{$k} = $v while defined($k = pop @keys) && defined($v = pop @values) +;
And I just realized that under use strict I must declare $k and $v outside the while to make it work... Great. Have you got something better ?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Making a hash with lists
by Fletch (Bishop) on Jun 27, 2008 at 14:16 UTC | |
|
Re: Making a hash with lists
by kyle (Abbot) on Jun 27, 2008 at 14:34 UTC | |
|
Re: Making a hash with lists
by holli (Abbot) on Jun 27, 2008 at 14:36 UTC | |
|
Re: Making a hash with lists
by pc88mxer (Vicar) on Jun 27, 2008 at 14:47 UTC | |
by BrowserUk (Patriarch) on Jun 27, 2008 at 16:18 UTC | |
by TGI (Parson) on Jun 27, 2008 at 18:20 UTC | |
by BrowserUk (Patriarch) on Jun 27, 2008 at 18:26 UTC | |
by Porculus (Hermit) on Jun 28, 2008 at 12:20 UTC | |
by BrowserUk (Patriarch) on Jun 28, 2008 at 13:58 UTC | |
by Porculus (Hermit) on Jun 28, 2008 at 16:29 UTC | |
|