assigning multiple hash values to multiple variables

P

Patrick

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,-slice-hash,-slice

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

then you can simply assign
($scalar1,$scalar2) = @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) = @hashref->{key1,$key2};

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

Still doesn't work.

Anyone has ideas ?

Thanks very much.

Patrick
 
M

Matt Garrish

Patrick said:
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,-slice-hash,-slice

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

then you can simply assign
($scalar1,$scalar2) = @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) = @hashref->{key1,$key2};

No one likes a multi-poster:

($scalar1,$scalar2) = @{$hashref}{'key1','key2'};

Matt
 
P

Patrick

Matt said:
No one likes a multi-poster:

($scalar1,$scalar2) = @{$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
 
B

Brian McCauley

Matt said:
No one likes a multi-poster:
Agreed.

($scalar1,$scalar2) = @{$hashref}{'key1','key2'};

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

($scalar1,$scalar2) = @$hashref{'key1','key2'};
 
P

Paul Lalli

Brian said:
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
 
B

Brian McCauley

Paul said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top