in reply to Re: Re[3]: How's your Perl?
in thread How's your Perl?
Create an array @x such that changing $x[0] ...
@x=(\$_,\$_); my $initial_value = $x[0]; $_=10; print "\$x[$_] = ${$x[$_]}\n" for 0..$#x; die "But \$x[0] did not change.\n" if $x[0] eq $initial_value; $_=20; print "\$x[$_] = ${$x[$_]}\n" for 0..$#x; die "But \$x[0] did not change.\n" if $x[0] eq $initial_value;
You're really printing the wrong thing.
print "\$x[$_] = ${$x[$_]}\n" for 0..$#x; ^^ ^ LHS is $foo, RHS is $$foo. The output is very misleading. print "\${\$x[$_]} = ${$x[$_]}\n" for 0..$#x; LHS is $$foo and RHS is $$foo. But we're interested in $foo. print "\$x[$_] = $x[$_]\n" for 0..$#x; LHS is $foo and RHS is $foo. Output is correct and shows that the need +ed change did not happen.
You're changing the variable that is refered to by the references in $x[0] and $x[1], but you're not actually changing the value in the container that is named $x[0].
Clearer would be: "so that assigning to $x[0] also changes ...", but as said, we're against sanity.
Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Re: Re[3]: How's your Perl?
by tachyon (Chancellor) on Oct 27, 2003 at 08:29 UTC | |
by Juerd (Abbot) on Oct 27, 2003 at 08:33 UTC | |
by xmath (Hermit) on Oct 27, 2003 at 10:06 UTC |