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
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
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
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
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'};
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
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.
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
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'};
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
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.
Perl » alt.perl » assigning multiple hash values to multiple variables