Re: Is item in array
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 31 Dec 2006 04:22:57 -0800
Ken Foskey wrote:
On Sun, 2006-12-31 at 00:54 -0500, Mathew Snyder wrote:
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.
Gah. Sure, please look it up, and read enough to realize it is very
much the *wrong* tool for this job.
To the OP: please read the FAQ *before* posting:
perldoc -q contained
Found in /software/perl-5.8.5-0/pkg/lib/5.8.5/pod/perlfaq4.pod
How can I tell whether a certain element is contained in a
list or array?
<snip>
Please do not use
($is_there) = grep $_ eq $whatever, @array;
or worse yet
($is_there) = grep /$whatever/, @array;
These are slow (checks every element even if the first
matches), inefficient (same reason), and potentially buggy
(what if there are regex characters in $whatever?).
<snip>
You might be wanting a hash table not an array.
That, however, is excellent advice.
Paul Lalli
.
- References:
- Is item in array
- From: Mathew Snyder
- Re: Is item in array
- From: Ken Foskey
- Is item in array
- Prev by Date: Re: What's being thrown away?
- Next by Date: Re: Here document as method parameter
- Previous by thread: Re: Is item in array
- Next by thread: Re: Is item in array
- Index(es):
Relevant Pages
|