In Perl, how do I create a hash whose keys come from a given array?

后端 未结 14 693
你的背包
你的背包 2021-01-29 19:53

Let\'s say I have an array, and I know I\'m going to be doing a lot of \"Does the array contain X?\" checks. The efficient way to do this is to turn that array into a hash, wher

相关标签:
14条回答
  • 2021-01-29 20:31

    You can place the code into a subroutine, if you don't want pollute your namespace.

    my $hash_ref =
      sub{
        my %hash;
        @hash{ @{[ qw'one two three' ]} } = undef;
        return \%hash;
      }->();
    

    Or even better:

    sub keylist(@){
      my %hash;
      @hash{@_} = undef;
      return \%hash;
    }
    
    my $hash_ref = keylist qw'one two three';
    
    # or
    
    my @key_list = qw'one two three';
    my $hash_ref = keylist @key_list;
    

    If you really wanted to pass an array reference:

    sub keylist(\@){
      my %hash;
      @hash{ @{$_[0]} } = undef if @_;
      return \%hash;
    }
    
    my @key_list = qw'one two three';
    my $hash_ref = keylist @key_list;
    
    0 讨论(0)
  • 2021-01-29 20:33

    There is a presupposition here, that the most efficient way to do a lot of "Does the array contain X?" checks is to convert the array to a hash. Efficiency depends on the scarce resource, often time but sometimes space and sometimes programmer effort. You are at least doubling the memory consumed by keeping a list and a hash of the list around simultaneously. Plus you're writing more original code that you'll need to test, document, etc.

    As an alternative, look at the List::MoreUtils module, specifically the functions any(), none(), true() and false(). They all take a block as the conditional and a list as the argument, similar to map() and grep():

    print "At least one value undefined" if any { !defined($_) } @list;

    I ran a quick test, loading in half of /usr/share/dict/words to an array (25000 words), then looking for eleven words selected from across the whole dictionary (every 5000th word) in the array, using both the array-to-hash method and the any() function from List::MoreUtils.

    On Perl 5.8.8 built from source, the array-to-hash method runs almost 1100x faster than the any() method (1300x faster under Ubuntu 6.06's packaged Perl 5.8.7.)

    That's not the full story however - the array-to-hash conversion takes about 0.04 seconds which in this case kills the time efficiency of array-to-hash method to 1.5x-2x faster than the any() method. Still good, but not nearly as stellar.

    My gut feeling is that the array-to-hash method is going to beat any() in most cases, but I'd feel a whole lot better if I had some more solid metrics (lots of test cases, decent statistical analyses, maybe some big-O algorithmic analysis of each method, etc.) Depending on your needs, List::MoreUtils may be a better solution; it's certainly more flexible and requires less coding. Remember, premature optimization is a sin... :)

    0 讨论(0)
  • 2021-01-29 20:35

    Also worth noting for completeness, my usual method for doing this with 2 same-length arrays @keys and @vals which you would prefer were a hash...

    my %hash = map { $keys[$_] => $vals[$_] } (0..@keys-1);

    0 讨论(0)
  • 2021-01-29 20:40

    You might also want to check out Tie::IxHash, which implements ordered associative arrays. That would allow you to do both types of lookups (hash and index) on one copy of your data.

    0 讨论(0)
  • 2021-01-29 20:46
    @hash{@keys} = undef;
    

    The syntax here where you are referring to the hash with an @ is a hash slice. We're basically saying $hash{$keys[0]} AND $hash{$keys[1]} AND $hash{$keys[2]} ... is a list on the left hand side of the =, an lvalue, and we're assigning to that list, which actually goes into the hash and sets the values for all the named keys. In this case, I only specified one value, so that value goes into $hash{$keys[0]}, and the other hash entries all auto-vivify (come to life) with undefined values. [My original suggestion here was set the expression = 1, which would've set that one key to 1 and the others to undef. I changed it for consistency, but as we'll see below, the exact values do not matter.]

    When you realize that the lvalue, the expression on the left hand side of the =, is a list built out of the hash, then it'll start to make some sense why we're using that @. [Except I think this will change in Perl 6.]

    The idea here is that you are using the hash as a set. What matters is not the value I am assigning; it's just the existence of the keys. So what you want to do is not something like:

    if ($hash{$key} == 1) # then key is in the hash
    

    instead:

    if (exists $hash{$key}) # then key is in the set
    

    It's actually more efficient to just run an exists check than to bother with the value in the hash, although to me the important thing here is just the concept that you are representing a set just with the keys of the hash. Also, somebody pointed out that by using undef as the value here, we will consume less storage space than we would assigning a value. (And also generate less confusion, as the value does not matter, and my solution would assign a value only to the first element in the hash and leave the others undef, and some other solutions are turning cartwheels to build an array of values to go into the hash; completely wasted effort).

    0 讨论(0)
  • 2021-01-29 20:46

    I always thought that

    foreach my $item (@array) { $hash{$item} = 1 }
    

    was at least nice and readable / maintainable.

    0 讨论(0)
提交回复
热议问题