Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48395
  • 博文数量: 51
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 72
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-22 16:36
个人简介

地方

文章分类

全部博文(51)

文章存档

2013年(51)

我的朋友

分类: LINUX

2013-04-22 16:41:04

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

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