Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I have 4 arrays that I want to map into a hash. Could someone show me a neat way to do it that doesn't involve modules?

I have one array of keys and three of values, all are ordered and of the same length.

Thanks!

Replies are listed 'Best First'.
Re: hashes with multiple values
by Roy Johnson (Monsignor) on Oct 19, 2005 at 15:42 UTC
    my %hash = map { ($keys[$_] => [$ar1[$_], $ar2[$_], $ar3[$_]]) } 0..$# +keys;

    Caution: Contents may have been coded under pressure.
Re: hashes with multiple values
by fizbin (Chaplain) on Oct 19, 2005 at 16:15 UTC
    Someone's already replied with what's probably the best way, but it wasn't quite obfuscated enough for me:
    my%hash=map{$_,[map{shift@$_}\@val1,\@val2,\@val3]}@keys;
    Of course, that destroys your value arrays, but you can always reconstitute them from %hash and @keys.
    --
    @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re: hashes with multiple values
by chester (Hermit) on Oct 19, 2005 at 17:31 UTC
    Another way you shouldn't do it:

    push@{$hash{$keys[$_%@keys]}},(@val1,@val2,@val3)[$_]for(0..@keys*3-1);

Re: hashes with multiple values
by shemp (Deacon) on Oct 19, 2005 at 18:00 UTC
    I just upvoted all the previous responses for giving nice obfuscated answers.

    Why?

    Probably for the same reason that obfuscated answers were given...

    Me smells homework!


    I use the most powerful debugger available: print!