Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Hash and arrays

by palette (Scribe)
on Aug 04, 2005 at 08:28 UTC ( #480716=perlquestion: print w/ replies, xml ) Need Help??
palette has asked for the wisdom of the Perl Monks concerning the following question:

How to build a hash using two arrays.

Consider I want the first element of the array to be the first key of the hash and the first element of the second array as the value for the corresponding hash key and so on

Please tell me how to go around this.
Thank you

Comment on Hash and arrays
Re: Hash and arrays
by holli (Monsignor) on Aug 04, 2005 at 08:33 UTC
    You mean like this?
    my @k = ("a", "b", "c"); my @v = (1, 2, 3); my %h = map { $k[$_] => $v[$_] } (0..$#k);


    holli, /regexed monk/

      Or, in a similar way, use a hash slice to populate the hash

      #!/usr/bin/perl use strict; use warnings FATAL => "all"; use Data::Dumper; my @k = ("a", "b", "c"); my @v = (1, 2, 3); my %h; @h{@k} = @v; print Dumper \%h; __END__ output $VAR1 = { 'c' => 3, 'a' => 1, 'b' => 2 };

      cheers

      thinker

Re: Hash and arrays
by TedPride (Priest) on Aug 04, 2005 at 08:45 UTC
    @hash{@keys} = @vals;

    EDIT: I've often wondered this. How come my answer showed up before thinker's did, yet now it's below his? Is order determined when you start your post, rather than when you complete it?

      thinker replied to holli's post, which appeared before yours. You'll notice that the answers in PM are arranged in a tree-like fashion, so holli's post whole subtree will always show up before your post here.

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.
      Although, despite the fact that people can reply to an earlier reply to 'jump the queue', as it might appear, your reply will be less indented i.e. at a higher level - from my point of view, when reading replies less indent-depth carries more weight, because it is more likely to address the original issue - hmmm gives me an idea for a poll.

      One world, one people

Re: Hash and arrays
by TedPride (Priest) on Aug 04, 2005 at 11:39 UTC
    Hmm. I personally find the whole tree structure thing confusing when there's no indented list of posts. It's much better to just list the posts sequentially. I always reply to the original poster so my posts aren't below the max shown depth.
      I personally find the whole tree structure thing confusing when there's no indented list of posts.

      But the posts _are_ indented to the correct level. Or, at least, they are in the view that I see.

      I always reply to the original poster so my posts aren't below the max shown depth.

      Which leads to confusing situations (like the post that I'm replying to) where your post has nothing to do with the one that it's a reply to. It's actually a reply to a completely different post. Not sure how you can think that's _less_ confusing :)

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-09-19 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls