searching a list, i always like binary search. of course, the list has to be sorted tough. here is a little script
use strict; use warnings; my @s = ( 'abc', 'dog', 'cat', 'rabbit', 'attic' ); my @sorted_copy = sort { $a cmp $b } @s; print ' at:' . contains_string( 'at', \@sorted_copy ) . "\n"; print 'cat:' . contains_string( 'cat', \@sorted_copy ) . "\n"; sub contains_string { my ( $var, $array ) = @_; return binary_string_search( $var, $array ) > -1; } sub binary_string_search { my ( $var, $array, $lo, $hi ) = @_; $lo = 0 unless defined $lo; $hi = $#{ @{$array} } unless defined $hi; while ( $lo <= $hi ) { # determine new division point my $mid = $lo + int( ( $hi - $lo ) / 2 ); if ( $array->[$mid] eq $var ) { return $mid; } elsif ( $var le $array->[$mid] ) { $hi = $mid - 1; } else { $lo = $mid + 1; } } # nothing found return -1; }
i think you can achieve a speedy solution when searching a big list for more than one item.

UPDATE: minor code changes.

In reply to Re: searching a list by esskar
in thread searching a list by keiusui

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.