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

发布于 2024-10-01 20:36:14 字数 818 浏览 1 评论 0原文

所以,我有一个哈希值 %HoHoA。 每个顶级哈希键都有一个二级哈希键作为值。 每个二级哈希都具有数组数组作为值。

在 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'

我试图获取每个套件的两个数组的大小。在上面的 例如,Second_Key_2 有两个数组,第 0 个数组的大小为 3。

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

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

收到一条错误消息,如下所示: 使用“严格引用”时无法使用字符串(“字符串 3”)作为 ARRAY 引用

为什么我会收到该错误消息,我应该做什么?

So, I have a hash %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:

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.

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

Why am I getting that error message, and what should I do instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十雾 2024-10-08 20:36:14

$HoHoA{$group}{$suite}[0] 不是 arrayref;它是一个字符串,因此会出现错误。也许您需要调试构建数据结构的代码。

$HoHoA{$group}{$suite}[0] isn't an arrayref; it's a string, thus the error. Maybe you need to debug the code that's building your data structure.

水中月 2024-10-08 20:36:14

在调试器下运行它,并使用 x 命令递归转储数据结构或指向其中。

您可以使用 Dumpvalue 模块以编程方式完成此操作,但不太方便。

我希望人们不要一直认为我写 perllol 是为了开玩笑。 ☺

Run it under the debugger and recursively dump out a data structure, or point therein, with the x command.

You can do that programmatically with the Dumpvalue module, but it’s much less convenient.

I wish people wouldn’t keep thinking I wrote perllol as a joke. ☺

2024-10-08 20:36:14

看起来你已经深入了一层。
下面的代码应该可以满足您的需求。

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

您可能还想使用 Data::Dumper 来查看
确保您正在处理写入数据格式的对象结构。
使用数据::转储器;
打印转储器($HoHoA);

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.
use Data::Dumper;
print Dumper($HoHoA);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文