retrieving data from array changes it???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thoin

    retrieving data from array changes it???

    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
  • Gunnar Hjalmarsson

    #2
    Re: retrieving data from array changes it???

    thoin wrote:[color=blue]
    > 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,[]].[/color]

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

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

    perldoc -f exists

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    Comment

    • thoin

      #3
      Re: retrieving data from array changes it???

      Gunnar Hjalmarsson <noreply@gunnar .cc> wrote in message news:<mKK0c.508 76$mU6.206010@n ewsb.telia.net> ...[color=blue]
      > thoin wrote:[color=green]
      > > 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,[]].[/color]
      >
      > Suppose you mean [1,2,undef,[]].[/color]

      Yes, of course I do mean that :-)
      [color=blue][color=green]
      > > Is this a feature I just don't understand or is it really a bug (as
      > > I would suppose)?[/color]
      >
      > I think it's called autovivificatio n. One place where it is commented
      > on is
      >
      > perldoc -f exists[/color]

      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

      Comment

      • Jim Gibson

        #4
        Re: retrieving data from array changes it???

        In article <943ec034.04030 12210.4826376e@ posting.google. com>, thoin
        <carstene@web.d e> wrote:
        [color=blue]
        > Gunnar Hjalmarsson <noreply@gunnar .cc> wrote in message
        > news:<mKK0c.508 76$mU6.206010@n ewsb.telia.net> ...[color=green]
        > > thoin wrote:[color=darkred]
        > > > 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,[]].[/color]
        > >
        > > Suppose you mean [1,2,undef,[]].[/color]
        >
        > Yes, of course I do mean that :-)
        >[color=green][color=darkred]
        > > > Is this a feature I just don't understand or is it really a bug (as
        > > > I would suppose)?[/color]
        > >
        > > I think it's called autovivificatio n. One place where it is commented
        > > on is
        > >
        > > perldoc -f exists[/color]
        >
        > 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[/color]

        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.

        Comment

        Working...