Solved

How to grep a value within an array?

Posted on 2011-09-13
6
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Union rows in array that have common elements 2 110
Perl Write to Specific line in a file 15 115
Perl File::Find alternative 1 87
perl syntax 3 36
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…
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…

730 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