Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

matching patterns

by theroninwins (Friar)
on Apr 11, 2006 at 06:58 UTC ( #542462=perlquestion: print w/replies, xml ) Need Help??

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

This it the code:
if ($line[0]=~ /^c/) { $line =~/(0x[A-Fa-f0-9]+).*(0x[A-Fa-f0-9]+).*((unit=([ +0-9]+)))/i; my @error = ($1, $2); my $value1= hex $error[0]; my $value2= hex $error[1]; my $unit= $5; $line =~/(port=([0-9]+))/i; my $port= $2; print "Value1: $value1,Value2: $value2, Unit: $unit,P +ort1: $port,Stat: $line[6]\n"; } ----data------ Ctl Date Severity Alarm Message ---------------------------------------------------------------------- +-------- c0 [Mon Apr 10 15:12:42 2006] INFO (0x04:0x000B): Rebuild star +ted: unit=0 c0 [Mon Apr 10 15:12:09 2006] INFO (0x04:0x003B): Rebuild paus +ed: unit=0 c0 [Mon Apr 10 15:08:28 2006] INFO (0x04:0x000B): Rebuild star +ted: unit=0 c0 [Mon Apr 10 15:06:31 2006] ERROR (0x04:0x0002): Degraded uni +t: unit=0, port=1 c0 [Mon Apr 10 15:06:31 2006] WARNING (0x04:0x0019): Drive was re +moved from a bay: port=1
The prob:

The $2 value is wrong in the print out when there is no port... so I would need to delete it but $2 is read-only :-)

Any idea??

Thanks for the help

2006-04-11 Retitled by g0n, as per Monastery guidelines
Original title: 'matching pattens'

Replies are listed 'Best First'.
Re: matching patterns
by Corion (Patriarch) on Apr 11, 2006 at 07:02 UTC

    I said it in the CB already:

    Never use an unguarded assignment after a regex match. Always do
    die "No data found" unless /...(...)/; my $value = $1;/
    or do
    my $value; if (/...(...)/) { $value = $1 };

    In your case, this amount to:

    ... my $port = '<no port found>'; if ($line =~/(port=([0-9]+))/i) { $port= $2; };
      Thanks for the help.. didn't get it in the CB... now it does the trick
Re: matching patterns
by Zaxo (Archbishop) on Apr 11, 2006 at 07:18 UTC

    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,

      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?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://542462]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2022-01-20 11:47 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (56 votes). Check out past polls.