On Thu, Apr 25, 2002 at 01:54:53PM +0100, [EMAIL PROTECTED] wrote:
> Hi
> 
> anyone have any ideas on how example 2 or 3 below should correctly be
> written ???

Actually your code can be interpreted in different ways.  I guess you'll
need the following:

    ---------- perldoc -f grep ----------
    grep BLOCK LIST
    grep EXPR,LIST
           This is similar in spirit to, but not the same as,
           grep(1) and its relatives.  In particular, it is
           not limited to using regular expressions.

           Evaluates the BLOCK or EXPR for each element of
           LIST (locally setting $_ to each element) and
           returns the list value consisting of those ele-
           ments for which the expression evaluated to true.
           In scalar context, returns the number of times the
           expression was true.
    ---------- perldoc -f grep ----------

> if(grep /Handicap|Help/, $source) {
> this seems to work

Then we'll leave it alone :-)

I will however nag about using grep to match on a single string.  grep
should be used to filter lists of strings.  For the single string, just
use a normal regexp.

> if(grep /(Handicap && Help)/, $source) {
> this does not work

Hmm, I suppose you want only lines that contain both words regardless of
their order?

    if (grep { /Handicap/ && /Help/ } @sources) { ... }

Should do what you want.

> ideally I would like multiple grep pairs ie..
> if(grep /(Handicap && Help) || (Friend && Foe)/, $source) {
> this does not work

Same as above.  The BLOCK can be as complex as you wish.

    if (grep {
            (/Handicap/ && /Help/)
                    ||
            (/Friend/ && /Foe/)
        } @sources) { ... }

See 'perldoc -f sort' for similar uses of the BLOCK style of filter
functions.

-- 
                       If we fail, we will lose the war.

Michael Lamertz                        |      +49 221 445420 / +49 171 6900 310
Nordstr. 49                            |                       [EMAIL PROTECTED]
50733 Cologne                          |                 http://www.lamertz.net
Germany                                |               http://www.perl-ronin.de 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to