Perl question: array member of referenced hash

Perl question: array member of referenced hash

Noam Rathaus noamr at beyondsecurity.com
Sat Oct 24 19:56:13 IST 2009


Sorry a mistake...

foreach my $ptrelem (keys %hash) {

Should be

foreach my $key (keys %hash) {
 my $ptritem = %hash->{$key};

On Sat, Oct 24, 2009 at 7:55 PM, Noam Rathaus <noamr at beyondsecurity.com> wrote:
> Hi Shachar,
>
> First you can always use Data::Dumper:
> use Data::Dumper;
> print Dumper($ref);
>
> To make sure that the data is stored correctly.
>
> In regard to your question:
>
> my $ref;
> my %hash = %{$ref};
> foreach my $ptrelem (keys %hash) {
>  my @array = @{$ptrelem};
>  foreach my $item (@array) {
>  print $item;
>  }
> }
>
> From my experience with perl, doing shortcuts don't work too well, and
> doing it the long way, helps with debugging and finding out what the
> problem is.
>
> 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
>>
>>
>



More information about the Linux-il mailing list