迭代 Perl 集合,计算新值

发布于 2024-10-06 15:29:50 字数 611 浏览 0 评论 0原文

我有一个 Perl 脚本,其中包含以下行:

print $_->{label} || $_->{irq}, "=", $_->{count}, "; " for @irqs;

其中 @irqs 是某种集合(我不是 Perl 程序员),它被 push 到其他位置程序。

我有另一个哈希集合,其中键等于 $_->{label}$_->{irq} 的值来自@irqs集合。

我不想在上面的语句中打印 $_->{count} ,而是打印 $_->{count} - X where X 是哈希值。

我确信我可以通过手动迭代 @irqs 并按下 $_->{label}$_->{ irq} 到具有计算值的新集合上,但是,有更好的方法吗?

正如我所说,我不是 Perl 程序员,所以我只是想在开始之前确保我走的是正确的道路......

I have a Perl script which has the following line in it:

print $_->{label} || $_->{irq}, "=", $_->{count}, "; " for @irqs;

Where @irqs is some kind of collection (I am not a Perl programmer) that is pushed to elsewhere in the program.

I have another collection which is a hash, where the key is equal to either the value of $_->{label}or $_->{irq} from the collection @irqs.

Instead of printing $_->{count} in the above statement, I would like to print $_->{count} - X where X is the value from the hash.

I am sure that I can do this by iterating through @irqs by hand and pushing either $_->{label}or $_->{irq} onto a new collection with the calculated value, but, is there a better way to do it?

As I said, I'm not a Perl programmer, so I just wanted to make sure I was going down the correct path before starting out...

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

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

发布评论

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

评论(1

你爱我像她 2024-10-13 15:29:50

您不需要复制/存储任何其他数据即可执行此操作。

foreach my $irq (@irqs) {
    my $key = $irq->{label} || $irq->{irq};
    print $key, "=", $irq->{count} - $myhash{$key}, "; ";
}

You don't need to copy/store any additional data to do this.

foreach my $irq (@irqs) {
    my $key = $irq->{label} || $irq->{irq};
    print $key, "=", $irq->{count} - $myhash{$key}, "; ";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文