Ken Foskey schreef:
> Mathew Snyder:

>> Is there an easy way to determine if an item is in an array without
>> iterating through the array and comparing each element to the item
>> in question?
>
> Look up grep.

That still involves iterating and comparing each element, but now done
(faster) by grep.

If the array is already sorted, a binary search would be the way to go.

Otherwise you can use a loop that short-circuits by using "last" if the
item is found.


With smallish arrays, you can even use index(), after stringizing it:

  $string = join $;, ('', @array, '');
  index($string, "$;$item$;") >= 0 and print "found\n";

or use a regular expression:

  $string = join "\n", @array;
  /^$item$/m and print "found\n";


> You might be wanting a hash table not an array.

Yes, that is the best answer. Especially if multiple items need to be
tested.

-- 
Affijn, Ruud

"Gewoon is een tijger."


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


Reply via email to