Don't wield symbolic references for this purpose. You don't need them here. Use strict to disabuse yourself of any temptation. Why it's stupid to use a variable as a variable name.
In your code above, ${'a'} is a symbolic reference to a package global named $main::a. And ${a} is synonymous with $a, which in the scope you're using it in, refers to the lexical version of $a, not the package global.
Instead, either use real references, or a hash:
# Using hard references my @cat = (1,2,3); my @dog = (5,6); my @pig = (4,3,2,1); for my $array (\@cat, \@dog, \@pig) { # On first iteration $array will contain a reference to @cat. # On second iteration, $array will contain a reference to @dog. # On third iteration $array will contain a reference to @pig. }
...or do this...
# Using a hash my %animals = ( cat => [1,2,3], dog => [5,6], pig => [4,3,2,1], ); for my $animal_type (keys %animals) { my @values = @{$animals{$animal_type}}; # Animals will come in an unpredictable order, but as an example: # First iteration, dog will be handled. # Second iteration, pig will be handled. # Third iteration, cat will be handled. }
Dave
In reply to Re: Referencing the locals
by davido
in thread Referencing the locals
by Chuma
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |