perl hash或者 OOP 问题

dark_tom 2013-02-05 10:46:33
有数据结构:
type=dielectric
name=sti_base
thickness=0.07
eps=4

type=diffusion
name=diff_
thickness=0.25
min_width=0.072
min_spacing=0.099
src_drn=( nsd_ psd_ )
r_sheet=15

type=dielectric
name=sti
thickness=0.25
eps=4

type=contact
name=dfcont_
min_width=0.081
min_spacing=0.099
measured_from=diff_
measured_to=m1_
enclosure_up=0
enclosure_down=0.0135
n1=0.19683
n2=0
min_poly_cont_spacing=0.0495


。。。。。。

其中,name是唯一的,每一段都是一个事物,“=”左边是属性,右边是值。
怎么把这个文件的内容读到一个hash中去呢?或者读到OOP中去?
注,每一个“type”是新的段落开始标志
...全文
68 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bugs2k 2013-02-05
  • 打赏
  • 举报
回复
用数组行不?
#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

my $nl = 0;
my $i = 0;
my @dats = [];

while (<DATA>) {
    chomp;
    if (/^\s*$/) {
        $i++ if ($nl == 1);
        $nl = 0;
        next;
    }
    push @{$dats[$i]}, [split /\s*=\s*/];
    $nl = 1;
}
print Dumper(\@dats);

__DATA__

type=dielectric
name=sti_base
thickness=0.07
eps=4

type=diffusion
name=diff_
thickness=0.25
min_width=0.072
min_spacing=0.099
dark_tom 2013-02-05
  • 打赏
  • 举报
回复
多谢,给分了,如有有其他问题还要请教
dark_tom 2013-02-05
  • 打赏
  • 举报
回复
我先试试你的代码,OK了马上结贴
bugs2k 2013-02-05
  • 打赏
  • 举报
回复
#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

my $nl = 0;
my $i = 0;
my @dats;

while (<DATA>) {
    chomp;
    if (/^\s*$/) {
        $i++ if ($nl == 1);
        $nl = 0;
        next;
    }
    my($n, $v) = split /\s*=\s*/;
    $dats[$i]->{$n} = $v;
    $nl = 1;
}
print Dumper(\@dats);

__DATA__

type=dielectric
name=sti_base
thickness=0.07
eps=4

type=diffusion
name=diff_
thickness=0.25
min_width=0.072
min_spacing=0.099
src_drn=( nsd_ psd_ )
r_sheet=15

type=dielectric
name=sti
thickness=0.25
eps=4

type=contact
name=dfcont_
min_width=0.081
min_spacing=0.099
measured_from=diff_
measured_to=m1_
enclosure_up=0
enclosure_down=0.0135
n1=0.19683
n2=0
min_poly_cont_spacing=0.0495
bugs2k 2013-02-05
  • 打赏
  • 举报
回复
#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

my $nl = 0;
my $i = 0;
my %dats;

while (<DATA>) {
    chomp;
    if (/^\s*$/) {
        $i++ if ($nl == 1);
        $nl = 0;
        next;
    }
    my($n, $v) = split /\s*=\s*/;
    $dats{$i}->{$n} = $v;
    $nl = 1;
}
print Dumper(\%dats);

__DATA__

type=dielectric
name=sti_base
thickness=0.07
eps=4

type=diffusion
name=diff_
thickness=0.25
min_width=0.072
min_spacing=0.099
src_drn=( nsd_ psd_ )
r_sheet=15

type=dielectric
name=sti
thickness=0.25
eps=4

type=contact
name=dfcont_
min_width=0.081
min_spacing=0.099
measured_from=diff_
measured_to=m1_
enclosure_up=0
enclosure_down=0.0135
n1=0.19683
n2=0
min_poly_cont_spacing=0.0495

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧