Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

checking an array of numbers for a particular value

by Anonymous Monk
on Aug 28, 2006 at 16:54 UTC ( [id://570009]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks! your wisdom I do require
I want to check an array of numbers for a particular value, and if found do something.
(no this is not homework)
I am thinking that this is likely a one-liner, but, the solution eludes me.
Thank you for your help.
  • Comment on checking an array of numbers for a particular value

Replies are listed 'Best First'.
Re: checking an array of numbers for a particular value
by Limbic~Region (Chancellor) on Aug 28, 2006 at 16:57 UTC
    Anonymous Monk,
    This is a FAQ. Additionally, I wrote a tutorial to accompany the FAQ - see Getting Matching Items From An Array. On a final note, if your numbers are in order - a binary search may be appropriate. If many searches are going to need to be performed and the data isn't going to change in between searches, consider using a parallel hash for fast lookups.

    Cheers - L~R

Re: checking an array of numbers for a particular value
by swampyankee (Parson) on Aug 28, 2006 at 18:02 UTC

    You could try List::MoreUtils. See the function "any".

    emc

    Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.

    Albert Einstein
Re: checking an array of numbers for a particular value
by wazzuteke (Hermit) on Aug 28, 2006 at 18:02 UTC
    If I understand, something like this could help:
    my @matched = grep{ /[0-9]/ } @original_array;
    Where you can look more into grep as defined in the perl docs for the grep function.

    Hope that helps and good luck!

    ha||ta

      The standard grep caveat applies - you could end up doing more work here than you actually wanted.

      From the OP: I want to check an array of numbers for a particular value, and if found do something.

      Personally, I read this as, "I'm looking for one value to operate upon." But, in general, if you're only looking for a single occurrence of a value, it's better to use a foreach loop (or another explicit loop - while/for/whatever).

      The reason is that the grep doesn't stop. So, for example, if the element you're looking for is the first item in your list, the grep solution will still process the entire list looking for additional instances. If you're using one of the looping constructs, you can perform your operation and last out of it when you find the value you're looking for.

Re: checking an array of numbers for a particular value
by planetscape (Chancellor) on Aug 29, 2006 at 08:31 UTC
Re: checking an array of numbers for a particular value
by TedPride (Priest) on Aug 28, 2006 at 19:59 UTC
    print "Found!" if grep {$_ == $val} @arr;
Re: checking an array of numbers for a particular value
by rsriram (Hermit) on Aug 29, 2006 at 07:42 UTC

    Hi, Try this

    @array=qw/2 3 7 5 34/; $no=13; if(grep {$_ == $no} @array) { print "Condition true"; } else { print "Condition false"; }

    Here I have a array @array, which contains the list of allowed values. $no should be the number you want to check.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://570009]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found