Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^4: trouble parsing log file...

by liverpole (Monsignor)
on Nov 20, 2006 at 22:06 UTC ( #585156=note: print w/replies, xml ) Need Help??

in reply to Re^3: trouble parsing log file...
in thread trouble parsing log file...

You need to look at my example more closely.

With regular expressions, you can't just set a string to $error=(/DOWN/), and then check whether your line matches it with if ($line eq $error) {.

You need to use the regular expression match operator =~ to see whether your string matches the pattern:

if ($line =~ /DOWN/) { # ... } elsif ($line =~ /PROBLEM/) { # ... }

Now, it's true that you can abstract some of this.  For example, you could assign string variables, and then use pattern matching on those variables:

my $error = "DOWN"; my $warn = "PROBLEM"; # ... and later ... if ($line =~ /$error/) { # ... } elsif ($line =~ /$warn/) { # ... }

Which in the above case should work fine.

But be aware that, if your variable contains any characters which have special meaning within regular expressions, those meanings will continue to apply.

For example, if the string you're trying to match is literally "Server ... DOWN", and you attempt to assign and match against that string with:

my $error = "Server ... DOWN"; if ($line =~ /$error/) { # conditional clause }

then you'll be using the special meaning of "." within a regular expression (that of matching any character other than end-of-line), and your conditional will thus also match strings like the hypothetical "Server Not DOWN".


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://585156]
[LanX]: ... and it might be your last chance ... I heard Perl is dead! OO
[marto]: someone read the post about search.cpan being shut down, and commented "It's going to be awhile before I learn to not type "" into my address bar."

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2018-06-20 09:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.