Syntactic Confectionery Delight PerlMonks

### Re: Re[3]: How's your Perl?

by tachyon (Chancellor)
 on Oct 27, 2003 at 07:26 UTC ( #302348=note: print w/replies, xml ) Need Help??

Read your rules. You don't specify HOW the value of \$x[0] is changed :-) vis Create an array @x such that changing \$x[0] also sets \$x[1] to the same value While assignment is one way to change a value....

```@x=(\\$_,\\$_);
\$_=10;
print "\\$x[\$_] = \${\$x[\$_]}\n" for 0..\$#x;
\$_=20;
print "\\$x[\$_] = \${\$x[\$_]}\n" for 0..\$#x;
__DATA__
\$x[0] = 10
\$x[1] = 10
\$x[0] = 20
\$x[1] = 20

cheers \$[

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"\$'\$`\$\"\$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Re[3]: How's your Perl?
by Juerd (Abbot) on Oct 27, 2003 at 08:11 UTC

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' }

I agree that it was not what was expected but it does fulfill the criteria as stated. The answer was probably supposed to be this as noted elsewhere:

```\$[

The index of the first element in an array, and of the first character
+ in a substring. Default is 0, but you could theoretically set it to
+1 to make Perl behave more like awk (or Fortran) when subscripting an
+d when evaluating the index() and substr() functions. (Mnemonic: [ be
+gins subscripts.)
As of release 5 of Perl, assignment to \$[ is treated as a compiler dir
+ective, and cannot influence the behavior of any other file. --> Its
+use is highly discouraged :-)

\$[ = 1;
\$x[0] = 1;
show();
\$x[0] = 2;
show();
\$x[1] = 3;
show();
sub show { print "\\$x[\$_] = \$x[\$_]\n" for 0..\$#x; }

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"\$'\$`\$\"\$\&"&ee&&y&srve&&d&&print

Yes, the answer was probably supposed to be this as noted elsewhere

As noted in the reply to that elsewhere, it's not the solution we were looking for. So if you're not all to disappointed yet, to achieve real insanity, try to find another solution.

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

I agree that it was not what was expected but it does fulfill the criteria as stated.

No, as juerd pointed out, you're not changing \$x[0] nor \$x[1]: they both remain equal to \\$_. All you show is that if you change \${\$x[0]} then \${\$x[1]} changes, which wasn't the exercise.

Note that I have no problem with unexpected solutions — I love the \$[ one.. didn't think of that — but they do need to be valid.   :-)

Create A New User
Node Status?
node history
Node Type: note [id://302348]
help
Chatterbox?
 [marto]: heh, there's some truth to this

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2017-12-14 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What programming language do you hate the most?

Results (389 votes). Check out past polls.

Notices?