Question about arrays of hashes

S

Steve

Forgive a newbie question, but I'm hoping that someone can shed some
light for me on weirdness I'm seeing when trying to access a particular
hash from an array-of-hashes. I had a lot of difficulty assigning a
particular element to its own variable, but after some experimentation I
found that this works:


my @array = MyModule->getArrayOfHashes();
my $hash = $array[$index];
%hash = %$hash;


I have to first create the variable in scalar context, then do a funky
dereferencing thing while changing the context to hash. For one thing,
I'd like to better understand what is really going on here... I just
stumbled across this solution through experimentation and dumb luck, I
don't really understand it. Secondly, I'm wondering if there's some
shorthand technique for doing this that doesn't require three lines of
code. Thanks in advance!
 
D

Damian Lukowski

Steve said:
my @array = MyModule->getArrayOfHashes();
my $hash = $array[$index];
%hash = %$hash;

Get n'th hash-Reference from the array: (MyModule->getArrayOfHashes())[n]

Get value of hash-key 'foo' from n'th hash-reference from the array:
(MyModule->getArrayOfHashes())[n]->{'foo'}
 
G

Gunnar Hjalmarsson

Steve said:
Forgive a newbie question, but I'm hoping that someone can shed some
light for me on weirdness I'm seeing when trying to access a particular
hash from an array-of-hashes. I had a lot of difficulty assigning a
particular element to its own variable, but after some experimentation I
found that this works:

my @array = MyModule->getArrayOfHashes();
my $hash = $array[$index];
%hash = %$hash;

I have to first create the variable in scalar context, then do a
funky dereferencing thing while changing the context to hash. For one
thing, I'd like to better understand what is really going on here...

perldoc perlref
I'm wondering if there's some shorthand technique for doing this that
doesn't require three lines of code.

What's "this"? Copying one of the hashes to a named hash?

my %hash = %{ $array[$index] };

Or assigning a value to one of the anonymous hashes?

$array[$index]->{somekey} = 'somevalue';
 
X

xhoster

Steve said:
Forgive a newbie question, but I'm hoping that someone can shed
some light for me on weirdness I'm seeing when trying to access a
particular hash from an array-of-hashes. I had a lot of difficulty
assigning a particular element to its own variable, but after some
experimentation I found that this works:

my @array = MyModule->getArrayOfHashes();
my $hash = $array[$index];
%hash = %$hash;

I have to first create the variable in scalar context, then do a
funky dereferencing thing while changing the context to hash. For one
thing, I'd like to better understand what is really going on here... I
just stumbled across this solution through experimentation and dumb luck,
I don't really understand it.

See the "perldoc"s for perlref, perlreftut, and perldsc. I don't how to
describe it better than they do. (If I did, I would propose replacing them
with my better description.)

Secondly, I'm wondering if there's some
shorthand technique for doing this that doesn't require three lines of
code. Thanks in advance!

You need get the right element out of the list, which you do by
subscripting the list with the ()[] form:

my $hashref = (MyModule->getArrayOfHashes())[$index];

Although this does seem strange for me for semantic rather than syntactic
reasons. It seems like an odd OO module design that would lead me to
already know which index I want from a list when I don't yet have the list
but need to go to the module to get it. I know that is the way things work
with things like the "stat" function, but the stat function isn't OO.

Then you need to dereference that, which you do by %{} form:

my %hash=%{(MyModule->getArrayOfHashes())[$index]};

Although 90+% of the time, I would just get the $hashref and dereference
it as needed, rather than making a dereferenced copy up-front.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top