如何获取列表以显示 Perl 中空条目的零? [英] How to get the a list to show zeros for empty entries in Perl?

查看:51
本文介绍了如何获取列表以显示 Perl 中空条目的零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初,我正在处理一个长度为 2^16 的列表.但是,为了抽象这一点,我将在此示例中设置 length = 5.

Originally, I am working with a list with length = 2^16. However, to abstract this, I will set length = 5 in this example.

#subroutine to make undefined entries -> 0
sub zeros {
    foreach(@_) {
        if(!defined($_))    {
            $_ = 0;
        }
    }
}       
 #print out and indicies and elements of list 
    sub checking {
        print "List = \n";
        my $counter = 0;
        foreach (@_) { 
            print "index = $counter\n";
            print "$_\n";
            $counter += 1;
        }
            print "\n";
    }

方法 1:如果我访问不同的索引来编辑元素,则在打印出数组时会得到以下信息.我不想看到空白.我希望它们为 0.我已经设置了一个子程序zeros"来使未定义的条目变为零.但我不知道我的代码出了什么问题.我还为列表的每个元素尝试了$_ += 0".我仍然无法为空条目获得零.

Method 1: If I access different indices to edit the element, I get the following when I print out the arrays. I dont want to see blank. I want them to be 0. I have already set up a subroutine "zeros" to make undefined entries become zero. But I dont know what went wrong in my code. I have also tried "$_ += 0" for each elements of the list. I still wasnt able to get zeros for empty entries.

#method 1
@abc = ();
$abc[1] = 3;
$abc[5] = 5;
&zeros(@abc);
&checking(@abc);
List = 
index = 0

index = 1
3
index = 2

index = 3

index = 4

index = 5
5

方法 2:如果我像这样初始化列表,我可以得到零.但是正如我所说,我正在处理很长的列表,我绝对不能像这样初始化我的列表.

And method 2: I can get zeros if I initialise the list like this. But as I said, I am working with very long list, I cannot definitely not initialise my list like this.

#method 2
@abc = (3,0,0,0,5);
&checking(@abc);

List = 
index = 0
3
index = 1
0
index = 2
0
index = 3
0
index = 4
5

推荐答案

您的方法是正确的,但是您的 zeros() 函数存在问题.您使用 @abc 作为参数调用它,这会生成该列表的副本.然后更改副本.在 sub 结束时,该副本将被丢弃.在您的 checking() 函数中,您仍在使用原始列表.

Your approach is correct, but there's an issue with your zeros() function. You are calling it with @abc as a parameter, which makes a copy of that list. You then change the copy. At the end of the sub, that copy is discarded. In your checking() function, you are still using the original list.

你可以这样修复它:

sub zeros {
  my @list = @_;
  @list = map { $_ // 0 } @list;
  return @list;
} 

@abc = zeros(@abc);
checking(@abc);

诀窍是返回更改后的列表并将其重新分配给原始变量.

The trick is to return the altered list and reassign it to the original variable.

如果你使用了 strictwarnings 它会告诉你:

If you had used strict and warnings it would have told you about that:

Use of uninitialized value $_ in concatenation (.) or string at F:\scratch.pl line 28. List =  index = 0

index = 1 3 index = 2

index = 3

index = 4

index = 5 5

Use of uninitialized value $_ in concatenation (.) or string at F:\scratch.pl line 28. 
Use of uninitialized value $_ in concatenation (.) or string at F:\scratch.pl line 28. 
Use of uninitialized value $_ in concatenation (.) or string at F:\scratch.pl line 28.

<小时>

但是由于您正在处理一个非常大的数组,我建议您改用数组引用,因为复制会很昂贵.


But since you are dealing with a very big array, I would advise to use an array reference instead because the copying will be expensive.

sub zeros {
  $_ //= 0 for @{ $_[0] };
} 

zeros(\@abc);
checking(@abc);

这篇关于如何获取列表以显示 Perl 中空条目的零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆