develooper Front page | perl.beginners | Postings from April 2002

Thanks - Re: multiple greps in if()

From:
Steven_Massey
Date:
April 25, 2002 12:40
Subject:
Thanks - Re: multiple greps in if()
Message ID:
OFE23EA0F8.12B650D6-ON80256BC4.006BDECA@amdahl.com

Excellent
thanks felix and michael.





Felix Geerinckx <felix_geerinckx@hotmail.com> on 04/25/2002 02:12:17 PM

To:   beginners@perl.org
cc:

Subject:  Re: multiple greps in if()


on Thu, 25 Apr 2002 12:54:53 GMT, Steven_Massey@notes.amdahl.com
(Steven Massey) wrote:

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

Put the '&&' outside of the pattern:

    #! perl -w
    use strict;

    my @strings = (
        'Handicap and Help',
        'Friend and Foe',
        'Handicap and Friend',
        'Help and Foe'
    );


    for my $s (@strings) {
        if (grep { (/Handicap/ && /Help/) ||
                   (/Friend/ && /Foe/)       } $s ) {
            print "'$s' passed the test\n";
        }
    }

--
felix

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org








nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About