Perl从文件中读取字符串的两种实现方法
1. 一次性将文件中的所有内容读入一个数组中(该方法适合小文件):
代码如下:
open(FILE,"filename")||die"can not open the file: $!"; @filelist=<FILE>; foreach $eachline (@filelist) { chomp $eachline; } close FILE; @filelist=<FILE>;
当文件很大时,可能会出现"out of memory"错误。
2. 一次从文件中读取一行,一行行地读取和处理(读取大文件时比较方便):
代码如下:
open(FILE,"filename")||die"can not open the file: $!"; while (defined ($eachline =<FILE>)) { chomp $eachline; # do what u want here! } close FILE;
相关推荐
边城客栈学无止境 2020-07-05
Walter的学习笔记 2020-07-04
A宇 2020-06-14
边城客栈学无止境 2020-06-10
邓博学习笔记 2020-06-03
davidliu00 2020-05-26
ShiShuo 2020-05-16
Aggressivesnail 2020-05-10
ShiShuo 2020-04-26
hanxingwang00 2020-04-22
davidliu00 2020-03-06
ShiShuo 2020-03-06
ShiShuo 2020-03-05
Aggressivesnail 2020-02-28
aaLiweipeng 2020-02-01
amberom 2020-01-16
Walter的学习笔记 2020-01-06