If you want named elements, use a hash instead.
my %hash = ( myvar => 3, name => 'Ovid', ); print $hash{name};
If the order of elements is important, you could use Tie::Hash::Indexed or something similar.
In any event, I would be careful about wanting to alias one variable to another. This can result in "action at a distance" problems and those can be hard to debug.
Finally, if you always know which index you wanted to access by name, you could try a constant:
use constant NAME => 2; print $array[NAME];
However, if you really, really want to be evil and alias a variable to an array element, then you can do the following. How this works is an exercise left to the reader :)
#!/usr/bin/perl use warnings; use strict; my @array = qw( foo bar baz ); my $var = alias_to(\@array, 1); print $var; sub alias_to { $_[0]->[$_[1]] }
Cheers,
Ovid
New address of my CGI Course.
In reply to Re: use simple scalar names to point to specific array elements
by Ovid
in thread use simple scalar names to point to specific array elements
by Sandy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |