Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2137621
  • 博文数量: 227
  • 博客积分: 10521
  • 博客等级: 上将
  • 技术积分: 3452
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-20 14:59
个人简介

低调做人,高调做事!

文章分类

全部博文(227)

文章存档

2013年(4)

2012年(8)

2011年(16)

2010年(24)

2009年(92)

2008年(83)

分类: LINUX

2009-05-07 22:37:26

测试数据文件如下:
[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

这个运行的结果是把测试文件中数值抽取出来了。
阅读(2088) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~