The issue is that push @list, $num doesn't set element $list[$num], so the tests don't correlate. If instead the OP used $list[$num] = 1; or equivalent, then [she|he] would get a false positive with exists if [she|he] set $list[5] and then checked $list[3] - defined would give correct results, though.