Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

( #68448=categorized question: print w/ replies, xml ) Need Help??
Contributed by blackjudas on Mar 30, 2001 at 22:10 UTC
Q&A  > hashes


Description:

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?

Answer: How can I assign the elements in an array to only the key values in a hash?
contributed by ariels

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".
Answer: How can I assign the elements in an array to only the key values in a hash?
contributed by MeowChow

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
Answer: How can I assign the elements in an array to only the key values in a hash?
contributed by arturo

Use a hash slice :

my %hash; @hash{@array} = undef;
Answer: How can I assign the elements in an array to only the key values in a hash?
contributed by stefp

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

Answer: How can I assign the elements in an array to only the key values in a hash?
contributed by Madam

It may be too late to answer,but still..,

my %hash1 = map {$_ =>1 } @array;

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (9)
    As of 2014-10-01 11:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (9 votes), past polls