测试数据文件如下:
[root@test opt]# more 1.txt
52428800S
7143090 688
1775 1
4946 1
1,用map可以实现,代码如下:
#!/usr/bin/perl -w
use strict;
my $filename = '/opt/1.txt';
open FH, "< $filename" or die "can't open $filename ..... ($!)";
my $line = 0;
my %file = map {($line++, $_)} ;
print $file{0};
print $file{1};
运行结果:
[root@test opt]# ./open.pl
52428800S
7143090 688
2,用 Tie::File 模块实现,代码如下:
#!/usr/bin/perl -w
use strict;
use Tie::File;
my $filename = '/opt/1.txt';
tie my @array, 'Tie::File', $filename or die "$!";
if ( @array) {
my $total = substr($array[0], 0, length($array[0])-1);
my $use = (split(' ', $array[1]))[0];
my $mail_count = (split(' ', $array[1]))[1];
print "$total\n";
print "$use\n";
print "$mail_count\n";
untie @array;
}
运行结果:
[root@test opt]# ./tie_file.pl
52428800
7143090
688
这个运行的结果是把测试文件中数值抽取出来了。
阅读(2102) | 评论(0) | 转发(1) |