Chinaunix

标题: perl 读目录下文件问题,请教如何略过“点”文件 [打印本页]

作者: huagao2000    时间: 2012-11-26 11:00
标题: perl 读目录下文件问题,请教如何略过“点”文件
目录下只有一个文件:
[laps0@localhost ~]$ ls -a data/rawdata/profile/from_liju/hd
.  ..  20100701.TXT
代码如下:
#!/usr/bin/perl
my $path="/home/laps0/data/rawdata/profile/from_liju/hd";
opendir(DIR,$path) or die " Error : can't open $path !";
my $file;
while ($file=readdir DIR)
   {
print "the file is : $file \n";
open(FILE,"$file";
my @var = <FILE>;
print "the var is : $var[25] \n";
close(FILE);

输出信息:

[laps0@localhost wind]$ perl tmp.pl
the file is : .
the var is :  
the file is : 20100701.TXT
the var is :  
the file is : ..
the var is :  

问题是不想出来那两个点文件,请问如何操作,请费心指教,谢谢!


作者: xi0ws    时间: 2012-11-26 11:15
  1. next if ($file eq "."|| $file eq "..");
复制代码

作者: 首天    时间: 2012-11-26 11:16
  1. #!/usr/bin/perl
  2. my $path="/home/laps0/data/rawdata/profile/from_liju/hd";
  3. opendir(DIR,$path) or die " Error : can't open $path !";
  4. my $file;
  5. while ($file=readdir DIR)
  6.    {
  7. next,if $file =~ /^\.+$/; #添加这一行就可以了
  8. print "the file is : $file \n";
  9. open(FILE,"$file";
  10. my @var = <FILE>;
  11. print "the var is : $var[25] \n";
  12. close(FILE);
复制代码

作者: huagao2000    时间: 2012-11-26 11:28
多谢 :wink:,xi0ws和首天 ,绝招跟奏效!
作者: kk861123    时间: 2012-11-26 13:16
首天 发表于 2012-11-26 11:16

好吧,严格来说'.' and '..'应该是:
  1. next if $file =~ /^\.\.?$/;
复制代码

作者: 首天    时间: 2012-11-26 16:22
kk861123 发表于 2012-11-26 13:16
好吧,严格来说'.' and '..'应该是:


多谢提醒




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2