免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2858 | 回复: 3
打印 上一主题 下一主题

如何用perl提取一个.h文件里的数据,并将数据放到两个数组里? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-28 20:11 |只看该作者 |倒序浏览
现在有一个文件: data.h, 其内容如下:

/*  File Name:  data.h */
UC D1[] =
    {
        //D1P
0,6,13,123,0,32,4,81,
3,109,0,23,5,17,32,12,

    };
UC D2[] =
    {
        //D2
127,107,127,132,127,127,111,127,
127,125,127,127,127,127,127,127,
    };

    现在要使用D1和D2里的数据,如何把D1和D2里的数据写到数组d1和d2里去呢? 哪位大侠能帮忙给段代码,兄弟感激不尽......

论坛徽章:
0
2 [报告]
发表于 2008-05-28 20:32 |只看该作者
#!/usr/bin/perl

use strict;
use warnings;

my %array_hash;
my $array_name;
while (<DATA>) {
    chomp;
        $array_name = lc $1 if (/(D.)\[/);
        if (/(\d,)+/) {
                s/\s//;
                push @{$array_hash{$array_name}}, (split ',', $_);
        }}

for (keys %array_hash) {
    print "$_\n";
        print "\t$_ \n" for @{$array_hash{$_}};
}

__DATA__
/*  File Name:  data.h */
UC D1[] =
    {
        //D1P
0,6,13,123,0,32,4,81,
3,109,0,23,5,17,32,12,

    };
UC D2[] =
    {
        //D2
127,107,127,132,127,127,111,127,
127,125,127,127,127,127,127,127,
    };

[ 本帖最后由 cobrawgl 于 2008-5-29 06:21 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-05-29 11:27 |只看该作者
我这里有一种读文件的内容的方法,不过是读变量内容,不是读数组,给你参考一下:
file content for "data.seq":
$isa_no = 453
$gs_no = 453
$st_no = 1853

Code

  1. #!/usr/bin/perl -w
  2. my $config_file = "data.seq";
  3. my $isa_no = 0;
  4. my $gs_no = 0;
  5. my $st_no = 0;
  6. open(CONFIGFILE, "<", $config_file) or die("Cannot open config file: $config_file.\n");
  7. while(my $line = <CONFIGFILE>){eval($line);}
  8. close(CONFIGFILE);
  9. print "$isa_no\n";
  10. print "$gs_no\n";
  11. print "$st_no\n";
复制代码

论坛徽章:
0
4 [报告]
发表于 2008-07-08 15:50 |只看该作者
#!/usr/bin/perl
use warnings;
use strict;

my @r;
my %h;
my $flag = 0;
while(<DATA>)
{
    chomp;
    if(/\/\//)
    {
        $flag = 1;
}
    if(/}/)
    {
        $flag = 0;
    }
    push @r,$1 if /\/\/(\w+)/;
    if($flag && !($_=~/\/\/(\w+)/))
    {
        $h{$1} .= $_;
   }
}
print $h{$_},"\n" for @r;



__DATA__
/*  File Name:  data.h */
UC D1[] =
    {
        //D1P
0,6,13,123,0,32,4,81,
3,109,0,23,5,17,32,12,
    };
UC D2[] =
    {
        //D2P
127,107,127,132,127,127,111,127,
127,125,127,127,127,127,127,127,
    };
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP