是否有可能分配在Perl foreach循环两个变量? [英] Is it possible to assign two variables in Perl foreach loop?
本文介绍了是否有可能分配在Perl foreach循环两个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能从一个Perl foreach循环数组指定两个变量相同的数据?
我使用Perl 5,我觉得我在Perl 6。
对面传来的东西事情是这样的:
我的$ VAR1;
我的$ VAR2;的foreach $ VAR1,$ VAR2(@array){...}
解决方案
这不是用Perl 5核心语言,但列表: :的Util有一个对
功能,它应该是非常接近(和一些其他对...
这可能是更方便,这取决于你在循环里做什么)功能:
#!的/ usr / bin中/ perl的包膜使用严格的;
使用警告;
用5.010;使用List ::的Util'对';我@list = QW(A 1 B 2 C 3);我对$(对@list){
我($第一,第二$)= @ $对;
说$第一=> $第二个;
}
输出:
A => 1
B => 2
C => 3
Is it possible to assign two variables the same data from an array in a Perl foreach loop?
I am using Perl 5, I think I came across something in Perl 6.
Something like this:
my $var1;
my $var2;
foreach $var1,$var2 (@array){...}
解决方案
It's not in the Perl 5 core language, but List::Util has a pairs
function which should be close enough (and a number of other pair...
functions which may be more convenient, depending on what you're doing inside the loop):
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use List::Util 'pairs';
my @list = qw(a 1 b 2 c 3);
for my $pair (pairs @list) {
my ($first, $second) = @$pair;
say "$first => $second";
}
Output:
a => 1
b => 2
c => 3
这篇关于是否有可能分配在Perl foreach循环两个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文