简体   繁体   English

Perl中'for'和'foreach'之间有什么区别?

[英]What's the difference between 'for' and 'foreach' in Perl?

I see these used interchangeably. 我看到这些可以互换使用。 What's the difference? 有什么不同?

There is no difference. 没有区别。 From perldoc perlsyn : 来自perldoc perlsyn

The foreach keyword is actually a synonym for the for keyword, so you can use foreach for readability or for for brevity. foreach关键字实际上是对一个同义词for关键字,所以你可以使用foreach的可读性或for为简洁。

I see these used interchangeably. 我看到这些可以互换使用。

There is no difference other than that of syntax. 除语法之外没有区别。

Four letters. 四个字母。

They're functionally identical, just spelled differently. 它们在 功能上 完全相同,只是拼写不同。

Ever since its introduction in perl-2.0 , foreach has been synonymous with for . 自从它在perl-2.0中推出以来, foreach一直是for代名词。 It's a nod to the C shell's foreach command. 这是对C shell的foreach命令的点头。

In my own code, in the rare case that I'm using a C-style for-loop, I write 在我自己的代码中,在极少数情况下,我正在使用C风格的for循环,我写道

for (my $i = 0; $i < $n; ++$i)

but for iterating over an array, I spell out 但是为了迭代一个数组,我拼出来了

foreach my $x (@a)

I find that it reads better in my head that way. 我发现它在我的头脑中读得更好。

From http://perldoc.perl.org/perlsyn.html#Foreach-Loops 来自http://perldoc.perl.org/perlsyn.html#Foreach-Loops

The foreach keyword is actually a synonym for the for keyword, so you can use either. foreach关键字实际上是for关键字的同义词,因此您可以使用其中任何一个。 If VAR is omitted, $_ is set to each value. 如果省略VAR,则为每个值设置$ _。

# Perl's C-style
for (;;) {
    # do something
}

for my $j (@array) {
    print $j;
}

foreach my $j (@array) {
    print $j;
}

However: 然而:

If any part of LIST is an array, foreach will get very confused if you add or remove elements within the loop body, for example with splice. 如果LIST的任何部分是一个数组,如果在循环体内添加或删除元素,foreach将会非常困惑,例如使用splice。 So don't do that. 所以不要这样做。

The foreach keyword is actually a synonym for the for keyword, so you can use foreach for readability or for for brevity. foreach关键字实际上是for关键字的同义词,因此您可以将foreach用于可读性或简洁性。 (Or because the Bourne shell is more familiar to you than csh, so writing for comes more naturally.) If VAR is omitted, $_ is set to each value. (或者因为Bourne shell比csh更熟悉,所以写作更自然。)如果省略VAR,则为每个值设置$ _。

There is a subtle difference ( http://perldoc.perl.org/perlsyn.html#Foreach-Loops ) : 有一个微妙的区别( http://perldoc.perl.org/perlsyn.html#Foreach-Loops ):

The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. foreach循环遍历正常列表值,并依次将变量VAR设置为列表的每个元素。 If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. 如果变量前面带有关键字my,则它是词法范围的,因此仅在循环中可见。 Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. 否则,该变量隐含在循环的本地,并在退出循环时重新获得其前一个值。 If the variable was previously declared with my, it uses that variable instead of the global one, but it's still localized to the loop. 如果先前使用my声明了变量,它将使用该变量而不是全局变量,但它仍然本地化为循环。 This implicit localization occurs only in a foreach loop. 这种隐式定位仅在foreach循环中发生。

This program : 这个程序:

#!/usr/bin/perl -w

use strict;

my $var = 1;
for ($var=10;$var<=10;$var++) {
  print $var."\n"; # print 10
  foo(); # print 10
}

print $var."\n"; # print 11    

foreach $var(100) {
  print $var."\n"; # print 100
  foo(); # print 11 !
}

sub foo {
  print $var."\n";
}

will produce that : 将产生:

10
10
11
100
11

In case of the "for" you can use the three steps. 如果是“for”,您可以使用这三个步骤。

1) Initialization 2) Condition Checking 3) Increment or decrement 1)初始化2)条件检查3)递增或递减

But in case of "foreach" you are not able increment or decrement the value. 但是在“foreach”的情况下,您无法增加或减少该值。 It always take the increment value as 1. 它总是将增量值取为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM