我的数组在下一个elsif循环中为空,但如果在循环外使用,则仍保留其值

问题描述 投票:0回答:1

我正在尝试在两个elsif循环之间使用数组,但是如果我在第二个循环内使用它,则数组输出将不返回任何内容。但是,如果我在循环外使用相同的数组,那么它将起作用。例如:

while (<FH>){
    if(condition1){
        do this;
        do that;
    }
    elsif(condition2){
        push (@arr, $_);
    }
    elsif(condition3){
        print @arr;
    }
}

上面的代码不会在输出中为我打印任何内容。

但是,如果我使用下面的代码,它将正确打印数组元素。

while (<FH>){
    if(condition1){
        do this;
        do that;
    }
    elsif(condition2){
        push (@arr, $_);
    }
    elsif(condition3){
        next;
    }
}
print @arr;

有人可以向我解释为什么会这样,或者您可以将我指向可能已经讨论过此问题的话题吗?预先感谢。

更新:根据注释部分的要求,在此处发布我的实际代码以更好地理解。

while(<USER_INPUT>){
    if ($_=~ /(.*) =\n/ ){
        print "\nFormat not correct on line $. of input file. Exiting script\n";
        exit;
    }
    elsif ($_=~ /(.*) =\s+\n/ ){
        print "\nFormat not correct on line $. of input file. Exiting script\n";
        exit;
    }
    elsif ($_=~ /(.*) = \s+(.*)/ ){
        print "\nFormat not correct on line $. of input file. Exiting script\n";
        exit;
    }
    elsif ($_=~ /^TASK_CELL_NAME\|VALUE = (.*)/ ){
        $n=0;
        $cell_name=$1;
        chomp $cell_name;
    }
    elsif ($_=~ /^TASK\|VALUE = (.*)/ ){
        $task=$1;
        chomp $task;
        $rpl= 'new_replay_files/'.$cell_name.'_'.$task.'_calibre.qvi_replay';
        $ref = $ref_dir.'/'.$task.'_calibre.qvi_replay';
    }
    elsif ($_=~ /^(.*)\|VALUE = (.*)/ ){
        $line=$_;
        $var=$1;
        push (@lines, $line);
        push (@vars, $var);
        $n ++ ;
    }

    elsif ($_=~ /^\s*$/ || $_=~ /^\n/){
        open(REF, "<$ref")  || print "\n!!!ERROR OPENING $ref.!!!\n";
        open(OUT, ">$rpl")  || print "\n!!!ERROR OPENING $rpl.!!!\n";

        foreach(@vars){

            while (<REF>){
                if ($_=~ /^(.*)\|VALUE = (.*)/ ){
                    $match_line=$_;
                    $match_var=$1;
                    $lines= \@lines;
                    if(grep( /$match_var/, @vars )){
                        s/$match_line/$lines->[$count]/g;
                        print OUT;
                    }
                }
                else{
                    s/\$CELL/$cell_name/g;
                    print OUT;
                }
                $count ++ ;
            }
        }
        close REF;
        close OUT;
    }

}

close USER_INPUT;

USER_INPUT文件看起来像这样:

TASK_CELL_NAME|VALUE = AAA
TASK|VALUE = lvs
TASK_VERSION|VALUE = 000
CHIP_PKG_TYPE|VALUE = 111

TASK_CELL_NAME|VALUE = BBB
TASK|VALUE = lvs
TASK_VERSION|VALUE = 222
CHIP_PKG_TYPE|VALUE = 333

TASK_CELL_NAME|VALUE = CCC
TASK|VALUE = lvs
TASK_VERSION|VALUE = 444
CHIP_PKG_TYPE|VALUE = 555
arrays loops perl
1个回答
0
投票

终于对我有用。感谢您抽出宝贵时间来检查我的问题和评论

    use strict;
use warnings;

#########################################################################################################################################################################################

my $num_args = $#ARGV +1 ;
if ($num_args != 0) 
{

   print "\nUsage: perl replay_file_changes.pl\n";
   exit;
}

#########################################################################################################################################################################################


#########################################################################################################################################################################################

print "\nPLEASE SPECIFY INPUT FILE\n";
my $ip_file = <>;
chomp $ip_file ;
if (-e $ip_file){
    }
else{
    print "\n$ip_file IS NOT FOUND\n";
    exit;
    }
print "\nPLEASE SPECIFY REFERENCE FILES DIRECTORY\n";
my $ref_dir = <>;
chomp $ref_dir ;
if (-d $ref_dir){
    }
else{
    print "\n$ref_dir IS NOT FOUND\n";
    exit;
    }
#########################################################################################################################################################################################






#########################################################################################################################################################################################



`rm -rf new_replay_files`;
`mkdir new_replay_files`;
my @lines=();
my @vars=();
my $cell_name;
my $rpl;
my $ref;
my $task;

open(USER_INPUT, "<$ip_file")  || die "\n!!!ERROR OPENING INPUT FILE. EXITING SCRIPT!!!\n";


while(<USER_INPUT>){
                  if ($_=~ /(.*) =\n/ ){
                                print "\nFormat not correct on line $. of input file. Exiting script\n";
                                exit;
                               }
                  elsif ($_=~ /(.*) =\s+\n/ ){
                                  print "\nFormat not correct on line $. of input file. Exiting script\n";
                                  exit;
                                 }
                  elsif ($_=~ /(.*) = \s+(.*)/ ){
                                     print "\nFormat not correct on line $. of input file. Exiting script\n";
                                     exit;
                                    }
                  elsif ($_=~ /^TASK_CELL_NAME\|VALUE = (.*)/ ){
                                            $cell_name=$1;
                                            chomp $cell_name;
                                           }
                  elsif ($_=~ /^TASK\|VALUE = (.*)/ ){
                                      $task=$1;
                                      chomp $task;
                                      $rpl= 'new_replay_files/'.$cell_name.'_'.$task.'_calibre.qvi_replay';
                                      $ref = $ref_dir.'/'.$task.'_calibre.qvi_replay';
                                         }
                  elsif ($_=~ /^(.*)\|VALUE = (.*)/ ){                
                                          my $line=$_;
                              my $var=$1;
                              push (@lines, $line);
                              push (@vars, $var);
                              }

              elsif ($_=~ /^\s*$/ || $_=~ /^\n/){



                               open(REF, "<$ref")  || print "\n!!!ERROR OPENING $ref.!!!\n";
                                   open(OUT, ">$rpl")  || print "\n!!!ERROR OPENING $rpl.!!!\n";
                               while (<REF>){

                                         if ($_=~ /^(.*)\|VALUE = (.*)/ ){
                                                         my $match_line=$_;
                                                         my $match_var=$1;
                                                         if(grep( /^$match_var$/, @vars )){
                                                                           foreach (@lines)
                                                                       {
                                                                        if($_=~ /^$match_var\|VALUE = (.*)/ )
                                                                        {           
                                                                             my $replace_line=$_;
                                                                         print OUT "$replace_line";
                                                                            }
                                                                       }
                                                                       }
                                                    else{
                                                            s/\$CELL/$cell_name/g;
                                                            print OUT;
                                                                }
                                                    }
                                    else{
                                             s/\$CELL/$cell_name/g;
                                             print OUT;
                                             }

                                        } 
                                 }
           close REF;
           close OUT;

        }


close USER_INPUT;

#########################################################################################################################################################################################

© www.soinside.com 2019 - 2024. All rights reserved.