简体   繁体   English

Perl:散列中的数组大小,在另一个散列中

[英]Perl: size of array in a hash, in another hash

So, I have a hash %HoHoA. 因此,我有一个哈希%HoHoA。 Each top level hash key has a second level hash key as a value. 每个顶级哈希键都有一个第二级哈希键作为值。 Each second level hash has arrays-of-arrays as values. 每个第二级哈希将数组数组作为值。

In perl's debugger it looks something like this: 在perl的调试器中,它看起来像这样:

0 'Top_key_1'
1 HASH(0x...)
  'Second_Key_1' => ARRAY(0x...)
    0   'string 1'
    1   'string 2'
  'Second_Key_2' => ARRAY(0x...)
    0  ARRAY(0x...)
      0 'string 3'
      1 'string 4'
      2 'string 5'
    1  ARRAY(0x...)
      0 'string 6'
      1 'string 7'
2 'Top_key_2'

I'm trying to get the size of each suite's two arrays. 我正在尝试获取每个套件的两个数组的大小。 In the above example, Second_Key_2 has two arrays, the 0th one is size 3. 在上面的示例中,Second_Key_2有两个数组,第0个是大小3。

my $count1 = $#{$HoHoA{$top_key}{$second_key}[0]}+1;
my $count2 = $#{$HoHoA{$top_key}{$second_key}[1]}+1;

and

my $count1 = @{$HoHoA{$group}{$suite}[0]};
my $count2 = @{$HoHoA{$group}{$suite}[1]};

I get an error message like: Can't use string ("string 3") as an ARRAY ref while "strict refs" in use 我收到类似以下错误消息:在使用“严格引用”时,不能将字符串(“字符串3”)用作阵列引用

Why am I getting that error message, and what should I do instead? 为什么会收到该错误消息,我该怎么办?

$HoHoA{$group}{$suite}[0] isn't an arrayref; $HoHoA{$group}{$suite}[0]不是arrayref; it's a string, thus the error. 它是一个字符串,因此是错误。 Maybe you need to debug the code that's building your data structure. 也许您需要调试构建数据结构的代码。

Run it under the debugger and recursively dump out a data structure, or point therein, with the x command. 在调试器下运行它,并使用x命令以递归方式转储数据结构或指向其中。

You can do that programmatically with the Dumpvalue module, but it's much less convenient. 您可以使用Dumpvalue模块以编程方式执行此Dumpvalue ,但它不太方便。

I wish people wouldn't keep thinking I wrote perllol as a joke. 我希望人们不要一直以为我把perllol写成一个笑话。

It look like you went one level too deep. 看来您太深了一层。 The code below should get you what you want 下面的代码可以为您提供所需的代码

my $count1 = @{$HoHoA{$group}{$suite}};

You may also may want to use Data::Dumper to see the Structure of your object to ensure you are working on the write data format. 您可能还需要使用Data :: Dumper来查看对象的结构,以确保您正在使用写数据格式。 use Data::Dumper; 使用Data :: Dumper; print Dumper($HoHoA); 打印垃圾站($ HoHoA);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM