retrieving data from array changes it???

Discussion in 'Perl' started by thoin, Mar 1, 2004.

  1. thoin

    thoin Guest

    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
    thoin, Mar 1, 2004
    #1
    1. Advertising

  2. thoin wrote:
    > 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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 1, 2004
    #2
    1. Advertising

  3. thoin

    thoin Guest

    Gunnar Hjalmarsson <> wrote in message news:<mKK0c.50876$>...
    > thoin wrote:
    > > 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 :)

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


    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
    thoin, Mar 2, 2004
    #3
  4. thoin

    Jim Gibson Guest

    In article <>, thoin
    <> wrote:

    > Gunnar Hjalmarsson <> wrote in message
    > news:<mKK0c.50876$>...
    > > thoin wrote:
    > > > 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 :)
    >
    > > > 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

    >
    > 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.
    Jim Gibson, Mar 2, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jane Austine
    Replies:
    14
    Views:
    769
    Dennis Lee Bieber
    Oct 9, 2004
  2. Jane Austine
    Replies:
    2
    Views:
    445
    Changjune Kim
    Oct 5, 2004
  3. Vince C.
    Replies:
    3
    Views:
    557
    Vince C.
    Jul 16, 2007
  4. Chris Coho, Jr

    reload SQL Data when Data Changes

    Chris Coho, Jr, Jul 14, 2003, in forum: ASP General
    Replies:
    0
    Views:
    111
    Chris Coho, Jr
    Jul 14, 2003
  5. Simon Harrison
    Replies:
    10
    Views:
    247
    Simon Harrison
    Jan 15, 2011
Loading...

Share This Page