代码问题是,当inputfile1的行数多于inputfile2时,petlist2_out的行数为petlist1_out。看起来通过引用传递的数组将起作用,并且被复制回以保留较旧的数组内容。请帮忙。该代码在所有其他情况下均能正常工作。除了使用undef之外,我不确定如何清洁数组和变量。使用引用时如何避免多次调用函数时使用相同的位置?
我不是Perl编码专家。
use File::Basename;
#use warnings;
if ($#ARGV != 1 )
{
print "usage: sort_petlist_extract.pl old-petlist new-petlist";
exit;
}
print "\n****** Reading petlist .....\n";
$inputfile1 = shift(@ARGV); #get text based IN filename
$inputfile2 = shift(@ARGV); #get text based IN filename
my $result = dirname $inputfile2;
print $result;
#my $filename = basename $filespec;
#$result = 'C:\\temp\\';
#my $inputfile=$ARGV[0];
open(log_file,"> ".$result.'\\'."debug.log");
open(diff_rpt_file,"> ".$result.'\\'."diff_report.txt");
print log_file $inputfile1."\n";
print log_file $inputfile2."\n";
open(petlist1,"< $inputfile1") or die "\nCouldn't open input file\n";
my @input_lines1 = <petlist1>;
close petlist1;
chomp @input_lines1;
#foreach(@input_lines1){print log_file $_."\n";}
my($petlist1_2d_ref)=extract_petlist_file(\@input_lines1);
my %petarray1 = %{$petlist1_2d_ref};
undef @input_lines1;
print log_file $petarray1_ref ."\n";
print scalar keys %petarray1;
open(petlist1_out,"> ".$result.'\\'."petlist1_out.txt");
#just test a display
for($i=0;$i<(scalar keys %petarray1);$i++)#just a display
{
print petlist1_out "${$petarray1{$i}}[0] \t ${$petarray1{$i}}[1] \n";
}
close petlist1_out;
undef %petarray1;
print log_file "###############################################################################first file copied to array#######################################\n";
print "####################first file copied to array########################\n";
open(petlist2,"< $inputfile2") or die "\nCouldn't open input file\n";
my @input_lines2 = <petlist2>;
close petlist2;
chomp @input_lines2;
#foreach(@input_lines2){print log_file $_."\n";}
undef $petlist2_2d_ref;
my($petlist2_2d_ref)=extract_petlist_file(\@input_lines2);
my %petarray2 = %{$petlist2_2d_ref};
#my($page_petname)=extract_page_petname(\@input_lines2);### page number extracttion
undef @input_lines2;
print log_file $petarray2_ref."\n";
print scalar keys %petarray2;
open(petlist2_out,"> ".$result.'\\'."petlist2_out.txt");
#just test a display
for($i=0;$i<(scalar keys %petarray2);$i++)#just a display
{
print petlist2_out "${$petarray2{$i}}[0] \t ${$petarray2{$i}}[1] \n";
}
close petlist2_out;
print log_file "###############################################################################second file copied to array #######################################\n";
print "####################second file copied to array ######################\n";
####################### sub routines
sub extract_petlist_file
{
undef $arr1;
my $arr1=$_[0];
undef @petlist;
my @petlist=@$arr1;
#foreach(@petlist){print log_file $_."\n";}
my $temp1='';
my $temp2='';
undef @temp;#clear 1D array
my @temp;
undef $_petarray;
my $_petarray;
undef $i;
print log_file scalar @petlist."\n";
my $k=0;
my $pattern1='PET_NAME';
for(my $i=0;$i<(scalar @petlist);$i++)
{
#print "$i\n";
#print log_file $_;
$_=$petlist[$i];
if ((m/$pattern1/) && $combine_flag eq 0)# first time ever in the loop
{
}
}
#store the last pet connection to array
@temp=($temp1,$temp2);
$_petarray{$k}=[@temp];
undef @temp;
undef $temp1;
undef $temp2;
return \%_petarray;
}