in reply to using input as array name
Like I mentioned above, the natural way to do this is usually a hash of arrays (or HoA). This is actually a hash of array references. See perlreftut, perllol, perlref, and/or perldsc. Your code might look like:
#!/usr/bin/perl use strict; use warnings; my %hoa = (names => ['joe', 'bob', 'sally'] ); print join ", ", @{$hoa{names}};
If you actually have a good reason to do what you say, you still need to consider the following from Symbolic references:
Only package variables (globals, even if localized) are visible to symbolic references. Lexical variables (declared with my()) aren't in a symbol table, and thus are invisible to this mechanism.
This means you cannot declare your @names array with my, but either must declare it with an explicit package or initialize it without strict in place. Something like:
#!/usr/bin/perl use strict; my $array_name = 'names'; @main::names = ('joe', 'bob', 'sally'); DISABLE_BLOCK: { # localizes disabling strict no strict 'refs'; print join ", ", @{$array_name}; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: using input as array name
by aquarium (Curate) on Oct 14, 2010 at 05:31 UTC | |
by calmthestorm (Acolyte) on Oct 14, 2010 at 21:55 UTC | |
by aquarium (Curate) on Oct 15, 2010 at 05:19 UTC |