如何在 foreach 循环中刷新数组?

发布于 2024-08-04 07:21:29 字数 1413 浏览 2 评论 0原文

我正在编写一个 Perl 脚本来对哈希值进行一些数学运算。该哈希值包含以下示例中给出的值。我写了下面的代码。如果我在不使用 foreach 循环的情况下单独对数组值执行此代码,则输出很好。但是,如果我对数组值使用 foreach 循环运行此操作,则 A 中的值的总和很好,但从 B 中输出会添加先前的值。

Hash Sample:

$VAR1 = 'A';
$VAR2 = {
    '"x"' => [values],
    '"y"' => [values],
    and so on...
$VAR3 = 'B';
$VAR4 = {
        '"x"' => [values],
    '"y"' => [values],
    and so on...
$VARn....

代码:

#!/usr/bin/perl -w
use strict;
use List::Util qw(sum);

my @data;
my @count;
my $total;

my @array = ("A", "B", "C", "D");

foreach my $v (@array) {

        my %table = getV($v); #getV is a subroutine returing a hash. 
        for my $h (sort keys %table) {
                  for my $et (sort keys %{ $table{$h} } ) {
        for $ec ($table{$h}{$et}) {
                            push @data, $ec;
                            @count = map { sum(@{$_}) } @data;
                            $total = sum(@count);
                        }
           }
print "sum of $v is  $total\n";
}

我认为问题出在这一行。它存储所有以前的值,因此在下一个 foreach 循环中添加所有值。

push @data, $ec;

所以,这里我有两个问题:

1)如何在每次 foreach 循环迭代中刷新数组(@data)?

2) 如何添加数组 ref ($ec) 中的值并将它们存储在数组中?因为当我使用以下代码时:

for $ec ($table{$h}{$et}) {
    @count = map { sum(@{$_}) } @$ec;
    $total = sum(@count);
}

输出为我提供了相同的 @count 和 $total 值。

请给我提供建议。

I am writing a Perl script to do some mathematical operations on a hash. This hash contains the values as given in the sample below. I have written the code below. If I execute this code for an array value separately without using a foreach loop, the output is fine. But if I run this using a foreach loop on the array values, the sum for values in A are good, but from B the output add the previous values.

Hash Sample:

$VAR1 = 'A';
$VAR2 = {
    '"x"' => [values],
    '"y"' => [values],
    and so on...
$VAR3 = 'B';
$VAR4 = {
        '"x"' => [values],
    '"y"' => [values],
    and so on...
$VARn....

Code:

#!/usr/bin/perl -w
use strict;
use List::Util qw(sum);

my @data;
my @count;
my $total;

my @array = ("A", "B", "C", "D");

foreach my $v (@array) {

        my %table = getV($v); #getV is a subroutine returing a hash. 
        for my $h (sort keys %table) {
                  for my $et (sort keys %{ $table{$h} } ) {
        for $ec ($table{$h}{$et}) {
                            push @data, $ec;
                            @count = map { sum(@{$_}) } @data;
                            $total = sum(@count);
                        }
           }
print "sum of $v is  $total\n";
}

I think the issue is with this line. It is storing all the previous values and hence adding all the values in next foreach loop.

push @data, $ec;

So, here I have two issues:

1) How can I refresh the array (@data) in each foreach loop iteration?

2) How can I add the values in the array ref ($ec) and store them in an array? Because when I use the following code:

for $ec ($table{$h}{$et}) {
    @count = map { sum(@{$_}) } @$ec;
    $total = sum(@count);
}

The output gives me the same values for @count and $total.

Please provide me with suggestions.

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

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

发布评论

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

评论(2

や莫失莫忘 2024-08-11 07:21:29

如果我理解正确的话,只需对您的代码进行一点小小的更改即可。在 for 循环的开头创建一个空数组 (@data)。希望这有帮助。

        for my $h (sort keys %table) {
                  my @data;

If I understand you correctly, just a small change in your code. Make an empty array (@data) at the beginning of for loop. Hope this helps.

        for my $h (sort keys %table) {
                  my @data;
嘿看小鸭子会跑 2024-08-11 07:21:29

1) 在循环体的顶部声明 @data 数组,您希望从一个新的空数组开始。或者也许您的意思是说@data = @$ec,而不是push @data, $ec

2) 要添加 $ec 引用的数组中的值,只需输入 sum(@$ec);不需要地图。

目前尚不完全清楚您的数据结构是什么或您想用它做什么。
这将有助于了解示例 %table 的外观以及您期望从中得到的结果。

1) Declare the @data array at the top of the loop body where you want to start with a fresh, empty array. Or maybe you mean to be saying @data = @$ec, not push @data, $ec?

2) To add the values in the array referred to by $ec, you would just say sum(@$ec); no map required.

It's not completely clear what your data structure is or what you are trying to do with it.
It would help to see what a sample %table looks like and what results you expect from it.

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