in reply to use simple scalar names to point to specific array elements
You're on the right track. You can make a reference to a specific array element, and changes you make through the reference will be to the array element, but you have to do it by de-referencing the reference. In your example above, $myothervar just ends up with an independent copy of what's in $array[0].
my @a = qw(a b c); my $var = \$a[1]; my $othervar = $$var; $othervar = 'x'; $$var = 7; print join ' ', $othervar, $$var, @a, "\n"; $a[1] = 'q'; print join ' ', $othervar, $$var, @a, "\n"
prints
x 7 a 7 c x q a q c
And all this is fine under strict. It would also be possible with symbolic references, but that would fail under strict refs.
But why do you want to do such a thing? It only seems to be begging for confusion and code that's hard to read and maintain.
Updated: Heh. Rereading the original post and the replies, now I see why you wanted to do such a thing. ++Ovid for seeing beyond your literal question and giving an answer relevant to what you really wanted.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: use simple scalar names to point to specific array elements
by Sandy (Curate) on Dec 05, 2003 at 21:09 UTC | |
by diotalevi (Canon) on Dec 05, 2003 at 21:14 UTC | |
by Sandy (Curate) on Dec 05, 2003 at 21:24 UTC |