Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

how to select which words have Y and W

by rammohan (Acolyte)
on Dec 26, 2013 at 10:10 UTC ( [id://1068408]=perlquestion: print w/replies, xml ) Need Help??

rammohan has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: how to select which words have Y and W
by LanX (Saint) on Dec 26, 2013 at 10:30 UTC
    Try this

     @selection = grep { /REGEX/ } @array

    and plz give us feedback with code you tried.

    see also grep and perlre

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      i tried like this but it doesn't show any result
      my@colors=('red', 'cyan', 'yellow', 'blue' ); my @yw_colors = grep {/yw/} @colors;

        UPDATE: Add a print statement. And the regex should be /x|y/, i.e. you want to find an x or an y, not x followed by y. Another possible regex that would work would use a character class: /[xy]/

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: how to select which words have Y and W
by Laurent_R (Canon) on Dec 26, 2013 at 12:19 UTC
    For your initial question, if you want words having both y and w, a character class or alternation is not sufficient. Try two regexes:
    $ perl -e 'print join " ", grep { /y/ and /w/} ('red', 'cyan', 'yello +w', 'blue' )' yellow
Re: how to select which words have Y and W
by karlgoethebier (Abbot) on Dec 26, 2013 at 12:42 UTC
    "what is the functionality of [] and |?"
    "I didn't find character class :("
    "let me know"

    rammohan, you got a lot of feedback and good advice. Very cool.

    But can you imagine how all this guys figured out this stuff?

    Let me us know...

    My best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Alternatively, there's beginning Perl training available here.

        Unbelievable. Unfortunately i'm not from India and i really want no Perl training. But perhaps some bros think that i should have one. Please donate (beer accepted). Regards, Karl

        Update:...and why the hell are this gurus interested in my sex and age?

        «The Crux of the Biscuit is the Apostrophe»

Re: how to select which words have Y and W
by Lennotoecom (Pilgrim) on Dec 26, 2013 at 19:34 UTC
    @a = qw/red cyan yellow blue why do you want this at all/; /y/ and /w/ and print "$_\n" for @a;
Re: how to select which words have Y and W
by rammohan (Acolyte) on Dec 26, 2013 at 11:19 UTC
    I didn't find character class :(

      Use http://www.google.com

      You got that? Once there, type in: perl "character class"

      Links galore...

Re: how to select which words have Y and W
by rammohan (Acolyte) on Dec 26, 2013 at 11:08 UTC
    what is the functionality of [] and |?
Re: how to select which words have Y and W
by rammohan (Acolyte) on Dec 26, 2013 at 11:46 UTC
    suppose i have an array like (12,-85,36,-10,75,-38),in this array i've to delete negative elements using grep.is it possible ?let me know
      yes it's possible

      DB<114> @a=(12,-85,36,-10,75,-38) => (12, -85, 36, -10, 75, -38) DB<115> @a = grep { $_ >= 0 } @a => (12, 36, 75)

      But for the basics people here appreciate if you first read docs (or a Perl book) and try the examples there before asking.

      People already provided links (like here) and they would like to get feedback if those links helped (and if not, why, to be able to improve them)

      Thats the way we help to learn efficiently! :-)

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      Try this:
      $ perl -e '@positive_int = grep {$_ >=0 } (12,-85,36,-10,75,-38); pri +nt "@positive_int"' 12 36 75

      Of course it's possible, just place a conditional in the grep.

      $ perl -Mstrict -Mwarnings -E ' my @array = ( 12, -85, 36, -10, 75, -38 ); say qq{@array}; my @notNegatives = grep { $_ >= 0 } @array; say qq{@notNegatives};' 12 -85 36 -10 75 -38 12 36 75 $

      I hope this is helpful.

      Cheers,

      JohnGG

      Yes, grep works with arbitrary expressions. Just remember that every single value in the array is aliased to $_ inside the grep (i.e. $_ is a placeholder for the array value).

      Thanks to all it works for me..its silly point sorry for asking here.. from now on words i'll try my level best..Thank you for advices

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1068408]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found