in reply to how to select which words have Y and W

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
  • Comment on Re: how to select which words have Y and W

Replies are listed 'Best First'.
Re^2: how to select which words have Y and W
by LanX (Saint) on Dec 26, 2013 at 12:15 UTC
    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)

Re^2: how to select which words have Y and W
by Laurent_R (Canon) on Dec 26, 2013 at 12:12 UTC
    Try this:
    $ perl -e '@positive_int = grep {$_ >=0 } (12,-85,36,-10,75,-38); pri +nt "@positive_int"' 12 36 75
Re^2: how to select which words have Y and W
by johngg (Canon) on Dec 26, 2013 at 12:13 UTC

    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

Re^2: how to select which words have Y and W
by jethro (Monsignor) on Dec 26, 2013 at 12:17 UTC

    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).

Re^2: how to select which words have Y and W
by rammohan (Acolyte) on Dec 26, 2013 at 12:31 UTC
    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