Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

converting ana array to a hash

by hotshot (Prior)
on Jul 24, 2002 at 13:04 UTC ( [id://184802]=perlquestion: print w/replies, xml ) Need Help??

hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys !

A simple one. How can I convert an array of n elements to a hash with those n elements as keys? (the values are not important to me).

Second one. I have to run on the above hash as follows:

while (1) { for $key (keys(%myhash)) { if (&isPassTest()) { delete($key); } } }

like that I run on a decresing hash %myhashtill every element passes the test and the hash is empty. Will it work or removing elements while running on the hash can cause problems of some sort?

Thanks.

Hotshot

Edited: ~Wed Jul 24 15:26:45 2002 (GMT) by footpad: Added missing </code> tag

Replies are listed 'Best First'.
Re: converting ana array to a hash
by broquaint (Abbot) on Jul 24, 2002 at 13:17 UTC
    How can I convert an array of n elements to a hash with those n elements as keys?
    Like so
    my @array = qw( foo bar baz quux ); my %hash = map { $_ => 1 } @array;
    Will it work or removing elements while running on the hash can cause problems of some sort?
    You need to change
    delete($key);
    to
    delete($myhash{$key});
    And assuming isPassTest() is performing the tests correctly it should work fine, although you will need to exit the loop at some point e.g last if 0 == keys %myhash;
    HTH

    _________
    broquaint

Re: converting ana array to a hash
by jmcnamara (Monsignor) on Jul 24, 2002 at 13:26 UTC

    The best way to do this is via a hash slice.

    In the first example the hash values will all be undef*. To assign a value to all the keys you can use the second method:

    my @a = (1..9); my %h; # hash slice @h{@a} = (); # Same thing but sets each value to 1 @h{@a} = (1) x @a;

    Hash, and array, slices are explained in perldata.

    --
    John.

    * It is worth noting that the assignment in the first example is only to the hash value associated with the first element of the array.

Re: converting ana array to a hash
by giulienk (Curate) on Jul 24, 2002 at 13:19 UTC
    1)my %hash = map {$_ => 1} @array;
    2) you got to use delete $myhash{$key} to erase it from %myhash and then you're gonna have no problem as the for loop iterates over the elements of the anonymous array created by keys(%myhash)
    Update: broquaint can type faster than me ;)


    $|=$_="1g2i1u1l2i4e2n0k",map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g

Re: converting ana array to a hash
by simeon2000 (Monk) on Jul 24, 2002 at 13:22 UTC
    First one is pretty simple:

    my %hash = (); my @array = qw /fred jim john joe/; @hash{ @array } = @array;

    ... since you're not worried about the values ;)

    "Falling in love with map, one block at a time." - simeon2000

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-25 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found