retrieving data from array changes it???

T

thoin

Hi all,

just stumbeled over a strange behaviour of perl: When trying to
retrieve a non-existing value from a perl array, e.g. by saying

my $a = [1,2];
my $b = $$a[3][0];

then $a is modified to [1,2,[]].

Is this a feature I just don't understand or is it really a bug (as I
would suppose)?

Thanx in advance,

thoin
 
G

Gunnar Hjalmarsson

thoin said:
just stumbeled over a strange behaviour of perl: When trying to
retrieve a non-existing value from a perl array, e.g. by saying

my $a = [1,2];
my $b = $$a[3][0];

then $a is modified to [1,2,[]].

Suppose you mean [1,2,undef,[]].
Is this a feature I just don't understand or is it really a bug (as
I would suppose)?

I think it's called autovivification. One place where it is commented
on is

perldoc -f exists
 
T

thoin

Gunnar Hjalmarsson said:
thoin said:
just stumbeled over a strange behaviour of perl: When trying to
retrieve a non-existing value from a perl array, e.g. by saying

my $a = [1,2];
my $b = $$a[3][0];

then $a is modified to [1,2,[]].

Suppose you mean [1,2,undef,[]].

Yes, of course I do mean that :)
I think it's called autovivification. One place where it is commented
on is

perldoc -f exists

Phew, nice to see that it will be fixed in the future. I mean, nice to
see that I was right supposing this behaviour to be kind of wrong.

Thanks a lot for your hint!

Thoin
 
J

Jim Gibson

thoin said:
Gunnar Hjalmarsson said:
thoin said:
just stumbeled over a strange behaviour of perl: When trying to
retrieve a non-existing value from a perl array, e.g. by saying

my $a = [1,2];
my $b = $$a[3][0];

then $a is modified to [1,2,[]].

Suppose you mean [1,2,undef,[]].

Yes, of course I do mean that :)
I think it's called autovivification. One place where it is commented
on is

perldoc -f exists

Phew, nice to see that it will be fixed in the future. I mean, nice to
see that I was right supposing this behaviour to be kind of wrong.

Thanks a lot for your hint!

Thoin

The only thing "perldoc -f exists" says might be fixed in the future is
that "exists $$a[3][0]" may not create $$a[3] if it doesn't already
exist. The statement "$b = $$a[3][0]" will definitely create $$a[3] if
it doesn't exist now and forever more in the future.

FYI: this newsgroup is defunct. Please try comp.lang.perl.misc in the
future.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top