Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

IF NOT! IF NOT!

by Anonymous Monk
on Aug 02, 2001 at 22:45 UTC ( #101741=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm writing this script and I'm stuck.

I need an if statement to say something like "if grep can't find 'home' in a word, print the word."

I started out with:
if (!grep /home/, $words[$x]) { print $words[$x]; }
This takes place in a loop where at the end $x is increased by one.
If some one could help me I would be so grateful.

Thanks

Edit: chipmunk 2001-08-02

Comment on IF NOT! IF NOT!
Download Code
Replies are listed 'Best First'.
Re: IF NOT! IF NOT!
by abstracts (Hermit) on Aug 03, 2001 at 00:25 UTC
    Hello,

    I think this is what you wanted to do:

    my @list = qw/hello hunn I am home homealone myhome myhomealone/; print "$_\n" for grep {$_ !~ /home/} @list; # hello # hunn # I # am
    Hope this helps,,,

    Aziz,,,

      A bit excessive, this will work just fine.

      grep{$_!~/home/&&print"$_\n"} @list;
      Which results the same as yours.

      UPDATE: Changed map to grep so there would be no void context stuff, thanks runrig.

      $_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.
        Hello

        You can go even with

        /home/||print"$_\n"for@list
        But this is not golf :-)

        Aziz,,,

        And that is using map in a void context (creating an array with map and throwing it away). Better to just use a for loop:
        /home/ and print "$_\n" for @list;
        Update: Damian's right (in reply below). reverse the logic:
        /home/ or print "$_\n" for @list;
Re: font color="#ff0000"IF NOT! IF NOT!/font
by twerq (Deacon) on Aug 02, 2001 at 22:48 UTC

      ...except that you got the logic backwards.

      I need an if statement to say something like "if grep can't find 'home' in a word, print the word."
      foreach (@words) { print "$_\n" unless /home/; }
Re: font color="#ff0000"IF NOT! IF NOT!/font
by suaveant (Parson) on Aug 02, 2001 at 22:51 UTC
    Grep returns a list, so I'm not sure what the ! will do... you probably either want
    unless(grep /home/, @words) { print $words[$x]; }
    Your problem is you need to pass grep an array @words, not part of the array $words[$x]... if $words[$x] contains a sub array you need to dereference it like... @{$words[$x]}

    It would help if you said a little more about what the data is

    Update Nevermind... from other people responses I see what is going on... yeah, you probably want

    print $words[$x] if $words[$x] =~ /home/;
    oops :)

                    - Ant

      As an aside, the construct ! grep EXPR, LIST is used in a boolean context. Either grep returns a list or it doesn't. ! evaluates in a boolean context. A list is TRUE and no list is FALSE.

      I've used it a lot in extended EXPR's, so that I don't have to work through the negation of my && or || that sending if to unless (or vice-versa) would do.

      --------
      /me wants to be the brightest bulb in the chandelier!

        As an aside, the construct ! grep EXPR, LIST is used in a boolean context. Either grep returns a list or it doesn't. ! evaluates in a boolean context. A list is TRUE and no list is FALSE.
        Not the best way to put it, IMHO ... to clarify: in scalar context grep returns the number of times it matched. So if it didn't match anything, it returns 0 which evaluates to false. Otherwise it returns a positive number which evaluates to true.

        The boolean context is enforced by the if (and by the !), so that the grep in if (grep /blah/, @array)
        also evaluates in boolean context.

        -- Hofmator

        Ahhh... I wasn't sure... the code had me a bit confused anyway... backwards logic does that :)

                        - Ant

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2016-02-10 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?





    Results (343 votes), past polls