您的位置:首页 > 其它

perl文件读取之心得

2005-06-08 11:35 417 查看
在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>,文件指针后移
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: