Solved

How to grep a value within an array?

Posted on 2011-09-13
6
319 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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…

840 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