Jan Eden wrote:
>
> Richard Heintze wrote:
>
> >I have an array of hashes. What function should I be
> >using to interrogate each array cell when I want to
> >know if it is occupied?
> >
> >  "exists" seemed to do the job nicely. What about
> >  "defined"?
> >
>
> exists only works with hashes. Please check the recent conversation about "Array
> containment", where Rob Dixon advised me to turn my array into a hash to use the
> exists function.

Sorry if I misled you Jan, but 'exists' works on both hash and array elements. Look:

  use strict;
  use warnings;

  my @array;

  @array[2, 4] = (1, 1);

  for (0 .. 6) {
    print "Element $_ Exists\n" if exists $array[$_];
  }

**OUTPUT

  Element 2 Exists
  Element 4 Exists


The only reason I suggested that you generate a hash from your array was that,
if the array was invariant and you needed to do several lookups, it would be
faster doing hash accesses than linear searches through the array.

Look at

  perldoc -f exists

for this topic, and make sure you read

  perldoc -q "array contains"

about your previous question.

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to