Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: matching patterns

by Zaxo (Archbishop)
on Apr 11, 2006 at 07:18 UTC ( [id://542469]=note: print w/replies, xml ) Need Help??


in reply to matching patterns

Your regex is complicated and likely to be buggy. The use of greedy .* may blow up in your face.

What is @line, and what is its relation to $line?

I'd get at the match variable problem like this:

if ($line[0] =~ /^c/) { # . . . my $unit = ($line =~ /\bunit=(\d*)/) ? $1 : ''; my $port = ($line =~ /\bport=(\d*)/) ? $1 : ''; # . . . }
With that, the $unit and $port variables can only be assigned from $1 if the match succeeded and the value fresh. If the match fails the empty string is assigned, which will save warnings about undefined values when you print.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: matching patterns
by theroninwins (Friar) on Apr 11, 2006 at 07:45 UTC
    Thanks Zaxo but that would be ok if I only needed unit and port vlaue.. but I need others from the string as well.. but those are always there so it was no prob (one int and 2 hex values)... here is the script I got now ... and it works fine:
    open ERASER,">output.txt"; close ERASER; open ERRORLOG, "<error.txt" or die "Can't read file error: $!\n"; open OUTPUT,">>output.txt" or die "Can't read file $!\n"; while( my $line = <ERRORLOG> ) { chomp $line; @line=split " ",$line; if ($line[0]=~ /^c/) { $line =~/(c([0-9]+)).*(0x[A-Fa-f0-9]+).*(0x[A-Fa-f0-9] ++)/; my $conn = $2; my @error = ($3, $4); my $value1= hex $error[0]; my $value2= hex $error[1]; my $unit = ''; if ($line =~/(unit=([0-9]+))/) { $unit= $2; }; my $port = ''; if ($line =~/(port=([0-9]+))/) { $port= $2; }; print OUTPUT "Conn: $conn, Value1: $value1,Value2: $v +alue2, Unit: $unit,Port1: $po rt,Stat: $line[6]\n"; } } close ERRORLOG; close OUTPUT;
      Perhaps a minor point, but why are you capturing "port=" and "unit=" in your regexes
      if ($line =~/(port=([0-9]+))/)
      ? Couldn't it just as easily be
      if ($line =~/port=([0-9]+)/){ $port= $1; };


      -----------------
      s''limp';@p=split '!','n!h!p!';s,m,s,;$s=y;$c=slice @p1;so brutally;d;$n=reverse;$c=$s**$#p;print(''.$c^chop($n))while($c/=$#p)>=1;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://542469]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-23 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found