Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How can I assign the elements in an array to only the key values in a hash?

by blackjudas (Pilgrim)
on Mar 30, 2001 at 22:10 UTC ( [id://68448]=perlquestion: print w/replies, xml ) Need Help??

blackjudas has asked for the wisdom of the Perl Monks concerning the following question: (hashes)

I have an array that keeps returning matches in a string, each match must be inserted into a hash as an empty key, this is what I have and it's obviuosly flawed.

keys(%hash) = @array;

Or should I go the route:
my $var = 0; $var = @array; for ($i = 0; $i <= $var; $i++) { %hash = ($array[$i] => ""); }


Any takers?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How can I assign the elements in an array to only the key values in a hash?
by ariels (Curate) on Apr 10, 2001 at 11:26 UTC
    Least confusing and most idiomatic is
    @hash{@keys} = ();
    Unlike arturo's code, it writes a list where it means to write a list. See MeowChow's comments; here all values get set to undef "implicitly".
Re: How can I assign the elements in an array to only the key values in a hash?
by MeowChow (Vicar) on Mar 31, 2001 at 06:19 UTC
    I prefer:
    undef @hash{@array};
    While this does exactly the same thing as the above, the syntax of @hash{@array} = undef is misleading, since only the first hash value is explictly set to undef, while the rest are implicitly undef'd. Novices who see this code might later write the following, expecting it to set all hash values to 1:
    @hash{@array} = 1;
    Of course, only the first hash value is set to 1 in this case, and the rest become undef.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: How can I assign the elements in an array to only the key values in a hash?
by arturo (Vicar) on Mar 30, 2001 at 22:12 UTC
Re: How can I assign the elements in an array to only the key values in a hash?
by stefp (Vicar) on Mar 31, 2001 at 20:59 UTC
    Q: I have an array that keeps returning matches in a string, each match must be inserted into a hash as an empty key (..)

    If i judge by your code, you want empty values. Anyway empty keys does not make sense. Because each key of a hash must be unique within the hash set of keys

    $hash{$_}=  '' for @array;

    usually conunting number of matches for each strings makes more sense:

    $hash{$_}++ for @array;

    -- stef

      If you're just trying to remove duplicates from the array (which I'm not sure you are), you can do this:

      my @array = getMatches();
      my %tmp;
      @tmp{@array} = (undef) x @array;
      @array = keys %tmp;
      undef %tmp;
      

      This takes advantage of Perl's hash slices and can be incredibly useful for set-like operations.

      Jerry Goure

Re: How can I assign the elements in an array to only the key values in a hash?
by Madam (Sexton) on Mar 09, 2006 at 08:48 UTC
    It may be too late to answer,but still..,
    my %hash1 = map {$_ =>1 } @array;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://68448]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found