On Jun 26, 2014, at 2:05 AM, Uday Vernekar wrote:
> Please suggest if any Corrections Needed.
There are several improvements you could make.
>
> [code]
> #!/usr/bin/perl
>
> use 5.10.0;
> use strict;
> use warnings;
> #########################Pattern############################################
> #U/A/S|Test| Test |Loop | Run |Pass |Fail | Arguments
> # | # | Name |Count|Count|Count|Count|
> #-----+----+---------------------------+-----+-----+-----+-----+-----------
> # | 72| Traffic Test | 1| 561| 560| 1| (none)
> ############################################################################
>
> my $pattern;
"$pattern" is not a good variable name for holding a string that matches some
pattern. The 'pattern' is embedded in your regular expression. I would use
something like "$match" or $found.
> open (my $Log_file, '<', '/tmp/EO-PCPE-23-10GT') || die "Couldn't open
> /tmp/EO-PCPE-23-10GT\n\t $!";
When posting programs to a mailing list, you can use the special <DATA> file
handle and put the lines at the end of your program after a line containing
just a '__DATA__' marker. That way, people who do not have access to your data
file will be able to run your program.
> while(<$Log_file>)
It is usually better to use explicit variables:
while( my $line = <$Log_file> ) {
if( $line =~ ... ) {
> {
>
> if($_ =~
> m/^(.+?)\|(.+72)\|(.+?)\|(.+?)\|(.+[0-9]|[0-9]|[0-9])\|(.+[0-9]|[0-9]|[0-9])\|(.+?)\|(.+?)$/)
This reqular expression is very long and hard to read. You seem to want match
any line that:
1. Has seven '|' characters delimiting eight fields.
2. Has '72' in field 2.
3. Has numbers in fields 5 and 6.
I would recommend first splitting the line, then checking the individual fields
for their desired content.
In addition, the regex fragment '(.+[0-9]|[0-9]|[0-9])' may not be doing what
you think. This fragment will match:
1. At least one character followed by a digit (0-9) OR
2. A digit (0-9) OR
3. A digit (0-9)
The | character is alternation. You seem to be treating it as concatenation.
The second and third alternatives are identical, and the third one is redundant.
If you want to match three successive digits, these will all work:
[0-9][0-9][0-9]
[0-9]{3}
\d\d\d
\d{3}
> {
> $pattern=$_;
>
>
> }
>
> }
> print "Pattern Found:\n$pattern";
> my $uas=(split /\|/, $pattern)[0];
> $uas =~ s/^\s+//;
> my $test=(split /\|/, $pattern)[1];
> $test =~ s/^\s+//;
> my $test_name=(split /\|/, $pattern)[2];
> $test_name =~ s/^\s+//;
> my $loop_count=(split /\|/, $pattern)[3];
> $loop_count =~ s/^\s+//;
> my $run_count=(split /\|/, $pattern)[4];
> $run_count =~ s/^\s+//;
> my $pass_count=(split /\|/, $pattern)[5];
> $pass_count =~ s/^\s+//;
> my $fail_count=(split /\|/, $pattern)[6];
> $fail_count =~ s/^\s+//;
> my $arguments=(split /\|/, $pattern)[7];
> $arguments =~ s/^\s+//;
You can perform the split only once. You can also include optional whitespace
in the delimiter string, which means the saved substrings will not include the
whitespace:
my( $uas, $test, $test_name, $loop_count, $run_count, $pass_count,
$fail_count,
$arguments ) = split('\s*\|\s*',$line);
> print '-' x 30, "\n";
> print " Test Data \n";
> print '-' x 30, "\n";
> print "UAS : $uas\n";
> print "Test : $test\n";
> print "Test Name : $test_name\n";
> print "Loop Count : $loop_count\n";
> print "Run Count : $run_count\n";
> print "Pass Count : $pass_count\n";
> print "Fail Count : $fail_count\n";
> print "Arguments : $arguments\n";
> print '-' x 30, "\n";
> print " RESULTS \n";
> print '-' x 30, "\n";
You can use a 'HERE' document to print these lines:
print <<EOS;
------------------------------
Test Data
------------------------------
UAS : $uas
Test : $test
Test Name : $test_name
Loop Count : $loop_count
Run Count : $run_count
Pass Count : $pass_count
Fail Count : $fail_count
Arguments : $arguments
------------------------------
RESULTS
------------------------------
EOS
So putting that altogether, here is an improved program:
[code]
#!/usr/bin/env perl
use strict;
use warnings;
while( my $line = <DATA> ) {
chomp($line);
next if $line =~ /^-/;
my @f = split('\s*\|\s*',$line);
next unless scalar @f == 8;
my( $uas, $test, $test_name, $loop_count, $run_count, $pass_count,
$fail_count,
$arguments ) = @f;
if( $test eq '72' ) {
print <<EOS;
------------------------------
Test Data
------------------------------
UAS : $uas
Test : $test
Test Name : $test_name
Loop Count : $loop_count
Run Count : $run_count
Pass Count : $pass_count
Fail Count : $fail_count
Arguments : $arguments
------------------------------
RESULTS
------------------------------
EOS
if( $fail_count != 0 && $run_count >= 0 ) {
print "Result: Fail\n";
}elsif( $fail_count == 0 && $run_count > 0 ) {
print "Result: Pass\n";
}
}
}
__DATA__
U/A/S|Test| Test |Loop | Run |Pass |Fail | Arguments
| # | Name |Count|Count|Count|Count|
-----+----+---------------------------+-----+-----+-----+-----+-----------
| 72| Traffic Test | 1| 561| 560| 1| (none)
[code]
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/