对于我的脚本,Perl的开始时间总是相同

时间:2013-03-17 11:53:16

标签: perl

我制作了一个脚本来提取日志文件的内容,并在任务完成时计算时差。

假设我有四个工作,每个工作都有各自的任务,到目前为止我需要每个任务的开始,然后打印它。

一切都很好,除非我尝试初始化以方便使用$j$l作为二维数组。

问题出在输出处,我为每个工作得到了相同的“开始于”。

$counter$l的值应该是根本原因。

有人可以帮忙吗?我尽我所能,有点新手。

sub getdate {
    my $line = $_[0];

    ($hrs, $min) = split(':', $line, 3);
    return $hrs, $min;
}

print FILE "<html><head>\n";
print FILE "<title>CGI Test</title>\n";
print FILE "</head>\n";
print FILE "<body>";
print FILE "<font size=\"5\" color=\"#008080\" face=\"Tahoma\"><b><u><br>";
print FILE "PBI Batch for 22/02/2013";
print FILE "</font></b></u><br><br><br>";

my $i = 0;
my $j = 0;
my $l = 0;

my @sample;

#print FILE "<h4>";

foreach $header (<COLLECTION>) {
    chomp($header);
    ($heading, $filepath) = split(',', $header);

    #$two[$i]="<font size=\"3\"color=\"#008000\" face=\"Tahoma\"><b><u><br>";
    #$two[$i]="<font size=\"3\" color=".$color." face=\"Tahoma\"><b><u><br>";
    $two[$i] .= $heading;

    #$two[$i] .= "</font></u></b><br>";
    #print FILE "<font size=\"3\" color=\"#008000\" face=\"Tahoma\"><b><u><br>";
    #       print FILE $two[$i];
    #print FILE $heading;
    #print FILE "</font></u></b><br>";

    #print $filepath."\n";
    open(MYFILE1, $filepath) or die 'Could nont openfile';

    my $counter;
    foreach $list (<MYFILE1>) {

        chomp($list);
        ($file, $path) = split(',', $list);

        #print FILE $file;
        my @secondstart;
        my @secondend;
        my $secondcounter = 0;

        #print FILE "valueofllllllllllllllllllllllllllll".$l;
        foreach $counter ($file) {
            print FILE "valueofllllllllllllllllllllllllllll" . $l;
            $l++;
            $sample[$j][$l] = $counter;
            print FILE "secCOUNTER  " . $secondcounter;
            $secondcounter++;
        }

        print FILE "                                space";

        open(MYFILE, $path) or die 'ERRROR';
        my $count = 0;
        foreach $line (<MYFILE>) {

            my @endtime;

            $flag = 'false';

            #$counter++;
            $count++;
            print FILE $count . "========";

            if ($count == 1) {
                ($hrs, $min) = getdate($line);
                $starttime[$j][$l] = ($hrs * 60) + $min;
            }
            else {
                ($hrs, $min) = split(':', $line, 3);
                if ($line =~ m/End of Procedure/) {
                    $flag            = 'true';
                    $endtime[$j][$l] = $hrs . $min;
                    $endtime[$j][$l] = ($hrs * 60) + $min;
                }
                else {
                    $endtime[$j][$l] = ($hrs * 60) + $min;
                }
            }

            $duration[$j][$l] = $endtime[$j][$l] - $starttime[$j][$l];

        }

        #   print $flag;

        #print FILE $file." : ";
        #print FILE "value of ".$j."and".$l;
        $startstatus[$j][$l]    = "Started at" . $starttime[$j][$l];
        $durationstatus[$j][$l] = "&nbspDuration is " . $duration[$j][$l] . "m";

        # print FILE "Started at".$starttime;
        # print FILE "&nbspDuration is ".$duration."m";

        #                 print FILE "<br>";

        close(MYFILE);

    }

    my $valueofl = $l;

    #print FILE "vlaeeofl".$valueofl;
    print "valueofllllllllllllllllllllllllllll" . $l;
    $l = 0;

    if ($flag eq 'true') {
        $status = 'Completed';
        $color  = '#008000';

        print FILE "<font size=\"3\" color=" 
            . $color
            . " face=\"Tahoma\"><b><u><br>"
            . $two[$i]
            . "</font></u></b><br>";
        print FILE $status . "<br>";

        while ($l <= $valueofl) {

            #print $j."and".$l;
            #   print "valueofllllllllllllllllllllllllllll".$l;

            print FILE $sample[$j][$l] . "&nbsp&nbsp&nbsp&nbsp";
            print FILE $startstatus[$j][$l] . "&nbsp&nbsp&nbsp&nbsp";
            print FILE $durationstatus[$j][$l] . "<br>";
            $l++;
        }

        # print FILE $startstatus[$j][0];
        # print FILE $durationstatus[$j][0];
    }
    else {
        #print "valueofllllllllllllllllllllllllllll".$l;
        #print $j."and".$l;
        $status = 'In Progress';
        $color  = 'blue';
        print FILE "<font size=\"3\" color=" 
            . $color
            . " face=\"Tahoma\"><b><u><br>"
            . $two[$i]
            . "</font></u></b><br>"
            . $status;
    }
    $i++;
    $j++;

}

print FILE "</body>";
print FILE "</html>";

close(FILE);
close(MYFILE1)

1 个答案:

答案 0 :(得分:4)

这是一个令人震惊的Perl。您必须始终开始使用use strictuse warnings进行编程,并使用my声明所有变量尽可能接近其第一个使用点。这是最基本的调试形式,在向其他人寻求帮助之前,至少要做到这一点是礼貌的。

问题可能在于您的for声明

foreach $counter ($file) { ... }

将执行一次的循环体,$content设置为$file的值。我无法想象你的意思。