Solved

How to grep a value within an array?

Posted on 2011-09-13
6
301 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
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.

 
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

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…
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…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

939 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

4 Experts available now in Live!

Get 1:1 Help Now