Wagner, David --- Senior Programmer Analyst --- WGO wrote:
Here is a watered down version, but unclear what I am missing. You should
be able to cut and past. It is self contained and I am running on XP Pro,
using AS 5.8.4.

What am I trying to do? Well I have to implement a new setup. So I pull
the reports I use for the emails from one system and place on my test node.
 I then copy all the output from my first node to holding area. Then I run
the processes on my test node. I then copy all the output from the test
node to a holding area.

I then bring up to a pc.

I have a small script that reads the files, then does a count of carriage
returns. If carriage returns are equal, then I compare the report output.
What I have in the output is timestamps and run times which I need to
remove otherwise will never be equal.

This one seems so simple, yet it is eluding me.  If you want to see more of
the output, then you can uncomment the lines needed.

Thanks for any insight and what I am doing wrong.

What you are doing wrong is improperly using or omitting regular expression options, specifically /g and /m.



cut starts on next line:
#!perl


use strict;
use warnings;

my @MyWrkP = ();
my @MyWrkT = ();
my $MyProdFile = 'aaaaa.pl026.02.P.txt';
my $MyTestFile = 'aaaaa.pl026.02.T.txt';;
my $MyPtr = 1;
my $MyWrkData = '';

my $MyProdCnt;
my $MyTestCnt;

while ( <DATA> ) {
    if ( /^\s*_{1,2}end\d_{1,2}\s*$/ig ) {

You are using the /g option in scalar (boolean) context which is superfluous.

      if ( /^\s*_{1,2}end\d_{1,2}\s*$/i ) {


        if ( $MyPtr == 1 ) {
            $MyWrkP[0] = $MyWrkData;
            $MyPtr++;
#            $MyWrkData = $MyWrkP[0];
#            $MyWrkData =~ s/\n/;/igm;

You are using the /i option but there are no alphabetic characters in the regular expression so it is superfluous. You are using the /m option which affects the behaviour of the ^ and $ anchors which you aren't using so it is superfluous. If you want to translate one character to another character globally you should use the tr/// operator instead.


#            $MyWrkData =~ tr/\n/;/;


#            printf "Data into Prod\n<%s>\n", $MyWrkData;
#            printf "Number of ;(c/r): %d\n", ($MyWrkData =~ tr/;//);
            $MyWrkData = '';
            next;
         }else {
            $MyWrkT[0] = $MyWrkData;
            $MyPtr++;
#            $MyWrkData = $MyWrkT[0];
#            $MyWrkData =~ s/\n/;/igm;
                                    ^^^  see above.

# printf "Data into Test\n<%s>\n", $MyWrkData;
# printf "Number of ;(c/r): %d\n", ($MyWrkData =~ tr/;//);
$MyWrkData = '';
last;
}
}
$MyWrkData .= $_;
}
#
# See if works here
#
if ( $MyProdFile =~ /\.0[23]\./g ) {
                                     ^  see above.


        $_ = $MyWrkP[0];

        s/fes.//ig;
        if ( $MyTestFile =~ /pl026/ig ) {
                                      ^  see above.


if ( ! s!^Run Date/Time of Report:\s+\d+\D\d+\D\d+\D\d+\D\d+\D\d+\s*\n!!ig ) {

^ see above.

Your regular expression is anchored at the beginning of the string but it will never match there. Because the string contains multiple lines you need to use the /m option to have it match the correct line in the string.

if ( ! s!^Run Date/Time of Report:\s+\d+\D\d+\D\d+\D\d+\D\d+\D\d+\s*\n!!im ) {


                printf "No valid hit on change of date/time for pl026(P)\n";
                printf "%s", $MyWrkP[0];
             }
         }else {
            s/Date:\s+\d+\D\d+\D\d+\D\d+\D\d+\D\d+\s*$//igm;
                                                           ^  see above.


         }
        $MyWrkP[0] = $_;

        $_ = $MyWrkT[0];
        s/fes.//ig;

        if ( $MyProdFile =~ /pl026/ig ) {
            if ( ! s!^Run Date/Time of 
Report:\s+\d+\D\d+\D\d+\D\d+\D\d+\D\d+\s*\n!!ig ) {

^ see above.


                printf "No valid hit on change of date/time for pl026(T)\n";
                printf "%s", $MyWrkT[0];
             }
         }else {
            s/Date:\s+\d+\D\d+\D\d+\D\d+\D\d+\D\d+\s*$//igm;
                                                           ^  see above.


         }
        $MyWrkT[0] = $_;

     }
    $MyProdCnt = ( $MyWrkP[0] =~ tr/\n// );
    $MyTestCnt = ( $MyWrkT[0] =~ tr/\n// );
    if ( $MyProdCnt == $MyTestCnt ) {
        printf "  %6d == %6d<-c/r  ",
                                $MyProdCnt,
                                $MyTestCnt;
        if ( $MyWrkP[0] eq $MyWrkT[0]) {
            printf "%-13s",
                                'Contents ==';
         }else {
            printf "%-13s",
                                'Contents !=';
            printf "Prod:\n<%s>\n", $MyWrkP[0];
            printf "Test:\n<%s>\n", $MyWrkT[0];
         }
        printf "$MyProdFile\n";
     }else {
        printf "  %6d != %6d%-20s$MyProdFile\n",
                                $MyProdCnt,
                                $MyTestCnt,
                                ' ';
     }

Also, printf() should only be used when you need the formatting that it provides as it is less efficient then print() and interpolating variables in the format string will not work properly if there are format meta-characters in the variable.


Here is another way to write the same program:

#!perl

use strict;
use warnings;

my $MyProdFile = 'aaaaa.pl026.02.P.txt';
my $MyTestFile = 'aaaaa.pl026.02.T.txt';

my %data;
{ local $/ = "__\n";
    while ( <DATA> ) {
        s/.+\n\z//;    # remove the last line ( __END__ tag )

        s/fes([pt])//i and ( my $type = uc $1 ) or next;

if ( ( $type eq 'P' ? $MyProdFile : $MyTestFile ) =~ /pl026/i ) {
unless ( s|^Run Date/Time of Report\s*:\s+\d+\D\d+\D\d+\D\d+\D\d+\D\d+\s*\n||im ) {
print "No valid hit on change of date/time for pl026($type)\n$_";
}
}
else {
s/Date\s*:\s+\d+\D\d+\D\d+\D\d+\D\d+\D\d+\s*$//im;
}


my $count = ( my $temp = $_ ) =~ tr/\n/;/;
# print 'Data into ', $type eq 'P' ? 'Prod' : 'Test', "\n<$temp>\nNumber of ;(c/r): $count\n";


        @{ $data{ $type } }{ qw/count data/ } = ( $count, $_ );
        }
    }

if ( $data{ P }{ count } == $data{ T }{ count } ) {
printf ' %6d == %6d<-c/r ', $data{ P }{ count }, $data{ T }{ count };
if ( $data{ P }{ data } eq $data{ T }{ data } ) {
print 'Contents == ';
}
else {
print "Contents != Prod:\n<$data{P}{data}>\nTest:\n<$data{T}{data}>\n";
}
print "$MyProdFile\n";
}
else {
printf " %6d != %6d%-20s%s\n", $data{ P }{ count }, $data{ T }{ count }, ' ', $MyProdFile;
}


__END__



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to