Building a hash map out of two arrays

Building a hash map out of two arrays

am 06.12.2007 12:47:34 von Kira Yamato

Suppose you have two arrays

my @keys = qw/ a b c d /;
my @values = ( 1, 2, 3, 4 );

Does anyone know a slick way of building a hash with @keys and @values?

Thanks.

--

-kira

Re: Building a hash map out of two arrays

am 06.12.2007 12:56:02 von Ben Morrow

Quoth Kira Yamato :
> Suppose you have two arrays
>
> my @keys = qw/ a b c d /;
> my @values = ( 1, 2, 3, 4 );
>
> Does anyone know a slick way of building a hash with @keys and @values?

my %hash;
@hash{ @keys } = @values;

or

use List::MoreUtils qw/zip/;

my %hash = zip @keys, @values;

Ben

Re: Building a hash map out of two arrays

am 06.12.2007 13:05:01 von Glenn Jackman

At 2007-12-06 06:47AM, "Kira Yamato" wrote:
> Suppose you have two arrays
>
> my @keys = qw/ a b c d /;
> my @values = ( 1, 2, 3, 4 );
>
> Does anyone know a slick way of building a hash with @keys and @values?

Don't know how I'll score golf-wise, but this is compact:

my %h = map {$keys[$_], $values[$_]} (0 .. $#keys);

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Re: Building a hash map out of two arrays

am 06.12.2007 13:05:28 von Peter Makholm

Kira Yamato writes:

> my @keys = qw/ a b c d /;
> my @values = ( 1, 2, 3, 4 );
>
> Does anyone know a slick way of building a hash with @keys and @values?

Use a hash slice:

my %hash;
@hash{ @keys } = @values;

Read 'perldoc perldata' for the explanation.

//Makholm

Re: Building a hash map out of two arrays

am 06.12.2007 13:40:45 von Kira Yamato

On 2007-12-06 07:05:28 -0500, Peter Makholm said:

> Kira Yamato writes:
>
>> my @keys = qw/ a b c d /;
>> my @values = ( 1, 2, 3, 4 );
>>
>> Does anyone know a slick way of building a hash with @keys and @values?
>
> Use a hash slice:
>
> my %hash;
> @hash{ @keys } = @values;
>
> Read 'perldoc perldata' for the explanation.
>
> //Makholm

Nice. Thank you everyone for your help and quick response!

--

-kira

Re: Building a hash map out of two arrays

am 06.12.2007 20:13:58 von malatinszky

On Dec 6, 6:56 am, Ben Morrow wrote:
> Quoth Kira Yamato :
>
> > Suppose you have two arrays
>
> > my @keys = qw/ a b c d /;
> > my @values = ( 1, 2, 3, 4 );
>
> > Does anyone know a slick way of building a hash with @keys and @values?
>
> my %hash;
> @hash{ @keys } = @values;
>
>
> Ben

That's probably as slick as it gets.

Re: Building a hash map out of two arrays

am 06.12.2007 20:55:22 von Uri Guttman

>>>>> "m" == malatinszky writes:

m> On Dec 6, 6:56 am, Ben Morrow wrote:
>> Quoth Kira Yamato :
>>
>> > Suppose you have two arrays
>>
>> > my @keys = qw/ a b c d /;
>> > my @values = ( 1, 2, 3, 4 );
>>
>> > Does anyone know a slick way of building a hash with @keys and @values?
>>
>> my %hash;
>> @hash{ @keys } = @values;

m> That's probably as slick as it gets.

i wouldn't call using slices a slick trick. see more in my slices
tutorial:

http://sysarch.com/Perl/hash_slice.txt

as for slick, i like this trick but it will destroy one of the
arrays. and it allows for the hash declare in the same statement.
(you can choose which array to destroy):

my %hash = map { $_ => shift @values } @keys ;
my %hash = map { shift @keys => $_ } @values ;

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: Building a hash map out of two arrays

am 07.12.2007 11:00:35 von rvtol+news

Uri Guttman schreef:

> my slices tutorial:
> http://sysarch.com/Perl/hash_slice.txt

Ah, maybe you could add "How to return a slice's values, without
copying":

return sub { \@_ }->( @data{ @keys } );

(from Josh Jore in <20071102004522.GB25954@grenekatz.org>)

--
Affijn, Ruud

"Gewoon is een tijger."