Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dereferencing a hash value

This doesn't work:

 my %y = ("lkj",34);
 my %i = ("lkj",66);
 my @e = (\%y, \%i);
 my $u = ${%{$e[0]}}{"lkj"};

but this does:

         my %u = %{$e[0]};
         print $u{"lkj"};

If I don't feel like typing that extra line what do i do.

like image 550
Sam Adamsh Avatar asked May 20 '26 11:05

Sam Adamsh


1 Answers

You use the -> operator:

$e[0]->{"lkj"}

You can do something similar for arrayrefs, and it's even chainable:

my $eref = \@e;
print $eref->[0]->{"lkj"}

As a bonus, you can do all the setup in a single line too by using the {} shorthand for arrayrefs:

my @e = ( { lkj => 34 }, { lkj => 66 } );