in reply to use simple scalar names to point to specific array elements
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: use simple scalar names to point to specific array elements
by sauoq (Abbot) on Dec 05, 2003 at 20:58 UTC | |
by Ovid (Cardinal) on Dec 06, 2003 at 00:39 UTC | |
by sauoq (Abbot) on Dec 06, 2003 at 01:30 UTC |