由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Perl:如何处理这种hash 结构,
相关主题
问个Python的问题perl的hash怎么用汉字做key啊
perl cgi中调用C可执行程序为什么在html中不显示? (转载)perl oneliner hash疑问
大牛 in Perl and SED?用包子呼唤大牛们--问关于C++Destructor的问题
A problem on string parsing (using either grep or perl)How to concatenate two .tar.gz files
perl array|hash questionInterview questions about hash function
perl question: can I have a key of hash to bePath with non-ascii character
这个perl的简单小程序为什么不work? (转载)问个有关Perl Subroutine 转换到Module的问题
问一个perl的有关hash的问题python gc question
相关话题的讨论汇总
话题: 06话题: p1话题: p5话题: var1话题: p2
进入Programming版参与讨论
1 (共1页)
s******a
发帖数: 184
1
我有两个Excel 文件,第一个文件存着下面这样的数据
12 A P1
23 B P5
24 C P2
15 D P1
06 E P5
第二个文件存着下面这样的数据
06 100
23 20
06 200
06 95
23 05
24 18
基于第一个文件, 我产生了下面这样的结构
$VAR1 = {
'P5' => {
'E' => '06',
'B' => '23'
},
'P2' => {
'C' => '24'
},
'P1' => {
'A' => '12',
'D' => '15'
}
};
我是这么做得
my %Var1;
for my $i (1 .. $row1)
{
# for simplicity, I just keep the main part to building this hash chain
$Var1{$column3}->{$column2} = {$column1};
}
这里 $column3 相应于p1, p2, etc; $column2 相应于E, B, etc ; $column1 相应
于06,23,24,etc.
我的问题是如何基于上面产生的结构生成下面的结构,
$VAR = {
'P5' => {
'E' => '06' => [100, 200, 95]
'B' => '23' => [20, 1000, 05, 30]
},
'P2' => {
'C' => '24' => [18, 23, 2300, 3456]
},
'P1' => {
'A' => '12' => [24, 25, 3200, 5668]
'D' => '15' => [168]
}
};
比如说,第二个文件的第一行是06 100, 如何在VAR1种找到06,并把100 和它联上。
d****n
发帖数: 1637
2
先扫进第二个文件,以col 1 为 key ,然后push 每个value 到 key
push @{$hash{'06'}}, 100
push @{$hash{'06'}}, 200
你就有了 hash of array, like 06 =>[100, 200,50]
再扫第一个文件,当你用到
$VAR{$key1}{$key2}{$key3} 时候,
你就 assignment reference to
$VAR{'P5'}{'E'}{'06'}= \@{$hash{'06'}} ;
第二个文件 用 hash of array
第一个文件用 hash of hash of hash of reference
大致是这个样子,可能有语法问题。
顺便说下,你的结构够混乱的
太乱了。
我早就不用perl, 用python吧。

【在 s******a 的大作中提到】
: 我有两个Excel 文件,第一个文件存着下面这样的数据
: 12 A P1
: 23 B P5
: 24 C P2
: 15 D P1
: 06 E P5
: 第二个文件存着下面这样的数据
: 06 100
: 23 20
: 06 200

w******p
发帖数: 166
3
$ cat file1 file 2 | perl -ne '@a=split; if(scalar @a==3){$h{$a[2]}{$a[1]}{$
a[0]}=[]; $h1{$a[0]}=[@a[2,1]]} else {if(exists $h1{$a[0]}){($p,$a)=@{$h1{$a
[0]}}; push @{$h{$p}{$a}{$a[0]}}, $a[1] }else{warn "no entry for found for $
_"}} END{use Data::Dumper; print Dumper(\%h)}'
$VAR1 = {
'P5' => {
'E' => {
'06' => [
'100',
'200',
'95'
]
},
'B' => {
'23' => [
'20',
'05'
]
}
},
'P2' => {
'C' => {
'24' => [
'18'
]
}
},
'P1' => {
'A' => {
'12' => []
},
'D' => {
'15' => []
}
}
};
d****n
发帖数: 1637
4
牛人啊。都写一行。
不过这也是我为什么不爱用perl的原因。

{$
$a
$

【在 w******p 的大作中提到】
: $ cat file1 file 2 | perl -ne '@a=split; if(scalar @a==3){$h{$a[2]}{$a[1]}{$
: a[0]}=[]; $h1{$a[0]}=[@a[2,1]]} else {if(exists $h1{$a[0]}){($p,$a)=@{$h1{$a
: [0]}}; push @{$h{$p}{$a}{$a[0]}}, $a[1] }else{warn "no entry for found for $
: _"}} END{use Data::Dumper; print Dumper(\%h)}'
: $VAR1 = {
: 'P5' => {
: 'E' => {
: '06' => [
: '100',
: '200',

1 (共1页)
进入Programming版参与讨论
相关主题
python gc questionperl array|hash question
[合集] 如何能让这个程序快一点呢?太慢了perl question: can I have a key of hash to be
gvim diff question这个perl的简单小程序为什么不work? (转载)
how to assign new value to loop variables?问一个perl的有关hash的问题
问个Python的问题perl的hash怎么用汉字做key啊
perl cgi中调用C可执行程序为什么在html中不显示? (转载)perl oneliner hash疑问
大牛 in Perl and SED?用包子呼唤大牛们--问关于C++Destructor的问题
A problem on string parsing (using either grep or perl)How to concatenate two .tar.gz files
相关话题的讨论汇总
话题: 06话题: p1话题: p5话题: var1话题: p2