willjones has asked for the wisdom of the Perl Monks concerning the following question:
The following code works:
File name: test.pl
#!/usr/bin/perl use strict; use warnings; my $res = { 'objects'=>[ {'a'=>'1', b=>'2'} ] }; my $arr = $res->{objects}; for my $elem (@$arr) { print $elem->{a}."\n"; print $elem->{b}."\n"; }
But this code doesn't work:
File name: test2.pl
#!/usr/bin/perl use strict; use warnings; my $res = { 'objects'=>[ {'a'=>'1', b=>'2'} ] }; for my $elem (@$res->{objects}) { print $elem->{a}."\n"; print $elem->{b}."\n"; }
The error message I get says the following:
Not an ARRAY reference at test2.pl line 7.
How can I do this without making the copy using the intermediate $arr variable?
Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Referring to an array without copying to a separate variable first
by keszler (Priest) on Jun 26, 2013 at 21:58 UTC | |
by LanX (Saint) on Jun 26, 2013 at 22:06 UTC | |
by kcott (Archbishop) on Jun 27, 2013 at 09:33 UTC | |
by LanX (Saint) on Jun 27, 2013 at 10:04 UTC | |
by kcott (Archbishop) on Jun 30, 2013 at 05:07 UTC | |
by Anonymous Monk on Jun 27, 2013 at 00:06 UTC | |
by LanX (Saint) on Jun 27, 2013 at 00:14 UTC | |
|
Re: Referring to an array without copying to a separate variable first
by Anonymous Monk on Jun 27, 2013 at 00:08 UTC |