org chen wrote:
Hi,
Hello,
I am coding a script to read a pure txt fiel and show it. I can't
understand why any lines in the file match the word of "[DIED]".
Thanks
Org
------------------------------------------------
the txt file is like this:
Fri Feb 13 14:31:26 2009
[PROCESS]: Welcome to our system!
[ERROR]: Please check step 1!
[SUCCESS]: all done! enjoy the data!
---------------------------------------
My code is: ...
while(my $line = <>){
chomp( $line );
print "<p>current line is $line";
if($line =~ /^[Mon|Tue|Wed|Thu|Wed|Fri|Sat|Sun]/){
The character class [Mon|Tue|Wed|Thu|Wed|Fri|Sat|Sun] matches *one*
character from the list of characters inside [ and ]. Since the order
of characters and duplicate characters are ignored that could be
simplified to [MTWFSadehinortu|]. Although what you *really* want is to
use alternation instead:
if($line =~ /^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)/){
That will match *either* the string 'Mon' *or* the string 'Tue' *or* the
string 'Wed' *or* the string 'Thu' *or* the string 'Fri' *or* the string
'Sat' *or* the string 'Sun'.
#push old data into array
print "<p>time: $line";
}
elsif($line = ~ /\[DIED\]/){
You have *two* operators, = and ~, where you should have one operator
=~. Since ~/\[DIED\]/ will always be true, that value is assigned to
$line and printed out.
print "<p>DIED: $line ";
}
elsif($line =~ /\[SUCCESS\]/){
print "<p> success:$line";
}
elsif($line =~ /\[PROCESS\]/){
print "<p>process: $line";
}
}
-------------------------------------------
the result is:
Current line is Fri Feb 13 14:31:26 2009
time: Fri Feb 13 14:31:26 2009
current line is [PROCESS]: Welcome to our system!
DIED: 18446744073709551615
current line is [ERROR]: Please check step 1!
DIED: 18446744073709551615
current line is [SUCCESS]: all done! enjoy the data!
DIED: 18446744073709551615
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/