in reply to Can an element be created because it was accessed ?

You are 'auto-vivifying' the array element, because you are trying to get something beyond it.

You can fix this by doing

$testedNVal = exists ( $this->{N}[$1]) ? $this->{N}[$1]{Val} : undef +;

            "Battle not with trolls, lest ye become a troll; and if you gaze into the Internet, the Internet gazes also into you."
        -Friedrich Nietzsche: A Dynamic Translation

Replies are listed 'Best First'.
Re^2: Can an element be created because it was accessed ?
by ikegami (Patriarch) on Dec 29, 2011 at 02:10 UTC

    exists( $a[$i] ) doesn't return whether an element exists in an array or not as its name implies, and it's deprecated for that very reason.

    Use $i < @a instead.

    if ( $1 < @{ $this->{N} }) { my $testedNVal = $this->{N}[$1]{Val}; ... }
      That wont work if the array is intentionally sparse.

      Depending on how the O.P enters data into the array, "defined" may be a reasonable choice.

                  "Battle not with trolls, lest ye become a troll; and if you gaze into the Internet, the Internet gazes also into you."
              -Friedrich Nietzsche: A Dynamic Translation

        That wont work if the array is intentionally sparse.

        This is not a supported "feature". It's the side effect of a bug in exists.

        But my fix was wrong. (I've been fixing uses of exists to check if an element exists.) It should be

        if ( defined( $this->{N}[$1] ) ) { my $testedNVal = $this->{N}[$1]{Val}; ... }

        And in all likelyhood, the following would also do the trick:

        if ( $this->{N}[$1] ) { my $testedNVal = $this->{N}[$1]{Val}; ... }