in reply to Perlish array to hash

Slices are your friend!

Notice for this to work with 'use strict' (and you wouldn't *dare* not use 'use strict', would you?), you can't declare AND initialize the hash in the same statement.

And for those of you that wonder what happens if the length of @a is unequal to that of @b (which was me, up until a few minutes ago), if @a is longer, the values that exist at positions greater than the length of @b will be inserted into hash as keys, and the value set to undefined. If @b is longer than @a, values that exist at positions greater than the length of @a are discarded/ignored.
#!/usr/local/bin/perl -w use strict; use Data::Dumper; { my @a = qw(one two three four); my @b = qw(1 2 3 4); my %hash = (); @hash {@a} = @b; print Dumper ([\%hash]); }

$VAR1 = [ { 'three' => 3, 'two' => 2, 'one' => 1, 'four' => 4 } ];
--Chris

e-mail jcwren

Replies are listed 'Best First'.
RE: (jcwren) RE: Perlish array to hash
by nop (Hermit) on Nov 06, 2000 at 05:25 UTC
    Thanks! Just what I was looking for.
    (And as for strict and warnings, I've seen the light; I am a believer. <g> )