in reply to converting an array to an hash
How can I convert a list to a hash data structure?/i>
Besies that you say "list" in one place and "array" in another which may indicate some confusion on your part (or mine), that sounds like a strange (if not dubious) think to want to do.
But, if your sure it is what you need, here are two ways. Judge the comparative merits for yourself:
sub listToIndexedHashRef{ my $n=0; my %hash = map{ $n++, $_} @_; return \%hash };; my $hashRef = listToIndexedHash( qw[the quick brown fox ] );; pp $hashRef;; { "0" => "the", 1 => "quick", 2 => "brown", 3 => "fox" } sub listToIndexedList{ my $n=0; return map{ $n++, $_} @_; };; my %hash = listToIndexedList( qw[the quick brown fox ] );; pp \%hash;; { "0" => "the", 1 => "quick", 2 => "brown", 3 => "fox" }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: converting an array to an hash
by savio (Initiate) on Dec 22, 2014 at 17:27 UTC | |
by BrowserUk (Patriarch) on Dec 22, 2014 at 17:46 UTC | |
by savio (Initiate) on Dec 22, 2014 at 17:54 UTC | |
by BrowserUk (Patriarch) on Dec 22, 2014 at 18:49 UTC | |
by karlgoethebier (Abbot) on Dec 22, 2014 at 20:26 UTC |