perl - 如何在Perl中同时遍历多个列表?

标签 perl arrays list

我需要创建一个文本文件(aptest.s),我可以使用它来读入另一个程序。我使用Perl是因为我有很多工作要做。我的代码如下(未提供所需的输出-在代码和实际输出后显示)。任何帮助,将不胜感激。

#!/usr/bin/perl -w
chdir("D://projects//SW Model ODME");
@link = ("319-116264||319-118664","320-116380||320-116846","321-119118||321-119119","322-115298||322-119087");
@link1 = ("116264-319||118664-319","116380-320||116846-320","119118-321||119119-321","115298-322||119087-322");
open (FSAS, ">>aptest.s");
foreach $link (@link) {
    foreach $link1 (@link1){
    print FSAS "other code \n";
    print FSAS "PATHLOAD SELECTLINK=(Link=".$link."), VOL[2]=MW[1] \n";
    print FSAS "PATHLOAD SELECTLINK=(Link=".$link1."), VOL[3]=MW[2] \n";
    print FSAS "other code \n";
}
}

实际输出:
other output
PATHLOAD SELECTLINK=(Link=319-116264||319-118664), VOL[2]=MW[1] 
PATHLOAD SELECTLINK=(Link=116264-319||118664-319), VOL[3]=MW[2] 
other output 

other output
PATHLOAD SELECTLINK=(Link=**319-116264||319-118664**), VOL[2]=MW[1] 
PATHLOAD SELECTLINK=(Link=**116380-320||116846-320**),      VOL[3]=MW[2] 
other output

所需的输出
other output
PATHLOAD SELECTLINK=(Link=319-116264||319-118664), VOL[2]=MW[1] 
PATHLOAD SELECTLINK=(Link=116264-319||118664-319), VOL[3]=MW[2] 
other output

other output
PATHLOAD SELECTLINK=(Link=**320-116380||320-116846**), VOL[2]=MW[1] 
PATHLOAD SELECTLINK=(Link=**116380-320||116846-320**), VOL[3]=MW[2] 
other output

最佳答案

参见List::MoreUtils中的 each_array :

#!/usr/bin/perl

use strict;
use warnings;

use List::MoreUtils qw( each_array );

my @x = qw( A B C D E F );
my @y = (10, 11, 12, 13, 14, 15);

my $it = each_array( @x, @y );
while ( my ($x, $y) = $it->() ) {
    print "$x = $y\n";
}
__END__

关于perl - 如何在Perl中同时遍历多个列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/822563/

相关文章:

python - 检查字符串的任何字符是否在列表中

regex - 中间带有可变字符的负向后视正则表达式 (PERL)

python - 根据颜色值将多维 Numpy 数组转换为二维数组

regex - 如何使用 Perl 解析 runmqsc 命令输出?

javascript - 将js数组转换为字典映射

c++ - 如何在可变大小的类中创建指针数组?

Python - 检查字母是否在列表中

python - 按 x 然后按 y 对字典列表进行排序

multithreading - Perl:特殊变量线程安全吗?

linux - 如何在 Unix/Linux 中提取两个括号之间的单词?