in reply to searching a list

you can grep but that goes through the whole list:
my $ct = scalar grep { $_ eq 'at' } @s;
Or you can look your self and break when you get a hit:
my $found; foreach ( @s ){ next unless $_ eq 'at'; $found = 1; last; }
I would also suspect that List::Util has something useful..

Yet another option, if you're ok w/more or less doubling the memory and going through the whole list, is to hash it up (maybe useful elsewhere in your code so it's worth it?):
my %h; $h{$_}++ for @s; warn "got " . ($h{at}||0) . " hits for 'at'";

Replies are listed 'Best First'.
Re^2: searching a list
by ikegami (Patriarch) on Jun 27, 2006 at 04:18 UTC

    I would also suspect that List::Util has something useful

    yeah, first.

    use List::Util qw( first ); my $found = defined first { $_ eq 'at' } @s;

    By the way,
    my $ct = scalar ...
    is the same as
    my $ct = ...

      By the way, my $ct = scalar ... is the same as my $ct = ...

      Yeah, true... but I think some people (myself included) adopt the practice (in these sorts of situations) of using scalar anyway, just so as to remove any doubt about the intention. Personally, I think it's not a bad practice, and I'm not aware of any performance hit - or is there? :)