Perl question: array member of referenced hash

Perl question: array member of referenced hash

Dov Grobgeld dov.grobgeld at gmail.com
Sat Oct 24 19:57:08 IST 2009


Noam beat me to it, but here's perl solution without additional variables:

#!/usr/bin/perl

%hash = (a=>['moo','goo','woo'],
         foo=>3,
         baz=>5);

$ref = \%hash;
foreach my $elem (@{$ref->{a}})
{
    print "$elem\n";
}

Regards,
Dov

2009/10/24 Shachar Shemesh <shachar at shemesh.biz>

>  Hi all,
>
> $ref is a reference to a hash. Hash contains a component called "a" which
> is an array. I would like to iterate all elements of said array.
>
> I would like to do something along the lines of:
> foreach my $elem @%$ref{a}
> {
>     print "$elem\n";
> }
>
> Which, I think it clear, does not work. I also tried:
> foreach my $elem @(%$ref){a}
> {
>     print "$elem\n";
> }
>
> which complains about:
> Bareword "a" not allowed while "strict subs" in use
>
> and:
> foreach my $elem @(%$ref){'a'}
> {
>     print "$elem\n";
> }
>
> which complains about:
> Global symbol "$elem" requires explicit package name at line 3 (i.e. -
> inside the for).
>
> Any help would be appreciated.
>
> Thanks,
> Shachar
>
> --
> Shachar Shemesh
> Lingnu Open Source Consulting Ltd.http://www.lingnu.com
>
>
> --
> Shachar Shemesh
> Lingnu Open Source Consulting Ltd.http://www.lingnu.com
>
>
> _______________________________________________
> Linux-il mailing list
> Linux-il at cs.huji.ac.il
> http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.cs.huji.ac.il/pipermail/linux-il/attachments/20091024/fb9c51f4/attachment.html>


More information about the Linux-il mailing list