Solved

How to grep a value within an array?

Posted on 2011-09-13
6
311 Views
Last Modified: 2012-05-12
Hi,
I have an array. @Options. I would like to grep a keyword (let's say it is KING) within this array and return true or false.

Can I do it this way?

my $result = grep {/KING/} @Options;

Open in new window


Thanks,
0
Comment
Question by:Tolgar
  • 3
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
sakman earned 500 total points
ID: 36530486
Using the scalar $result will return the number of matches.  

No matches $result will = 0, if 1 match $result will = 1, etc.

Remember if you have KING and KING2 in @Options you will get 2 matches.


0
 
LVL 9

Expert Comment

by:parparov
ID: 36530549
You may also use a CPAN module List::MoreUtils:

use List::MoreUtils qw(any);
my $true_or_false = any { /PATTERN/ } @Options;

Open in new window

This is truly boolean and somewhat quicker than grep since it doesn't scan all of the list but waits till first match.
0
 

Author Comment

by:Tolgar
ID: 36530596
What about the way I propose? Do you think it works fine?

my $result = grep {/KING/} @Options;

Open in new window



Thanks,
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 4

Expert Comment

by:sakman
ID: 36530679
Yes, $result will be greater than 0 if KING is in @Options.  You won't get a true or false - you would need a module like parparov suggested - but you would get a count.
0
 

Author Comment

by:Tolgar
ID: 36530711
ok So if I use it in a if condition this will work right?

 my $result = grep {/KING/} @Options;
if ($result) {

do something

}

Open in new window



Is that right?

Thanks,
0
 
LVL 4

Expert Comment

by:sakman
ID: 36530730
Yes.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

806 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