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

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}; ... }

Replies are listed 'Best First'.
Re^3: Can an element be created because it was accessed ?
by NetWallah (Canon) on Dec 29, 2011 at 03:46 UTC
    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}; ... }