Solved

perl, check if data exists in array

Posted on 2010-11-17
3
540 Views
Last Modified: 2012-06-21
# DEFINE AN ARRAY
@coins = ("Quarter","Dime","Nickel");
my $money = "Dime";

How do I check if Dime is one of the value in the array?  Would be nice if i don't have to loop through it.
0
Comment
Question by:rgbcof
3 Comments
 
LVL 7

Accepted Solution

by:
whosbetterthanme earned 125 total points
ID: 34159681
Simple:

@coins = ("Quarter","Dime","Nickel");
my $money = "Dimes";

if (grep(/$money/,@coins)) {
print "Yep, it's there\n";
}
0
 

Author Closing Comment

by:rgbcof
ID: 34159727
That works.  
0
 
LVL 84

Expert Comment

by:ozo
ID: 34161953
perldoc -q "How can I tell whether a certain element is contained in a list or array"
Found in pods/perlfaq4.pod
       How can I tell whether a certain element is contained in a list or
       array?

       (portions of this answer contributed by Anno Siegel)

       Hearing the word "in" is an indication that you probably should have
       used a hash, not a list or array, to store your data.  Hashes are
       designed to answer this question quickly and efficiently.  Arrays
       aren't.

       That being said, there are several ways to approach this.  If you are
       going to make this query many times over arbitrary string values, the
       fastest way is probably to invert the original array and maintain a
       going to make this query many times over arbitrary string values, the
       fastest way is probably to invert the original array and maintain a
       hash whose keys are the first array's values.

               @blues = qw/azure cerulean teal turquoise lapis-lazuli/;
               %is_blue = ();
               for (@blues) { $is_blue{$_} = 1 }

       Now you can check whether $is_blue{$some_color}.  It might have been a
       good idea to keep the blues all in a hash in the first place.

       If the values are all small integers, you could use a simple indexed
       array.  This kind of an array will take up less space:

               @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31);
               @is_tiny_prime = ();
               for (@primes) { $is_tiny_prime[$_] = 1 }
               # or simply  @istiny_prime[@primes] = (1) x @primes;

       Now you check whether $is_tiny_prime[$some_number].

       If the values in question are integers instead of strings, you can save
       quite a lot of space by using bit strings instead:

               @articles = ( 1..10, 150..2000, 2017 );
               undef $read;
               for (@articles) { vec($read,$_,1) = 1 }

       Now check whether "vec($read,$n,1)" is true for some $n.

       These methods guarantee fast individual tests but require a re-
       organization of the original list or array.  They only pay off if you
       have to test multiple values against the same array.

       If you are testing only once, the standard module "List::Util" exports
       the function "first" for this purpose.  It works by stopping once it
       finds the element. It's written in C for speed, and its Perl equivalent
       looks like this subroutine:

               sub first (&@) {
                       my $code = shift;
                       foreach (@_) {
                               return $_ if &{$code}();
                       }
                       undef;
               }

       If speed is of little concern, the common idiom uses grep in scalar
       context (which returns the number of items that passed its condition)
       to traverse the entire list. This does have the benefit of telling you
       how many matches it found, though.

               my $is_there = grep $_ eq $whatever, @array;

       If you want to actually extract the matching elements, simply use grep
       in list context.

               my @matches = grep $_ eq $whatever, @array
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now