我没有得到有关foreach循环? [英] What am I not getting about foreach loops?

查看:164
本文介绍了我没有得到有关foreach循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  foreach(@arr)
{
... 。
}



<$ (我的$ i = 0; $ i< @arr; $ i ++)
{
.....
}
pre>

在功能上是等效的。
然而,在我的所有代码中,每当我使用一个 foreach 循环时,遇到的问题在我更改为 循环。它总是与比较两件事的价值,通常与嵌套循环。



下面是一个例子:

$ p $ for(my $ i = 0; $ i< @files; $ i ++)
{
my $ sel;
foreach(@selected)
{
if(files [$ i] eq selected [$ _])
{
$ selected ='selected';


选项值= $文件[$ i] $ sel> $文件[$ i]< / option>

$ / code>

上面的代码位于cgi程序中的选择标签之间。
基本上我正在编辑选择框的内容根据用户的规格。
但是在添加或删除选项之后,我希望选中的选项保持选中状态。

上面的代码在重组下一个表单中的select时应该完成这个工作。但是,使用 foreach 版本,只会得到所选的第一个选项,然后跳过其余部分。如果我将它切换到循环的三部分,而不更改其他任何东西,它将按预期工作。


@files 是一个文件名列表。



在下面的代码中, $ i 是数组 (即它是一个整数):

  for(my $ i = 0; $ i< @files; $ i ++){...} 
$ i
被依次设置给每个数组 item 。它是一个文件名):

  foreach my $ i(@files){...} 


$ b

例如:

 使用严格; 
使用警告;

my @files =(
'foo.txt',
'bar.txt',
'baz.txt',
);

printfor ... \\\
;
(my $ i = 0; $ i< @files; $ i ++){
print\ $ i is $ i.\\\
;
}

printforeach ... \\\
;
foreach我的$ i(@files){
print\ $ i是$ i.\\\
;





产生以下输出:

  for ... 
$ i是0.
$ i是1.
$ i是2.
foreach。 ..
$我是foo.txt。
$ i是bar.txt。
$我是baz.txt。

foreach 数组以避免(类似引起的意外错误。对于(my $ i = 0; $ i <= @ arr; ...)



也就是说, for foreach 实际上是作为Perl中的同义词来实现的,所以下面的脚本产生了与我前面的例子相同的输出:

  use strict; 
使用警告;

my @files =(
'foo.txt',
'bar.txt',
'baz.txt',
);

printfor ... \\\
;
foreach(my $ i = 0; $ i< @files; $ i ++){
print\ $ i is $ i.\\\
;
}

printforeach ... \\\
;
为我$(@files){
打印\ $ i是$ i.\\\
;



$ b

它只是习惯于把第二种类型的循环称为 foreach 循环,即使源代码使用关键字来执行循环(这已经变得非常常见了) p>

It was always my understanding that

foreach (@arr)
{
    ....
}

and

for(my $i=0; $i<@arr; $i++)
{
    .....
}

were functionally equivalent. However, in all of my code, whenever I use a foreach loop I run into problems that get fixed when I change to a for loop. It always has to do with comparing the values of two things, usually with nested loops.

Here is an example:

for(my $i=0; $i<@files; $i++)
{
    my $sel;
    foreach (@selected)
    {
        if(files[$i] eq selected[$_])
        {
            $selected='selected';
        }
    }
    <option value=$Files[$i] $sel>$files[$i]</option>
}

The above code falls between select tags in a cgi program. Basically I am editing the contents of a select box according to user specifications. But after they add or delete choices I want the choices that were origionally selected to remain selected.

The above code is supposed to accomplish this when reassembling the select on the next form. However, with the foreach version it only gets the first choice that's selected and skips the rest. If I switch it to a 3 part for loop, without changing anything else, it will work as intended.

This is only a recent example, so clearly I am missing something here, can anyone help me out?

解决方案

Let's assume that @files is a list of filenames.

In the following code, $i is the array index (i.e. it's an integer):

for (my $i=0; $i<@files; $i++) { ... }

In the following code, $i is set to each array item in turn (i.e. it's a filename):

foreach my $i (@files) { ... }

So for example:

use strict;
use warnings;

my @files = (
   'foo.txt',
   'bar.txt',
   'baz.txt',
);

print "for...\n";
for (my $i=0; $i<@files; $i++) {
   print "\$i is $i.\n";
}

print "foreach...\n";
foreach my $i (@files) {
   print "\$i is $i.\n";
}

Produces the following output:

for...
$i is 0.
$i is 1.
$i is 2.
foreach...
$i is foo.txt.
$i is bar.txt.
$i is baz.txt.

foreach loops are generally preferred for looping through arrays to avoid accidental off-by-one errors caused by things like for (my $i=1;...;...) or for (my $i=0;$i<=@arr;...).

That said, for and foreach are actually implemented as synonyms in Perl, so the following script produces identical output to my previous example:

use strict;
use warnings;

my @files = (
   'foo.txt',
   'bar.txt',
   'baz.txt',
);

print "for...\n";
foreach (my $i=0; $i<@files; $i++) {
   print "\$i is $i.\n";
}

print "foreach...\n";
for my $i (@files) {
   print "\$i is $i.\n";
}

It it simply customary to refer to the second type of loop as a foreach loop, even if the source code uses the keyword for to perform the loop (as has become quite common).

这篇关于我没有得到有关foreach循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆