gpt4 book ai didi

arrays - Perl 中迭代循环并提取对或三元组的最佳方法是什么

转载 作者:行者123 更新时间:2023-12-04 11:17:21 27 4
gpt4 key购买 nike

我有一个平面坐标数组,我想迭代和提取 x 和 y 坐标对。相同的逻辑可以应用于 RGB 颜色的三元组。这是我到目前为止所拥有的,但它并没有感觉 super 灵活或优雅。

my @coords = qw(1 5 2 6 3 8 6 12 7 5);

for (my $i = 0; $i < @coords; $i += 2) {
my $x = $coords[$i];
my $y = $coords[$i+1];

print "$x, $y\n";
}
必须有更好的方法来做到这一点吗?

最佳答案

模块List::MoreUtilsnatatime (一次一次)

use List::MoreUtils qw(natatime);

my @ary = 1..12;

my $it = natatime 3, @ary; # iterator

while (my @triplet = $it->()) { say "@triplet" }

关于arrays - Perl 中迭代循环并提取对或三元组的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67896519/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com