qsl has asked for the wisdom of the Perl Monks concerning the following question:

Great Monks,
I have a file with contents like,
INFO Lindered... DEBUG Using ThreadLocal: false ID Run Timestamp Entry Attempt Message Type Messag +e 13 251 2006-07-06 13:21:14.0 1 1 INFO Starti +ng for (ActivityId=13, DataSource=dunkle-rs) 13 251 2006-07-06 13:21:14.0 2 1 INFO Queryi +ng remote datasource ...
Here i want to check,if all the id and run values are 13 and 251 in the lines.For that i want to take the line if it not starts with INFO/DEBUG/ID/<blankspace>.
So I used the regular expression like this,
open fhand,temp; while(<fhand>){ if(($_ !~ /^INFO/) ||($_ !~ /^DEBUG/)||($_ !~ /^ID/)){ print"Line check"; }
But if i give only one condition,it is working fine for me.How can i check more than one condition here.
Thanks.

Replies are listed 'Best First'.
Re: Regular Expression matching more than one condition
by davorg (Chancellor) on Jul 06, 2006 at 15:16 UTC

    Think about your boolean logic. What you are saying here is:

    Line doesn't start with INFO
    OR Line doesn't start with DEBUG
    OR Line doesn't start with ID

    Does that sounds right to you? Would it make more sense if those "OR"s were all changed to "AND"s?

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Regular Expression matching more than one condition
by prasadbabu (Prior) on Jul 06, 2006 at 15:24 UTC

    i want to take the line if it not starts with INFO/DEBUG/ID/<blankspace>.

    Here is one way to do it.

    while(<DATA>){ if(($_ !~ /^(INFO|DEBUG|ID|\s+)/)){ print"Line check\n"; } } outputs: -------- Line check Line check __DATA__ ID Run Timestamp Entry Attempt Message Type Messag +e 13 251 2006-07-06 13:21:14.0 1 1 INFO Starti +ng for (ActivityId=13, DataSource=dunkle-rs) 13 251 2006-07-06 13:21:14.0 2 1 INFO Queryi +ng remote datasource ...

    Prasad

Re: Regular Expression matching more than one condition
by cowboy (Friar) on Jul 06, 2006 at 15:19 UTC
    You're going about it backwards. In the example you posted, it's much easier to test if the line contains what you want, rather than filtering out everything else.
    if ($_ =~ /^13\s+251/) { print $_; }
Re: Regular Expression matching more than one condition
by Ieronim (Friar) on Jul 06, 2006 at 17:25 UTC
    Here i want to check,if all the id and run values are 13 and 251 in the lines.
    So you need to extract all lines starting on two digits and check if these digits are 13 and 251, respectively :)
    while (<fhand>) { next unless /^(\d+)\s+(\d+)/; print "Bad line $. !\n" if ($1 != 13 || $2 != 251); }