in reply to Check if value exists into an array in a hash

If I understand what you're doing, I think, yes, it will do what you want.

exists($list[$num])

Will tell if there is an item of some kind, possibly undefined, at index $num in list $list.

I think $#{$arrayref} is what you're looking for with the 2nd question. EG $#{$hash_of_lists{$foo}}.

Replies are listed 'Best First'.
Re^2: Check if value exists into an array in a hash
by kennethk (Abbot) on Feb 10, 2009 at 20:09 UTC
    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.

      Oy, yes. I see what you mean. The array will grow to accomodate it's highest index, and therefore all lower indexes will come into existence.

      I think the OP should use a hash. Clearly, it's a hash-ish behavior he/she wants. The fact that the keys are numbers is making it look like a list problem, but really, it's a hash problem.