无法获得第二次测试的预期输出

时间:2016-05-01 02:44:32

标签: arrays perl match

我想要我的剧本

  • 从ITUFF提取每个测试名称并转换小数值(来自测试名称后面的mrslt行)
  • 使用ITUFF的测试名称在MAP文件中查找匹配的记录并提取ID和引脚值
  • 从TPLFILE
  • 中提取修改令牌“AFC”
  • 使用ID打开相关的IN文件
  • 打开每个IN文件以提取输出标签(“TDO_STROBE”...“TDO_STROBE_9”)。
  • 再次打开每个IN文件,用空格替换所有出现的“CTV”字符串。将修改后的文件放入不同的目录中,保留原始文件
  • 为每个MODULE打印modify.txt文件

例如,对于MODULE_A,TEST_ABC是2位且TDO_STROBE&需要为它们分配TDO_STROBE_0。 TEST_BCD为9位,需要分配TDO_STROBE_1到TDO_STROBE_9。

我设法为MODULE_A分配了TDO_STROBE标签的正确二进制位,但$ pat1 [0]中的ID错误,并且为MODULE_B分配给TDO STROBE标签的后续位也搞砸了。

输入文件

ITUFF

2_tname_chuid
2_mrslt_3
0_tname_CKK_MODULE_A::TEST_ABC
0_mrslt_1
0_tname_CKK_MODULE_A::TEST_BCD
0_mrslt_451
0_tname_CKK_MODULE_B::TEST_EFG
0_mrslt_2
0_tname_CKK_MODULE_B::TEST_FEI
0_mrslt_405

MAP

Test,ID,Pin,Token
TEST_ABC,41534,TDO,AFC
TEST_BCD,41534,TDO,AFC
TEST_EFG,41532,GPIO_0,AFC
TEST_FEI,41532,GPIO_0,AFC

TPLFILE

Test PatModTest
{
    input_file = "~PATMO_PATH\\modify.txt";
    modify_token = "AFC";
}

有两个IN文件,其文件名中包含不同的ID

IN

#
TDO_STROBE:
NOP          
NOP          
NOP          
NOP       CTV
TDO_STROBE_0:
NOP          
NOP          
NOP          
NOP       CTV
TDO_STROBE_1:
NOP          
NOP          
NOP          
NOP       CTV
TDO_STROBE_2:
NOP          
NOP          
NOP          
NOP       CTV
TDO_STROBE_3:
NOP          
NOP          
NOP          
NOP       CTV
TDO_STROBE_4:
NOP          
NOP          
NOP          
NOP       CTV
TDO_STROBE_5:
NOP          
NOP          
NOP          
NOP       CTV
TDO_STROBE_6:
NOP          
NOP          
NOP          
NOP       CTV
TDO_STROBE_7:
NOP          
NOP          
NOP          
NOP       CTV
TDO_STROBE_8:
NOP          
NOP          
NOP          
NOP       CTV
TDO_STROBE_9:

我的Perl代码

$ituf = <"*ituff*.txt">;
open( ITUFF, "<$ituf" ) || die "can't open the file";
while (<ITUFF>) {

    if ( $_ =~ /tname_CCK/ ) {

        @tname  = split /::/, $_;
        @tnameC = split /\n/, $tname[1];

        open( MAP, "<test_tupple.csv" ) || die "can't open the file";
        while (<MAP>) {
            @line = ( split ',', $_ );
            if ( $_ =~ /$tnameC[0]/ ) {
                push @test, $line[0];
            }
        }
    }
}
close ITUFF;
close MAP;


open( MAP, "<test_tupple.csv" ) || die "can't open the file";
while (<MAP>) {

    next if /^Test/;
    chomp;

    my @line = ( split ',', $_ );

    foreach $tn (@test) {

        if ( $line[0] eq $tn ) {
            $search = "found";
            push @test_map, $line[0];
            push @ID,       $line[1];
            push @pin,      $line[2];
        }
        else {
            $search = "not found";
        }
    }
}
close MAP;


$tpl = <"*CCK*.tpl">;
open( TPLFILE, "<$tpl" ) || die "can't open the file";
while (<TPLFILE>) {
    if ( $_ =~ /modify_token/ ) {
        @tpl_line0 = split /=/, $_;
        @tpl_line1 = split /;/, $tpl_line0[1];
        @tpl_line2 = split /"/, $tpl_line1[0];
    }
}
close TPLFILE;


$input = <"*@ID[$#ID]*.pat.data">;
@pat   = split /ALL.pat.data/, $input;
@pat1  = split /_/, $pat[0];
@tid   = split /C/, $pat1[0];


open( IN, "<$input" ) || die "can't open the file";
my $keyword = "CTV";
my $regex   = qr|\b($keyword)\b|;
while (<IN>) {

    next if /^#/;
    chomp;

    if ( $_ =~ /STROBE/ ) {
        @label = split /:/, $_;
        push @labelList, $label[0];
    }
    while (/$regex/g) {
        push @vectorLine, $.;
        $j = join ' ', @vectorLine;
        @vector = split /\s/, $j;
    }
}
close IN;


$d = $vector[1] - $vector[0] - 1;


my $replacestring = "CTV";
open( IN, "<$input" ) || die "can't open the file";
open( my $outfile, ">", "C:/Users/klee/Documents/perl/pat_heck/$pat[0]All.pat.data" ) || die "can't open the file";
while (<IN>) {
    $_ =~ s/$replacestring/   /g;
    print $outfile $_;
}
close IN;


open( ITUFF, "<$ituf" ) || die "can't open the file";
while (<ITUFF>) {
    foreach $tn (@test) {
        if ( $_ =~ /$tn/ ) {
            my $nextLine = <ITUFF>;
            @dec_res = split /_/, $nextLine;
            $bin_res = sprintf( "%02b", $dec_res[2] );
            push @bv, split //, $bin_res;
            $bitNum = ( $bin_res =~ tr/[0-1]// );
            push @totalBit, $bitNum;
        }
    }
}
close ITUFF;


foreach $b (@bv) {
    if ( $b == 0 ) {
        $tmap = "L";
    }
    else {
        $tmap = "H";
    }
    push @HL, $tmap;
}

for ( $t = 0; $t <= $#test; $t++ ) {
    push @test_iteration, $t;
}


open( OUT, ">modify.txt" ) || die "can't open the file";
foreach $ite (@test_iteration) {
    for (
        $i = $ite * $totalBit[ $ite - 1 ];
        $i < $totalBit[$ite] + $ite * $totalBit[ $ite - 1 ];
        $i++ )
    {
        print OUT "+ $tpl_line2[1] MAIN PAT $pat1[0]_$pat1[1]_$pat1[2]_$pat1[3]_$pat1[4]_*_$pat1[6]_$pat1[7]_$pat1[8]_$pat1[9]_$pat1[10]_$pat1[11]_$pat1[12] $labelList[$i] +$d DATA $pin[$ite] $HL[$i]\n";
    }
}
close OUT;


#checking use
for ( $i = 0; $i < $totalBit[0]; $i++ ) {

    #print "$test[$i] $labelList[$i]\n";
}

for ( $i = $totalBit[0]; $i <= $totalBit[1]; $i++ ) {

    #print "subsequent $labelList[$i]\n";
}

预期产出:

+ AFC MAIN PAT g0041534_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE +4 DATA TDO L
+ AFC MAIN PAT g0041534_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_0 +4 DATA TDO H
+ AFC MAIN PAT g0041534_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_1 +4 DATA TDO H
+ AFC MAIN PAT g0041534_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_2 +4 DATA TDO H
+ AFC MAIN PAT g0041534_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_3 +4 DATA TDO H
+ AFC MAIN PAT g0041534_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_4 +4 DATA TDO L
+ AFC MAIN PAT g0041534_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_5 +4 DATA TDO L
+ AFC MAIN PAT g0041534_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_6 +4 DATA TDO L
+ AFC MAIN PAT g0041534_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_7 +4 DATA TDO L
+ AFC MAIN PAT g0041534_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_8 +4 DATA TDO H
+ AFC MAIN PAT g0041534_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_9 +4 DATA TDO H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE +4 DATA GPIO_0 H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_0 +4 DATA GPIO_0 L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_1 +4 DATA GPIO_0 H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_2 +4 DATA GPIO_0 H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_3 +4 DATA GPIO_0 L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_4 +4 DATA GPIO_0 L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_5 +4 DATA GPIO_0 H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_6 +4 DATA GPIO_0 L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_7 +4 DATA GPIO_0 H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_8 +4 DATA GPIO_0 L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_9 +4 DATA GPIO_0 H

实际输出:

+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE +4 DATA TDO L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_0 +4 DATA TDO H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_1 +4 DATA TDO H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_2 +4 DATA TDO H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_3 +4 DATA TDO H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_4 +4 DATA TDO L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_5 +4 DATA TDO L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_6 +4 DATA TDO L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_7 +4 DATA TDO L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_8 +4 DATA TDO H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_9 +4 DATA TDO H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count  +4 DATA GPIO_0 L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count  +4 DATA GPIO_0 H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_5 +4 DATA GPIO_0 L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_6 +4 DATA GPIO_0 L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_7 +4 DATA GPIO_0 L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_8 +4 DATA GPIO_0 H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count TDO_STROBE_9 +4 DATA GPIO_0 H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count  +4 DATA GPIO_0 H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count  +4 DATA GPIO_0 L
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count  +4 DATA GPIO_0 H
+ AFC MAIN PAT g0041532_B_n1604_CB0609a_dack_*_B_F_dfx_f_pp_h_count  +4 DATA GPIO_0 H

1 个答案:

答案 0 :(得分:2)

我担心您的代码存在很多问题,主要问题是您的想法并不是很清楚。在开始测试之前,您似乎也编写了整个程序,正如您所发现的那样,在没有基础的情况下进行了大量的调试

您应该一次编写不超过三行或四行代码并在继续之前测试这些代码。这样你就永远不会建立大量的调试工作,你将始终对最新版本的程序充满信心

其他一些指导原则

  • 您应该总是 use strictuse warnings 'all'位于您编写的每个 Perl程序的顶部。您还需要声明您使用的所有变量,这些变量应该在代码中尽可能尽可能,而不是在文件开头的大块中

  • 您应该将词汇文件句柄三参数版本的open 一起使用。您也可以在程序顶部use autodie,这将为您检查许多Perl IO操作,以便您不必手动执行此操作

    例如

    open(MAP,"<test_tupple.csv")||die "can't open the file";
    

    更好(autodie就位)这样

    open my $map_fh, '<', 'test_tupple.csv';
    

还有很多其他问题,但我不想让你感到压力。以上两点是迄今为止最重要的一点

现在发现错误

  • 您说您的ITUFF文件包含类似

    的行
    0_tname_CKK_MODULE_A::TEST_ABC
    

    但您正在使用声明

    测试它们
    if ( $_ =~ /tname_CCK/ ) {
    

    永远不会匹配

  • 您对输出for循环中迭代范围的计算是错误的。你有这个

    for $ite (@test_iteration) {
        for (
            $i = $ite * $totalBit[ $ite - 1 ];
            $i < $totalBit[$ite] + $ite * $totalBit[ $ite - 1 ];
            $i++ ) {
    
            print OUT "+ $tpl_line2[1] MAIN PAT $pat1[0]_$pat1[1]_$pat1[2]_$pat1[3]_$pat1[4]_*_$pat1[6]_$pat1[7]_$pat1[8]_$pat1[9]_$pat1[10]_$pat1[11]_$pat1[12] $labelList[$i] +$d DATA $pin[$ite] $HL[$i]\n";
        }
    }
    

应该是这样的

    for $ite (@test_iteration) {

        my $start = 0;
        $start += $_ for @total_bit[ 0 .. $ite - 1 ];

        for my $i ( $start .. $start + $totalBit[$ite] - 1 ) {

            print ...;
        }
    }
  • 最后,您的数据会产生22行输出(表示ITUFF中mrslt个数字的总位数),但@label_list中只有11个项目(已获得)来自IN文件中包含STROBE的行。这意味着只有前11行输出有一个像TDO_STROBE_9等字段。这在过去的11中是空白的

    我无法建议解决方案应该是什么,因为我对您的数据一无所知,并且无法知道缺失值应该来自何处

我希望这能帮到你的路上