Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re3: IF NOT! IF NOT!

by Hofmator (Curate)
on Aug 03, 2001 at 13:11 UTC ( [id://101921]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: font color="#ff0000"IF NOT! IF NOT!/font
in thread IF NOT! IF NOT!

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

Replies are listed 'Best First'.
Re{4}: IF NOT! IF NOT!
by dragonchild (Archbishop) on Aug 03, 2001 at 17:17 UTC
    Is it that grep returns a number when evaluated in a scalar context or that grep returns a list which, when evaluated in scalar context, gives the number of matches?

    I know it sounds like a semantic difference, but I'm just curious.

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

      says perldoc -f grep

      In scalar context, returns the number of times the expression was true.
      And watch your language ;) a list evaluated in scalar context does not give its length my $len = (3,4,5); # $len == 5 ! only an array gives back its length in scalar context.

      It would be very inefficient to first build up an array with lots of elements and then only use its length. That's why only a number is returned directly.

      -- Hofmator

        I stand corrected. My bad. :)

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

        But isn't ... *pouts*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-03-28 22:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found