Jas*_*ash 2 arrays perl for-loop
我知道这个问题相当含糊,但我希望解释的空间可以帮助解决问题,这是我整天都在捣乱我的大脑,并且通过搜索找不到任何建议.
基本上我有一个数组@cluster,我试图使用迭代器$ x跳过位于该数组中的值.这个数组的大小会有所不同,所以我不能只是(相当恶劣地)使if语句不幸地适合所有情况.
通常,当我需要使用标量值执行此操作时,我只需执行以下操作:
for my $x (0 .. $numLines){
if($x != $value){
...
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
你可以做:
my @cluster = (1,3,4,7);
outer: for my $x (0 .. 10){
$x eq $_ and next outer for @cluster;
print $x, "\n";
}
Run Code Online (Sandbox Code Playgroud)
使用Perl 5.10,您还可以:
for my $x (0 .. 10){
next if $x ~~ @cluster;
print $x, "\n";
}
Run Code Online (Sandbox Code Playgroud)
或者更好地使用哈希:
my @cluster = (1,3,4,7);
my %cluster = map {$_, 1} @cluster;
for my $x (0 .. 10){
next if $cluster{$x};
print $x, "\n";
}
Run Code Online (Sandbox Code Playgroud)