我爱小虾 发表于 2017-5-18 07:20:50

perl文件读取之心得

  在perl中用到的读文件一般有两种方法:
  1.先把整个文件download到一个数组里面然后对数组进行遍历操作。
  如:
  open (IN,"alignresult.txt") ||(die "can not open the file input.txt\n$!");
my @all = <IN>;
  foreach $line(@all){
  process $line;}
  或者
  foreach $line(<IN>){...}
  语义: 将文件load到数组中,在对数组进行遍历操作
  这样子做得缺点是文件很大时,会占据很大内存,当然速度上占优。
  2.为节省内存,一般建议如下操作
  open (IN,"alignresult.txt") ||(die "can not open the file input.txt\n$!");
while ($line = <IN>){..}
  这样的语义:
  每次$line = <IN>,文件指针后移
页: [1]
查看完整版本: perl文件读取之心得