Solved

How to grep a value within an array?

Posted on 2011-09-13
6
294 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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 video discusses moving either the default database or any database to a new volume.

746 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

11 Experts available now in Live!

Get 1:1 Help Now