<数据>阻止 foreach 循环执行,为什么? :)数据>
我有两个嵌套的 foreach 循环。如果我使用此代码:
foreach (@directories) {
my $actual_directory = $_;
print "\nactual directory: ".$actual_directory."\n";
foreach (@files) {
my $file_name = $_;
my $actual_file = $actual_directory.$file_name;
print $actual_file."\n";
open(DATA, $actual_file) or die "Nelze otevřít zdrojový soubor: $!\n";
my $line_number = 0;
# while (<DATA>){
# my @znaky = split(' ',$_);
# my $poradi = $znaky[0]; #poradi nukleotidu
# my $hodnota = $znaky[1]; #hodnota
# my @temp = $files_to_sum_of_lines{$actual_file};
# $temp[$line_number] += $hodnota;
# $files_to_sum_of_lines{$actual_file} = @temp;
# $line_number+=1;
# }
# close(DATA);
}
}
我得到以下输出:
actual directory: /home/n/Plocha/counting_files/1/
/home/n/Plocha/counting_files/1/a.txt
/home/n/Plocha/counting_files/1/b.txt
actual directory: /home/n/Plocha/counting_files/2/
/home/n/Plocha/counting_files/2/a.txt
/home/n/Plocha/counting_files/2/b.txt
但是,如果我取消注释“while (){ }
”,我会丢失 a.txt 和 b.txt,因此输出看起来像this:
actual directory: /home/n/Plocha/counting_files/1/
/home/n/Plocha/counting_files/1/a.txt
/home/n/Plocha/counting_files/1/b.txt
actual directory: /home/n/Plocha/counting_files/2/
/home/n/Plocha/counting_files/2/
/home/n/Plocha/counting_files/2/
这个 while ()
如何阻止我的 foreach 被执行? 任何帮助将不胜感激。多谢。
I have two nested foreach loops. If I use this code:
foreach (@directories) {
my $actual_directory = $_;
print "\nactual directory: ".$actual_directory."\n";
foreach (@files) {
my $file_name = $_;
my $actual_file = $actual_directory.$file_name;
print $actual_file."\n";
open(DATA, $actual_file) or die "Nelze otevřít zdrojový soubor: $!\n";
my $line_number = 0;
# while (<DATA>){
# my @znaky = split(' ',$_);
# my $poradi = $znaky[0]; #poradi nukleotidu
# my $hodnota = $znaky[1]; #hodnota
# my @temp = $files_to_sum_of_lines{$actual_file};
# $temp[$line_number] += $hodnota;
# $files_to_sum_of_lines{$actual_file} = @temp;
# $line_number+=1;
# }
# close(DATA);
}
}
I got this output:
actual directory: /home/n/Plocha/counting_files/1/
/home/n/Plocha/counting_files/1/a.txt
/home/n/Plocha/counting_files/1/b.txt
actual directory: /home/n/Plocha/counting_files/2/
/home/n/Plocha/counting_files/2/a.txt
/home/n/Plocha/counting_files/2/b.txt
However, if I uncomment "while (<DATA>){ }
", I loose a.txt and b.txt, so the output looks like this:
actual directory: /home/n/Plocha/counting_files/1/
/home/n/Plocha/counting_files/1/a.txt
/home/n/Plocha/counting_files/1/b.txt
actual directory: /home/n/Plocha/counting_files/2/
/home/n/Plocha/counting_files/2/
/home/n/Plocha/counting_files/2/
How can this while (<DATA>)
prevent my foreach from being executed?
Any help will be appreciated. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了不使用 DATA 之外,还可以尝试使用词法循环变量和词法文件句柄。此外,Perl 的内置
$.
会为您跟踪行号。另一方面,我也不太清楚其中是否存在逻辑错误。像下面这样的东西可能会有用:
In addition to not using
DATA
, try using lexical loop variables, and lexical filehandles. Also, Perl's built-in$.
keeps track of line numbers for you.On the other hand, I can't quite tell if there is a logic error in there. Something like the following might be useful:
Perl 使用
__DATA__
制作伪数据文件在包裹的最后。您可以使用文件句柄DATA
访问该文件,例如;
。您的文件句柄可能有冲突吗?尝试将文件句柄更改为其他内容,看看是否效果更好。Perl uses
__DATA__
to make a pseudo-data file at the end of the package. You can access that using the filehandleDATA
, e.g.<DATA>
. Is it possible that your filehandle is conflicting? Try changing the filehandle to something else and see if it works better.