assigning multiple hash values to multiple variables

Hi people,

I'm looking for info I just can't figure out through the manual.

Here is the doc :
http://perldoc.perl.org/perldata.html#Slices-slice-array%2c- slice-hash%2c-slice

I understand it says that if
%hash = ( key1, 'value1', key2, 'value2 };

then you can simply assign
($scalar1,$scalar2) = [at] hash{key1,key2};

Easy, simple, convenient.


Now I try to do it with a hashref.

$hashref = { key1 => 'value1', key2 => 'value2' };

One would imagine that would work:
($scalar1,$scalar2) = $hashref->{key1,$key2};
or lets say :
($scalar1,$scalar2) = [at] hashref->{key1,$key2};

I even tried :
($scalar1,$scalar2) = [at] {$hashref->{key1,$key2}};

Still doesn't work.

Anyone has ideas ?

Thanks very much.

Patrick
patrick [ Mi, 03 Mai 2006 01:38 ] [ ID #1299706 ]

Re: assigning multiple hash values to multiple variables

"Patrick" <spamsucks [at] microsoft.com> wrote in message
news:4457ed6e$0$298$7a628cd7 [at] news.club-internet.fr...
> Hi people,
>
> I'm looking for info I just can't figure out through the manual.
>
> Here is the doc :
> http://perldoc.perl.org/perldata.html#Slices-slice-array%2c- slice-hash%2c-slice
>
> I understand it says that if
> %hash = ( key1, 'value1', key2, 'value2 };
>
> then you can simply assign
> ($scalar1,$scalar2) = [at] hash{key1,key2};
>
> Easy, simple, convenient.
>
>
> Now I try to do it with a hashref.
>
> $hashref = { key1 => 'value1', key2 => 'value2' };
>
> One would imagine that would work:
> ($scalar1,$scalar2) = $hashref->{key1,$key2};
> or lets say :
> ($scalar1,$scalar2) = [at] hashref->{key1,$key2};
>

No, I don't think I would imagine that:

($scalar1,$scalar2) = [at] {$hashref}{'key1','key2'};

See perlref for more info or references and dereferencing them.

Matt
Matt Garrish [ Mi, 03 Mai 2006 02:06 ] [ ID #1299707 ]

Re: assigning multiple hash values to multiple variables

"Patrick" <spamsucks [at] microsoft.com> wrote in message
news:4457f18f$0$298$7a628cd7 [at] news.club-internet.fr...
> Hi people,
>
> I'm looking for info I just can't figure out through the manual.
>
> Here is the doc :
> http://perldoc.perl.org/perldata.html#Slices-slice-array%2c- slice-hash%2c-slice
>
> I understand it says that if
> %hash = ( key1, 'value1', key2, 'value2 };
>
> then you can simply assign
> ($scalar1,$scalar2) = [at] hash{key1,key2};
>
> Easy, simple, convenient.
>
>
> Now I try to do it with a hashref.
>
> $hashref = { key1 => 'value1', key2 => 'value2' };
>
> One would imagine that would work:
> ($scalar1,$scalar2) = $hashref->{key1,$key2};
> or lets say :
> ($scalar1,$scalar2) = [at] hashref->{key1,$key2};
>

No one likes a multi-poster:

($scalar1,$scalar2) = [at] {$hashref}{'key1','key2'};

Matt
Matt Garrish [ Mi, 03 Mai 2006 02:07 ] [ ID #1299708 ]

Re: assigning multiple hash values to multiple variables

Matt Garrish wrote:

> No one likes a multi-poster:
>
> ($scalar1,$scalar2) = [at] {$hashref}{'key1','key2'};
>
> Matt
>
>

Thanks for your help, it works perfectly
About the multipost, I actually posted the first message, and only then
thought about looking up if there was more than one group about perl.
Sorry for the noise.

Patrick
patrick [ Mi, 03 Mai 2006 12:09 ] [ ID #1299709 ]

Re: assigning multiple hash values to multiple variables

Matt Garrish wrote:
>
> No one likes a multi-poster:

Agreed.

> ($scalar1,$scalar2) = [at] {$hashref}{'key1','key2'};

Note that the {...} is redundant - it may however aid readability.

($scalar1,$scalar2) = [at] $hashref{'key1','key2'};
nobull67 [ Mi, 03 Mai 2006 13:06 ] [ ID #1299710 ]

Re: assigning multiple hash values to multiple variables

Brian McCauley wrote:
> Matt Garrish wrote:
> > ($scalar1,$scalar2) = [at] {$hashref}{'key1','key2'};
>
> Note that the {...} is redundant - it may however aid readability.

Noe that it { } is not "redundant", but merely "optional" - in *this*
particular case. That case being that the hashref is contained in a
simple scalar variable. If the hashref was instead an element of a
hash or array, (like $hashrefs[$i]), the { } would be required.

It is *always* okay to use the { }. It is only *sometimes* okay to
leave them out. When it doubt, use them.

Paul Lalli
Paul Lalli [ Mi, 03 Mai 2006 14:04 ] [ ID #1299711 ]

Re: assigning multiple hash values to multiple variables

Paul Lalli wrote:
> Brian McCauley wrote:
> > Matt Garrish wrote:
> > > ($scalar1,$scalar2) = [at] {$hashref}{'key1','key2'};
> >
> > Note that the {...} is redundant - it may however aid readability.
>
> Noe that it { } is not "redundant", but merely "optional" - in *this*
> particular case.

You appear to perceive the term "redundant" as in some way pejorative.
I was using the word "redundant" in the strict sense - meaning it can
be removed without altering the meaning.

The use of the term "optional" is best avoided to describe this concept
because saying something is optional it does not necessarily imply that
its omission has no semantic effect.

> That case being that the hashref is contained in a
> simple scalar variable. If the hashref was instead an element of a
> hash or array, (like $hashrefs[$i]), the { } would be required.

And if my uncle were a woman she'd be my aunt.

> It is *always* okay to use the { }. It is only *sometimes* okay to
> leave them out.

The same can be said of ( ) in expressions containing operators.

$foo = 2 * 3 + 4 * 5;
$foo = ((2 * 3) + (4 * 5)); # Parentheses are redundant in this
statement.

> When it doubt, use them.

Yes, when you think the reader may be in doubt about binding precedence
use {...} or (...) to clarify. That's what I meant by "aid
readability".

But do not use them to create the doubt in the first place. When
explaining syntax to someone it is wise to explain what parts of the
syntax are required and what parts are sugar that you've put in to aid
readbility.
nobull67 [ Mi, 03 Mai 2006 19:06 ] [ ID #1299712 ]

Re: assigning multiple hash values to multiple variables

Matt Garrish wrote:

> No one likes a multi-poster:
>
> ($scalar1,$scalar2) = [at] {$hashref}{'key1','key2'};
>
> Matt
>
>

Thanks for your help, it works perfectly
About the multipost, I actually posted the first message, and only then
thought about looking up if there was more than one group about perl.
Sorry for the noise.

Patrick
patrick [ Mi, 03 Mai 2006 12:09 ] [ ID #1299741 ]

Re: assigning multiple hash values to multiple variables

Matt Garrish wrote:
>
> No one likes a multi-poster:

Agreed.

> ($scalar1,$scalar2) = [at] {$hashref}{'key1','key2'};

Note that the {...} is redundant - it may however aid readability.

($scalar1,$scalar2) = [at] $hashref{'key1','key2'};
nobull67 [ Mi, 03 Mai 2006 13:06 ] [ ID #1299743 ]

Re: assigning multiple hash values to multiple variables

Brian McCauley wrote:
> Matt Garrish wrote:
> > ($scalar1,$scalar2) = [at] {$hashref}{'key1','key2'};
>
> Note that the {...} is redundant - it may however aid readability.

Noe that it { } is not "redundant", but merely "optional" - in *this*
particular case. That case being that the hashref is contained in a
simple scalar variable. If the hashref was instead an element of a
hash or array, (like $hashrefs[$i]), the { } would be required.

It is *always* okay to use the { }. It is only *sometimes* okay to
leave them out. When it doubt, use them.

Paul Lalli
Paul Lalli [ Mi, 03 Mai 2006 14:04 ] [ ID #1299746 ]

Re: assigning multiple hash values to multiple variables

Paul Lalli wrote:
> Brian McCauley wrote:
> > Matt Garrish wrote:
> > > ($scalar1,$scalar2) = [at] {$hashref}{'key1','key2'};
> >
> > Note that the {...} is redundant - it may however aid readability.
>
> Noe that it { } is not "redundant", but merely "optional" - in *this*
> particular case.

You appear to perceive the term "redundant" as in some way pejorative.
I was using the word "redundant" in the strict sense - meaning it can
be removed without altering the meaning.

The use of the term "optional" is best avoided to describe this concept
because saying something is optional it does not necessarily imply that
its omission has no semantic effect.

> That case being that the hashref is contained in a
> simple scalar variable. If the hashref was instead an element of a
> hash or array, (like $hashrefs[$i]), the { } would be required.

And if my uncle were a woman she'd be my aunt.

> It is *always* okay to use the { }. It is only *sometimes* okay to
> leave them out.

The same can be said of ( ) in expressions containing operators.

$foo = 2 * 3 + 4 * 5;
$foo = ((2 * 3) + (4 * 5)); # Parentheses are redundant in this
statement.

> When it doubt, use them.

Yes, when you think the reader may be in doubt about binding precedence
use {...} or (...) to clarify. That's what I meant by "aid
readability".

But do not use them to create the doubt in the first place. When
explaining syntax to someone it is wise to explain what parts of the
syntax are required and what parts are sugar that you've put in to aid
readbility.
nobull67 [ Mi, 03 Mai 2006 19:06 ] [ ID #1299769 ]
Perl » alt.perl » assigning multiple hash values to multiple variables

Vorheriges Thema: Convert TXT to CSV
Nächstes Thema: Convert plain text to html formatted text