【问题标题】:Why does this function show an array of an array instead of just an array?为什么这个函数显示一个数组而不是一个数组?
【发布时间】:2020-09-29 09:12:34
【问题描述】:
use warnings;
use strict;

testfunc();
sub testfunc {
    my @first_pin_name_list=qw(
        VDD2_DDR2_S2_4
        VDD1_DDR2_S2_2
    );

    my @second_pin_name_list=qw(
        VDD2_DDR2_S2_4
        VDD1_DDR2_S2_2
    );
    my @expected_list =qw(
        VDD1_DDR0_S2_[2:1]
        VDD2_DDR0_S2_[5:1]
    );

    my @listoftests = ( 
        {INPUT_LIST => \@first_pin_name_list,OUTPUT_LIST => \@expected_list,OK_2_FAIL=> 0}, 
        {INPUT_LIST => \@second_pin_name_list,OUTPUT_LIST => \@expected_list,OK_2_FAIL => 1}

    );  

    print @expected_list;

    # should show an array but instead debugger shows an array of an array
    my @listtotest = $listoftests[0] -> {INPUT_LIST};
    print "hello";
    return @listoftests;
}

调试器显示 @listtotest 包含一个数组的数组,但我只想查看一个包含元素的数组。如何更改代码以仅显示元素数组?

【问题讨论】:

  • 我猜调试器正在向您显示\@listtotest,所以这是顶级参考。其他的,它会分别处理每个元素。

标签: arrays perl reference


【解决方案1】:

您没有向我们展示调试器向您展示的内容,但没有数组数组。

$listoftests[0]->{INPUT_LIST} 是对数组@first_pin_name_list 的引用。 如果您想将该数组中的元素分配给@listtotest,您需要取消对它的引用:

my @listtotest = $listoftests[0]->{INPUT_LIST}->@*;

或在较旧的 perls 上:

my @listtotest = @{ $listoftests[0]->{INPUT_LIST} };

【讨论】:

  • 抱歉,如何在 SO 上添加图片?我想知道以供将来参考。我试试这个。这可能会解决我的问题。
  • 不能从调试器复制粘贴吗?虽然我看过图片,但我不知道如何找到图片不是很有用
  • 啊好的。我可以从调试器中复制,只是觉得这里看起来不干净。
  • 这里是让事情看起来干净的帮助stackoverflow.com/editing-help
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
相关资源
最近更新 更多