计算文件中的总记录数,并与Perl中的预告片记录进行比较

时间:2018-02-19 06:13:12

标签: perl

我有以下代码

if ( -e $filein ) {

    if ( open( FILEIN, $filein ) ) {

        print log_date() . "STARTING TO CHECK  FILE \n";

        while ( <FILEIN> ) {

            chomp( $_ );

            if ( length( $_ ) == $l_recordLength ) {

                $rectype = substr( $_, $0, 3 );
                my $config = "fdposmarkertaxentity.ini";

                if ( -e $config ) {

                    my $cfg = new Config::IniFiles( -file => $config );
                    print "$rectype \n";

                    if ( $rectype eq "999" ) {

                        print log_date() . "CHECK TRAILER RECORD IN CHECKFILE SUBROUTINE WHILE CHECKING  FILE \n";
                        $filereccnt = substr( $_, 3, 17 );

                        if ( ( $filereccnt != $. ) || ( $. <= 2 ) ) {

                            if ( $BLog ) {

                                $errhdrordtlrec += $l_valueOne;

                                print HEADERTRAILER_ERROR_LOGFILE log_date() . "$. \n";
                                print HEADERTRAILER_ERROR_LOGFILE log_date() . "$_ \n";
                                print HEADERTRAILER_ERROR_LOGFILE log_date() . "====================================================================================================== \n";
                                $ERRHDRTRLRRCRDMSG = "Trailer record count does not match record or no records present in file.";
                                print HEADERTRAILER_ERROR_LOGFILE log_date() . "$ERRHDRTRLRRCRDMSG $filereccnt \n";
                                print HEADERTRAILER_ERROR_LOGFILE log_date() . "====================================================================================================== \n";

                                $BTrailerRecAlreadyRead = $l_valueOne;
                            }
                        }
                    }
                }
            }
        }
    }
}

我的记录文件包含以下输入

000FDPOSTAXENTITY2018021317243200001
DTL11~|~|110~|~|220~|~|T0333~|~|~|~|
DTL11~|~|333~|~|444~|~|T0555~|~|~|~|
DTL11~|~|555~|~|222~|~|T0777~|~|~|~|
99900000000000000005~|~|~|~|~|~|~|~|

我想比较输入文件中的行数。 00000000000000005是文件中的行数。我的记录文件有五行。如果详细记录为5且预告片记录00000000000000005不是5或类似00000000000000007,那么它将抛出错误消息,如

  

预告片记录计数不匹配

否则如果是00000000000000005则抛出

  

预告片记录计数匹配

任何人都可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:-1)

不确定我是否正确理解了您的问题。我就是这样写的:

use strict;
use warnings;

my $records = 0;
my $line;
while (<DATA>) {
    #print "LINE: $_";

    # inside a record
    if (/^000/.../^999/) {
        #print "RECORD: $_";

        # start of record
        $line = 0 if /^000/;

        $line++;

        # end of record
        if (/^999/) {
            $records++;

            # record length check
            my($record_length) = /^999(\d+)/;
            $record_length += 0; # integer
            die "Record $records is $line line(s) long, but should be $record_length!\n"
                unless $line == $record_length;

            print "Record $records is OK\n";
        }
    }
}

__DATA__
asdjaslkdjasd
asdjasdjasld
asdjasdaskd

000FDPOSTAXENTITY2018021317243200001
DTL11~|~|110~|~|220~|~|T0333~|~|~|~|
DTL11~|~|333~|~|444~|~|T0555~|~|~|~|
DTL11~|~|555~|~|222~|~|T0777~|~|~|~|
99900000000000000005~|~|~|~|~|~|~|~|

asdasdasd

asdasdasdasd

000FDPOSTAXENTITY2018021317243200001
DTL11~|~|110~|~|220~|~|T0333~|~|~|~|
DTL11~|~|555~|~|222~|~|T0777~|~|~|~|
99900000000000000005~|~|~|~|~|~|~|~|

asdasdasd

在嵌入数据上运行示例:

Record 1 is OK
Record 2 is 4 line(s) long, but should be 5!