in reply to Two arrays. One hash.

Zaxo and Limbic~Region showed you how to do it with a hash slice, and that's definately the most efficient mechanism.

Just be careful to avoid this sort of situation:

my @keyset = qw/this repeat that repeat/ my @valset = qw/jerry george elaine kramer/; my %hash; @hash{@keyset} = @valset; print "$_: $hash{$_}\n" foreach keys %hash; __OUTPUT__ that: elaine this: jerry repeat: kramer

For nonunique keys, only the last item assigned to a given nonunique key will be preserved. Poor 'george' was discarded.


Dave


"If I had my life to live over again, I'd be a plumber." -- Albert Einstein

Replies are listed 'Best First'.
Re: Re: Two arrays. One hash.
by Roger (Parson) on Nov 29, 2003 at 23:47 UTC
    Just to add to davido's comment - would be nice to implement an array duplicated element detection mechanizm. Instead of assuming that the key array does not have duplicates, you might as well test for it. ;-)

    use strict; my @keyset = qw/this repeat that repeat/; my @valset = qw/jerry george elaine kramer/; print "\@keyset has ", hasdup(\@keyset) ? "duplicates (warning)" : "no duplicates", "\n"; print "\@valset has ", hasdup(\@valset) ? "duplicates" : "no duplicates", "\n"; sub hasdup { my $array = shift; # get the index of the elements in the uniq array my $last = $#{[keys %{{ map {$_ => 1} @$array }}]}; return $#{@$array} != $last; }
    And the output -
    @keyset has duplicates (warning) @valset has no duplicates

      And just to add to Roger's comment ... ;)

      I never use a hash slice unless i know that the array containing the keys has no duplicates. Now, i know that sometimes this just isn't practical, but i really only use hash slices when i know what the keys are going to be -- that is, i don't use them when i don't know what the keys are going to be. Besides, 99% of the time, you see the creating of a hash slice like so:

      my %month; my @month = qw(Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec); @month{0..$#month} = @month;
      ... that is, you declare the keys and the values. As long as this is true, you shouldn't have to worry about writing code to catch duplicates. On the other hand, something like:
      print "Enter keys (sep with space): "; chomp (my $key = <STDIN>); print "Enter vals (sep with space): "; chomp (my $val = <STDIN>); my %hash; @hash{ split '\s',$key } = split '\s',$val;
      is just asking for trouble. I would instead (sigh) explicitly code out a "standard" for loop to ensure that duplicates were caught, as well as a mismatching number of keys and vals.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)