AndyB216 has asked for the wisdom of the Perl Monks concerning the following question:
Is there a quick way to reference the array given the string? (Assume there are multiple arrays and the sting can contain the name of any of them...) I can fairly easlily do an 'if' check and then utilize a reference to the array based on a match, but it seems like there should be a more direct way of accessing the array. The following works just fine, but isn't very elegant:@list1 = ("one","two","three","four"); @list2 = ("four","three","two","one"); $name = "list1";
@list1 = ("one","two","three","four"); @list2 = ("four","three","two","one"); $name = "list1"; if ($name eq "list1") { $list_ref = \@list1; } elsif ($name eq "list2") { $list_ref = \@list2; } foreach $item (@{$list_ref}) { print "$item\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array name contained within a string
by Zaxo (Archbishop) on Jun 20, 2007 at 15:35 UTC | |
|
Re: Array name contained within a string
by ForgotPasswordAgain (Vicar) on Jun 20, 2007 at 15:29 UTC | |
|
Re: Array name contained within a string
by FunkyMonk (Bishop) on Jun 20, 2007 at 16:25 UTC |