是否有可能检测当前的while循环迭代是否是perl中的最后一个? [英] Is it possible to detect if the current while loop iteration is the last in perl?
问题描述
我从没想过这是可能的,但是读了一些矛盾的评论并以为我会问专家.
I never thought this was possible, but read some conflicting comments and thought I would ask the experts.
如果我正在执行一个while循环,该循环正在逐行读取文件,那么如果当前迭代将是该循环中的最终迭代,是否可以执行一些代码?我知道我可以简单地将这段代码放在while循环之后,以便代码可以在最后一行之后执行,但是我只是想知道迭代是否可以检测到它的位置.
If I am progressing through a while loop that is reading a file line by line, is there a way to execute some code if the current iteration will be the final iteration in the loop? I understand that I could simply place this code immediately after the while loop, so that the code would execute after the last line, but I was just wondering if the iteration has any way of detecting it's position.
谢谢!
推荐答案
在特殊情况下,从文件中读取文件是.
In the special case where you are reading from a file, yes.
while(<>) {
if(eof) {
print "The last line of the file!\n";
}
}
这篇关于是否有可能检测当前的while循环迭代是否是perl中的最后一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!