如何忽略Perl的readdir中的单点和双点条目? [英] How to ignore the single and double dot entries in Perl's readdir?
问题描述
从这里追踪: Perl Imgsize不工作在循环?
我有另一个问题 - 当我读取目录中的文件时,怎么不让perl列出单和双点条目?
Following up from here: Perl Imgsize not working in loop? I have another question - how do I not let perl list the single and double dot entries when it reads the files in a directory?
如上面链接的问题,我输出到while循环( while($ file = readdir($ dh))
)是这样的:
As in the linked question above, my output to the while loop (while ($file = readdir($dh))
) is something like this:
.
..
file1.jpg
file2.jpg
file3.jpg
file4.jpg
file5.jpg
这样会产生问题,因为我还有一个计数器,这在计数我的文件。和。
和 ..
不要算作文件,所以我的柜台给我更多的文件在我的目录比真正的。
And this creates problems because I've also got a counter somewhere, which is counting my files. And .
and ..
don't count as files, so my counter is giving a greater number of files in my directories than there really are.
我添加了 if($ file ==。|| $ file ==..){继续;}
和 if($ file〜/^\./或$ file〜/^\.\./){continue;}
和while循环开始的不同的变体...但是我不断得到一个中止由于编译错误。
I added if($file == "." || $file == ".."){ continue;}
and if ($file ~ /^\./ or $file ~ /^\.\./) {continue;}
and different variations of that to the beginning of the while loop... but I keep getting an abort "due to compilation errors".
我们如何得到保留单点和双点条目?
How can we get a hold of the single and double dot entries?
推荐答案
要测试字符串,您必须使用 eq
:
To test string you have to use eq
:
if($file eq "." || $file eq ".."){ next;}
或:
next if $file =~ /^\.\.?$/;
这篇关于如何忽略Perl的readdir中的单点和双点条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!