Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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 ( #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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?
    [holli]: but then you DO give a fuck
    [1nickt]: I don;t think beliefs should be down-voted, just behaviours.
    [james28909]: im not quite how to explain it any better nick. you evolved from ignorance to intelligence. not the other way. the universe evolves from gas coulds and debris into planets stars and galaxies ect. it doesnt happen any other way. hence it has ....
    [james28909]: some kind of logic behind it
    [james28909]: and that is also anothe rpoint i made, i think it has to do with perception of the world around you. most people think of evolution on a human scale. why could life evolve on this planet? because this planet evolved in this solar system. and so on.
    [holli]: here's something for you to watch, James. I think you will like it
    [erix]: for the record: I have not downvoted anyone on that subthread that was my fault
    [james28909]: there are all kinds of things that had to happen to let life come to be. but at the same time, life may not be the end goal IF there is any kind of end goal lol
    [james28909]: well who is the person who gets to decide which behaviour is worthy of a downvote? a person with their own beliefs? xD
    [erix]: teleology -- I've never understood why that was thunk up

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (18)
    As of 2017-12-15 14:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What programming language do you hate the most?




















      Results (433 votes). Check out past polls.

      Notices?