Robert Citek wrote:
> 
> Is there an easier, more compact way to convert two arrays, on with 
> keys and the other with values, into a hash?
> 
> I have this sample code:
> 
>      1  #!/usr/local/bin/perl -w
>      2
>      3  my $hash ;
>      4  my @keys =qw(col1 col2 col3);
>      5  @vals=qw(a b c);
>      6  for ($i=0; $i<=2 ; $i++) {
>      7    $hash->{$keys[$i]}=$vals[$i] ;
>      8  }
>      9
>     10  print join("\t", keys %$hash) . "\n";
>     11  print join("\t", values %$hash) . "\n";
> 
> which works:
> 
>   $ ./sample.v01.pl
>   col3    col1    col2
>   c       a       b
> 
> However, I've tried to replace the for loop with something more 
> compact, but this doesn't work.  That is, I've replace lines 6-8 with 
> this line:
> 
>   $hash->[EMAIL PROTECTED]@vals;
> 

Close. You want a hash slice.

@[EMAIL PROTECTED] = @vals;

A marvelously Perlish construct.

http://danconia.org

> But get this:
> 
>   $ ./simple.v02.pl
>   3
>   3
> 
> Can this be done, or is the for loop the "accepted" way?
> 
> Regards,
> - Robert
> http://www.cwelug.org/downloads
> Help others get OpenSource software.  Distribute FLOSS
> for Windows, Linux, *BSD, and MacOS X with BitTorrent
> 
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to