Re: Is item in array



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.

You might be wanting a hash table not an array.

Ta
Ken

.



Relevant Pages

  • Re: How to sort an array which contains a value several times ?
    ... Sure, we are using a hash table as a way to deal with duplicates, which is a standard idiom in Perl. ... The idea is to use the elements you need to process as keys of a hash table. ... After that code the keys of %counters are the distinct elements of @array, and the hash associates the number of occurrences of each one in @array. ... because we want to iterate over @array and filter duplicates out (you need to know grep to understand this): ...
    (perl.beginners)
  • Re: How to sort an array which contains a value several times ?
    ... Sure, we are using a hash table as a way to deal with duplicates, which is a standard idiom in Perl. ... The idea is to use the elements you need to process as keys of a hash table. ... After that code the keys of %counters are the distinct elements of @array, and the hash associates the number of occurrences of each one in @array. ... In the example I sent before, we used a similar trick but using grep, because we want to iterate over @array and filter duplicates out: ...
    (perl.beginners)
  • Re: Effective perl function to remove one element from array?
    ... >> from an array, and have the array size shrink by one. ... The OP's pseudo-code looks like ... It is probably true that a hash is a better data structure for the ... "grep" seems to be what OP was aiming at. ...
    (comp.lang.perl.misc)
  • Re: In search of elegant code - searching keys of hashes in array
    ... > Obviously I can't just grep the array. ... This way you build the hash from scratch each time you need it. ... > %SSN hash). ...
    (comp.lang.perl.misc)
  • Re: In search of elegant code - searching keys of hashes in array
    ... > Obviously I can't just grep the array. ... but those only tell you if an ssn exists. ... by ssn just replace the 1 with $_ in the second and call the hash ...
    (comp.lang.perl.misc)