multiple assignments with a HoHoA

A

Abhinav

Hi,

I have a hash variable, %table, which I populate as

my %table;
$table{'A'}{'B'}=['C','D','E'];

I now do

my $val1;
my $val2;
my $val2;

This :

($val1, $val2, $val3) = $table{'A'}{'B'}; #causes $val1, $val2 to
be undef, #$val3 = ARRAYREF

However, this :

$val1 = $table{'A'}{'B'}[0];
$val2 = $table{'A'}{'B'}[1];
$val3 = $table{'A'}{'B'}[2]; # Does the Right Thing.

Could someone point out what silly mistake I am committing in the 1st case,
and how to rectify it ?


Thanks

Abhinav
 
G

Gunnar Hjalmarsson

Abhinav said:
I have a hash variable, %table, which I populate as

my %table;
$table{'A'}{'B'}=['C','D','E'];

I now do

my $val1;
my $val2;
my $val2;

This :

($val1, $val2, $val3) = $table{'A'}{'B'}; #causes $val1, $val2 to
be undef, #$val3 = ARRAYREF

Suppose you mean that $val1 is assigned the array reference, not $val3.

Could someone point out what silly mistake I am committing in the 1st
case,

You don't dereference your array reference.
and how to rectify it ?

Dereference your array reference.

perldoc perlreftut
 
M

Mark Clements

Abhinav said:
I have a hash variable, %table, which I populate as

my %table;
$table{'A'}{'B'}=['C','D','E'];

OK: this is actually syntactic sugar for

$table{A}->{B} = [ 'C', 'D', 'E'];
# note you'd probably use qw here instead ie [qw( C D E )]

this sets $table{A}->{B} to be an arrayref

Perl allows you to specify multiple dereferences without the ->,
supposedly to make it easier to read

ie

$table{A}{B}{C} is the same as $table{A}->{B}->{C} but not the same as
$table->{A}->{B}->{C}.
($val1, $val2, $val3) = $table{'A'}{'B'}; #causes $val1, $val2 to
be undef, #$val3 = ARRAYREF
it should set $val1 *not* $val3: $table{A}->{B} is a scalar value ie an
arrayref, so the right-hand side only returns one value, which is
assigned to the first item in the list on the lhs.
However, this :

$val1 = $table{'A'}{'B'}[0];
$val2 = $table{'A'}{'B'}[1];
$val3 = $table{'A'}{'B'}[2]; # Does the Right Thing.
syntactic sugar is coming into play again.

$val1 = $table->{A}->{B}->[0];

and so on.

Could someone point out what silly mistake I am committing in the 1st
case, and how to rectify it ?
Not silly so much as missing some of the subtleties of perl data
structures - you need to check out

man perldsc

Mark
 
T

Tore Aursand

my %table;
$table{'A'}{'B'}=['C','D','E'];

I now do

my $val1;
my $val2;
my $val2;

I guess you mean '$val3' on that last one. No need to declare them before
you actually use them;

my ( $val1, $val2, $val3 ) = ...;
This :

($val1, $val2, $val3) = $table{'A'}{'B'}; #causes $val1, $val2 to
be undef, #$val3 = ARRAYREF

You need to dereference your array reference;

my ( $val1, $val2, $val3 ) = @{$table{'A'}{'B'}};
 
A

Abhinav

Tore said:
my %table;
$table{'A'}{'B'}=['C','D','E'];

I now do

my $val1;
my $val2;
my $val2;


I guess you mean '$val3' on that last one. No need to declare them before
you actually use them;

Oops ..yes .. :)
my ( $val1, $val2, $val3 ) = ...;




You need to dereference your array reference;

my ( $val1, $val2, $val3 ) = @{$table{'A'}{'B'}};

Thanks ! I skimmed through perldsc before my OP, but I did not grasp it. It
is much clearer now.

Even the above didn't work initially, thanks to precedence issues as I was
using the || operator directly after this assignment.

Thanks to everyone for the useful replies

Regards
Abhinav

P.S : The reason for this post was this week's QOTW. While it sounded a bit
complex, it was actually easy, and newbies might want to go through it. It
is available at http://perl.plover.com/qotw/r/current.txt (This is quiz #24)
 
G

Gunnar Hjalmarsson

Abhinav said:
I skimmed through perldsc before my OP, but I did not grasp it.

Of course not. The docs about references and data structures is
nothing you just "skim"!
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top