CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
how to select which words have Y and W

 



Ram.bm
Novice

Dec 26, 2013, 1:54 AM

Post #1 of 5 (1201 views)
how to select which words have Y and W Can't Post

This is my ('red', 'cyan', 'yellow', 'blue' ) array.In this i want t o select which words have Y and W using grep.Please let me know


Laurent_R
Veteran / Moderator

Dec 26, 2013, 4:02 AM

Post #2 of 5 (1197 views)
Re: [Ram.bm] how to select which words have Y and W [In reply to] Can't Post

Hi,

this is a Perl one-liner doing what you want:


Code
$ perl -e 'print join " ", grep { /y/ and /w/}  ('red', 'cyan', 'yellow', 'blue' )' 
yellow

The join is there to insert a space between the results, it may seem useless here because there is only one result, so it could be written simply:


Code
$ perl -e 'print grep { /y/ and /w/}  ('red', 'cyan', 'yellow', 'blue' )' 
yellow


But if you want the words that have 'y' or 'w', it is necessary to separate the two results:


Code
$ perl -e 'print grep { /y/ or /w/}  ('red', 'cyan', 'yellow', 'blue' )' 
cyan yellow


If you want to store the results into an array, you can do this:


Code
$ perl -e 'my @result = grep { /y/ or /w/}  ('red', 'cyan', 'yellow', 'blue' ); print "@result"' 
cyan yellow



(This post was edited by Laurent_R on Dec 26, 2013, 4:04 AM)


BillKSmith
Veteran

Dec 26, 2013, 6:36 AM

Post #3 of 5 (1190 views)
Re: [Laurent_R] how to select which words have Y and W [In reply to] Can't Post

We should note that your one-liners are quoted for *nix. I do not have access to such a system, but I doubt that the conflicting use of single quotes would be resolved correctly. I would prefer:

Code
$ perl -e 'print join " ", grep { /y/ and /w/}  qw(red cyan yellow blue)' 
yellow


Or for windows:

Code
$ perl -e "print join ' ', grep { /y/ and /w/}  qw(red cyan yellow blue)" 
yellow

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Dec 26, 2013, 11:07 AM

Post #4 of 5 (1184 views)
Re: [BillKSmith] how to select which words have Y and W [In reply to] Can't Post

Hi Bill,

I just tried under Windows, it works:


Code
C:\Users\Laurent>perl -e "print join ' ', grep { /y/ and /w/}  ('red', 'cyan', 'yellow', 'blue' )" 
yellow


But I agree that using qw// is better in such a case. Actually, to completely remove the quoting problems between various systems, it could be improved to:


Code
perl -e "print join qq/ /, grep { /y/ and /w/}  qw(red cyan yellow blue)"


But the aim was really to show how to use grep to filter the elements.

BTW, the question has been cross-posted on Perl Monks.


sh.ajay12
User

Jan 2, 2014, 9:17 AM

Post #5 of 5 (953 views)
Re: [Laurent_R] how to select which words have Y and W [In reply to] Can't Post

Thanks Laurent.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives