Question about arrays of hashes

Question about arrays of hashes

am 11.01.2008 23:35:49 von 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!

Re: Question about arrays of hashes

am 12.01.2008 00:03:58 von Damian Lukowski

Steve schrieb:
> 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'}

Re: Question about arrays of hashes

am 12.01.2008 00:08:12 von Gunnar Hjalmarsson

Steve wrote:
> 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';

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Question about arrays of hashes

am 12.01.2008 00:08:31 von xhoster

Steve wrote:
> 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.