Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

array to hash key value pairs

by lomSpace (Scribe)
on Feb 11, 2013 at 20:46 UTC ( #1018238=perlquestion: print w/ replies, xml ) Need Help??
lomSpace has asked for the wisdom of the Perl Monks concerning the following question:

I want to use map function to turn an array into key value pairs. The odd numbers are the keys and the even numbers are the values.

my @gene = qw(ATAD3B 83858 ATAD3A 55210 SSU72 29101 SLC35E2 9906 GNB1 2782 TMEM52 339456 GABRD 2563); my %gene = map{$_ => 0} @gene;

Comment on array to hash key value pairs
Download Code
Replies are listed 'Best First'.
Re: array to hash key value pairs
by toolic (Bishop) on Feb 11, 2013 at 20:51 UTC
    List value constructors
    use warnings; use strict; my @gene = qw( ATAD3B 83858 ATAD3A 55210 SSU72 29101 SLC35E2 9906 GNB1 2782 TMEM52 339456 GABRD 2563 ); my %gene = @gene;
      what about a foreach?
      my %Gene; for my $index (@gene) { $Gene{$gene[$index]} = $gene[$index+1] if $index%2; }
      J -
      Hi toolic

      Your code just turns array into hash only. What about "The odd numbers are the keys and the even numbers are the values."

      lomSpace has to clarify this one!

      I just figured out how to do it

      %gene = map{S_} @gene;

      Thanks!

Re: array to hash key value pairs
by punch_card_don (Curate) on Feb 11, 2013 at 21:52 UTC
    deleted post

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2015-08-03 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (24 votes), past polls