假设我有以下数据
cluster1中:d(A)中,f(C)类,(A)
Cluster2中:R(d)中,h(d)中,f(A)
我想要这个
输出:
cluster1:A->2
cluster1:C->1
cluster2:D->2
cluster2:A->1
这是我的尝试,但它不正确,我试图计算字符的部分有一个我无法解决的问题 代码是非常大的代码的一部分,我想要完全多维哈希
use strict;
use Data::Dumper;
my %count;
while (<DATA>) {
my %HoH;
my ( $cluster, $ch ) = split (/:/,$_);
$HoH{$cluster}={split /[()]+/,$ch};
for my $clust ( keys %HoH ) {
for my $character ( keys %{ $HoH{$clust} } ) {
$count{$clust}{$HoH{$clust}{$character}}++;
}
}
}
print Dumper(\%count);
foreach my $name (sort keys %count) {
foreach my $subject (keys %{$count{$name}}) {
print "$name:$subject->$count{$name}{$subject}\n";
}
}
数据
cluster1中:d(A)中,f(C)类,(A)
Cluster2中:R(d)中,h(d)中,f(A)
答案 0 :(得分:1)
您期望$count{$cluster}{$characters}+=1;
完全做什么?如果您希望得到所需的结果,则必须循环输入数据以填充%count
:
while (<DATA>) {
next unless /^(cluster\d+):(.+)/;
$count{$1}{$_}++ for split/,/, $2;
}
如果您还将sort
添加到第二个foreach
,您将获得所需的输出。
编辑:这解决了更新输入和要求的问题:
my %count;
while (<DATA>) {
next unless /^(cluster\d+):(.+)/;
my $cluster = $1;
$count{$cluster}{$_}++ for $2 =~ /\((\w)\)/g;
}
for my $key (sort keys %count) {
for my $value (sort {
$count{$key}{$b} <=> $count{$key}{$a}
} keys %{$count{$key}}) {
print "$key:$value->$count{$key}{$value}\n";
}
}
答案 1 :(得分:0)
如果你试着理解下面的代码,那么你就可以找到解决问题的想法: -
use strict;
use Data::Dumper;
my $data = "cluster1:A,B,C,A";
my %cluster = ();
my ($cluster_key, $cluster_val ) = split (':', $data);
my @cluster1_data = split(',', $cluster_val);
foreach my $val ( @cluster1_data ) {
$cluster{$cluster_key}{$val}++;
}
print Dumper(\%cluster);
foreach my $clus ( keys %cluster ) {
my $clus_ref = $cluster{$clus};
foreach my $clu ( keys %{ $clus_ref } ){
my $count = $clus_ref->{$clu};
print"$clus:$clu->$count\n";
}
}
输出:
$VAR1 = {
'cluster1' => {
'A' => 2,
'C' => 1,
'B' => 1
}
};
cluster1:A->2
cluster1:C->1
cluster1:B->1