【问题标题】:passing Array by reference perl通过引用传递数组 perl
【发布时间】:2020-03-09 04:04:52
【问题描述】:

当 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;
}

【问题讨论】:

  • use strict;use warnings; 开始,以及像 Perl Tidy 这样的工具来修复缩进。
  • 还可以先添加一些空格并清理缩进 :-)
  • @Quentin tidy 未能使其整洁。我会尝试其他方式...我需要按照 Dave cross 的评论手动进行
  • 总是使用use strict; use warnings;。并停止尝试清除您的变量来弄乱您的代码。特别是,undef $var; my $var 毫无意义。它清除了一个名为$var 的(未使用的)包变量,然后创建一个新的完全不相关的同名变量。
  • @ikegami 感谢您的建议。undef 被用作清除变量以避免任何副本的尝试。上面的代码是我 5 年前作为爱好开始 perl 编码时所做的一个大代码的一部分。在对完整的代码进行严格处理并修复它们之后,代码进入了无限循环。无论如何,这个 sn-p 现在按预期工作。

标签: arrays perl pass-by-reference


【解决方案1】:

感谢您的指导。严格和警告有所帮助。实际问题是“我的 $_petArray;”必须是“我的 %_petArray;”。我将它用作变量而不是声明为数组。

【讨论】:

  • 我将它用作变量而不是声明为数组。 呃……这没有意义。 (我认为)您的意思是您将其声明为标量变量,但您应该将其声明为哈希变量。
猜你喜欢
  • 1970-01-01
  • 2011-08-09
  • 2015-12-23
  • 2020-07-30
  • 2012-04-17
  • 2014-08-06
  • 2019-06-29
  • 2015-07-04
相关资源
最近更新 更多