tip : array nx2 to hash in one line

# nothing great, just how to convert an nx2 array of arrays to a hash in one
line.

my [at] array = ( ['k1','v1'], ['k2','v2'], ['k3','v3'] );

my %hash;
[at] hash{map{$_->[0]} [at] array}=map{$_->[1]} [at] array;

print $hash{'k2'}
George [ Mi, 09 Mai 2007 15:44 ] [ ID #1708406 ]

Re: tip : array nx2 to hash in one line

On May 9, 9:44 am, "George" <geo... [at] localhost.com> wrote:
> # nothing great, just how to convert an nx2 array of arrays to a hash in one
> line.
>
> my [at] array = ( ['k1','v1'], ['k2','v2'], ['k3','v3'] );
>
> my %hash;
> [at] hash{map{$_->[0]} [at] array}=map{$_->[1]} [at] array;
>
> print $hash{'k2'}

Wow. That's incredibly over-complicated and illegible.

my [at] array = ( ['k1','v1'], ['k2','v2'], ['k3','v3'] );
my %hash = map { [at] {$_} } [at] array;

Paul Lalli
Paul Lalli [ Mi, 09 Mai 2007 16:24 ] [ ID #1708407 ]

Re: tip : array nx2 to hash in one line

Paul Lalli wrote:
> On May 9, 9:44 am, "George" <geo... [at] localhost.com> wrote:
>># nothing great, just how to convert an nx2 array of arrays to a hash in one
>>line.
>>
>>my [at] array = ( ['k1','v1'], ['k2','v2'], ['k3','v3'] );
>>
>>my %hash;
>> [at] hash{map{$_->[0]} [at] array}=map{$_->[1]} [at] array;
>>
>>print $hash{'k2'}
>
> Wow. That's incredibly over-complicated and illegible.
>
> my [at] array = ( ['k1','v1'], ['k2','v2'], ['k3','v3'] );
> my %hash = map { [at] {$_} } [at] array;

And that has too much punctuation. :-)

my [at] array = ( ['k1','v1'], ['k2','v2'], ['k3','v3'] );
my %hash = map [at] $_, [at] array;



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
someone [ Mi, 09 Mai 2007 16:51 ] [ ID #1708408 ]

Re: tip : array nx2 to hash in one line

On May 9, 10:51 am, "John W. Krahn" <some... [at] example.com> wrote:
> Paul Lalli wrote:
> > On May 9, 9:44 am, "George" <geo... [at] localhost.com> wrote:
> >># nothing great, just how to convert an nx2 array of arrays to a hash in one
> >>line.
>
> >>my [at] array = ( ['k1','v1'], ['k2','v2'], ['k3','v3'] );
>
> >>my %hash;
> >> [at] hash{map{$_->[0]} [at] array}=map{$_->[1]} [at] array;
>
> >>print $hash{'k2'}
>
> > Wow. That's incredibly over-complicated and illegible.
>
> > my [at] array = ( ['k1','v1'], ['k2','v2'], ['k3','v3'] );
> > my %hash = map { [at] {$_} } [at] array;
>
> And that has too much punctuation. :-)
>
> my [at] array = ( ['k1','v1'], ['k2','v2'], ['k3','v3'] );
> my %hash = map [at] $_, [at] array;

Nah, it was very specifically the way I wanted it. I hate the "drop
the { } if the reference is a simple scalar" shortcut, because people
inevitably forget the "if" part of that. And I always prefer block-
map over expression-map, because of the case in which your EXPR is
actually a hash reference that you want to create, and Perl guesses
wrong, thinking you're using block-syntax. If you get into the habbit
of using block at all times, you'll know to use a double { { for
hashref-within-block, and avoid the ambiguity.

Paul Lalli
Paul Lalli [ Mi, 09 Mai 2007 21:36 ] [ ID #1708409 ]
Perl » alt.perl » tip : array nx2 to hash in one line

Vorheriges Thema: perl and IPv6: what about?
Nächstes Thema: how to join array into array