Syntactic Confectionery Delight PerlMonks

### Re: Array to hash refs

by Abigail-II (Bishop)
 on Aug 13, 2002 at 16:01 UTC ( #189843=note: print w/replies, xml ) Need Help??

in reply to Array to hash refs

```my @array = qw /a b c/;
my \$hash;
my \$val = 1;
(\$hash, \$hash -> {pop @array}, \$val) = (undef, \$val, \$hash) while @arr
+ay > 1;
\$hash -> {pop @array} = \$val;
Abigail

Replies are listed 'Best First'.
Re: Re: Array to hash refs
by BrowserUk (Patriarch) on Aug 14, 2002 at 08:06 UTC

Even with jeffa's notes below, it still took me a while to wrap this around my brain but when I had, I came up with this....

```my @array = qw /a b c d e f/;
my \$hash;
my \$val = 1;
(\$hash->{pop @array}, \$val, \$hash) = (\$val, \$hash, undef) while @array
+;
\$hash = \$val;

Which is slightly cleaner?, but that final assignment stood out so...

```my @array = qw /a b c d e f/;
my \$hash = 1;
my \$tmp;

(\$tmp->{pop @array}, \$hash, \$tmp) = (\$hash, \$tmp, undef)  while @array
+;

Slightly non-intuative to assign the value to the hashref at the start but...

Re: Re: Array to hash refs
by aufrank (Pilgrim) on Aug 13, 2002 at 16:25 UTC

```my @array = qw /a b c/;
my %hash = map {\$_ => 1} @array;
return \%hash;

can that be done with a map and a hashref rather than having to return a hashref? ripped off from tadman in Re: an easier way with grep, map, and/or sort?.
--au

```return {map {\$_ => 1} qw /a b c/}
Or did I understand your questing wrong?

Abigail

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://189843]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-01-26 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?