Perl從文件中讀取字符串的兩種實現(xiàn)方法
更新時間:2013年02月08日 20:44:31 作者:
有時候我們需要從文件中讀取字符串,這里簡單介紹下, 需要的朋友可以參考下
1. 一次性將文件中的所有內(nèi)容讀入一個數(shù)組中(該方法適合小文件):
復(fù)制代碼 代碼如下:
open(FILE,"filename")||die"can not open the file: $!";
@filelist=<FILE>;
@filelist=<FILE>;
foreach $eachline (@filelist) {
chomp $eachline;
}
close FILE;
@filelist=<FILE>;
當(dāng)文件很大時,可能會出現(xiàn)"out of memory"錯誤。
2. 一次從文件中讀取一行,一行行地讀取和處理(讀取大文件時比較方便):
復(fù)制代碼 代碼如下:
open(FILE,"filename")||die"can not open the file: $!";
while (defined ($eachline =<FILE>)) {
chomp $eachline;
# do what u want here!
}
close FILE;
相關(guān)文章
perl之print,printf,sprintf使用案例詳解
這篇文章主要介紹了perl之print,printf,sprintf使用案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09Perl 和 StrawberryPerl 與 ActivePerl 的區(qū)別詳解
這篇文章主要介紹了Perl 和 StrawberryPerl 與 ActivePerl 的區(qū)別詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12