perl读写txt

前端之家收集整理的这篇文章主要介绍了perl读写txt前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


perl写文件,源码示例: 

open(FILE,">keyword.txt");  
syswrite(FILE,"This is my write file contents\n");  
close(FILE); 
>为每次新建
>>为追加


perl读文件,源码示例(文件中只有一行文字,多行需要添加循环): 

open(FILE,"keyword.txt");  
my $keyword=<FILE>;  
close(FILE);  



这里有一个 读写的例子 把A中的内容 写到B中:

#!/usr/bin/perl
use strict;
use warnings;

open A," A.txt" or die "A: $!";
open B,">B.txt" or die "B: $!";

while (<A>) {
   chomp;
   print B $_/10,"\n";
}
close A;
close B;


读取txt放入数组中。

open IN,“<”,"data.txt" or die;my @a = <IN>;close IN;
chop--数组去尾 (chomp)     chop的意义是去掉STDIN(键盘)输入字符串时最后一个字符--换行符。 而如果它作用到数组上,则将数组中每一个元素都做如此处理。     @list = ("rabbit", "12345","quartz") ;     chop (@list); 

猜你在找的Perl相关文章