Come for the quick hacks, stay for the epiphanies. PerlMonks

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

by xmath (Hermit)
 on Oct 27, 2003 at 06:36 UTC Need Help??

in reply to Re: Re: How's your Perl?

oops, you're right, my definition of "static" in the clarification was flawed :-)

I do mean function-scoped static like in C/C++ ofcourse

btw tachyon, your #1 solution isn't:
perl -le 'my @x = ( \\$_, \\$_ ); \$x[0]=42; print \$x[1]'

Replies are listed 'Best First'.
Re: Re[3]: How's your Perl?
by tachyon (Chancellor) on Oct 27, 2003 at 07:26 UTC

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

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

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://302339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2024-04-15 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found