in reply to Getting element of array with a match

Close, but grep returns a list, not a scalar. Forcing the return value in a scalar, as you do, will get you the number of elements in it, or in your case the number of elements starting with 1006. Also, you might look into split to get to the second field in the element:
print (split /,/,$_)[1] for grep /^1006/, @array; #UNTESTED
As a better solution for this problem, consider using a hash instead of an array to store your data (that is, if the first field in your data is unique). Hashes are built for fast text searches:
use strict; my %hash=qw(1001 choochoo 1002 candycane 1003 sockpuppet); print $hash{'1003'}; __END__ sockpuppet

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: Getting element of array with a match
by broquaint (Abbot) on Dec 23, 2002 at 15:19 UTC
    A variation on the theme of grep
    my @ar = map { chomp; $_ } <DATA>; print +(split /,/, $ar[ grep { /^(\d+)/ and $1 < 1006 } @ar ])[1], $/; __DATA__ 1001,choochoo 1002,candycane 1003,sockpuppet 1004,choochoo 1005,candycane 1006,sockpuppet6 1007,foo 1008,bar
    This of course blindly assumes that you're data is a linear, contiguous set of numbers.
    HTH

    _________
    broquaint

      Thank you everyone for the help and education. I got what I needed and everything works perfectly!

      peppiv