perl:将哈希对添加到更大的哈希中

发布于 2024-12-29 05:47:15 字数 916 浏览 1 评论 0原文

我生成了一个哈希表,然后尝试将多个文件中的每个文件添加到更大的哈希表(如果唯一),但我在语法上遇到了问题,并且不断意外调用值或创建哈希值。我想做的就是把:

(The actual $hash key) => $hash{$key};

变成

 $compound_hash{$key} = $hash{$key};


目前我有:

    if ($file_no == 0){
            while (my ($key, $value) = each %hash){
                    $compound_hash{$key} = $value;
            }       

    }else{
            while (my ($key, $value) = each %compound_hash){

                    if (exists $hash{$key}){
                            print "$key: exists\n";
                            $compound_hash{$key} .= ",$hash{$key}";
                    }else{
                          print "$key added\n";  
                          XXXXXXX
                    }

最终结果是将散列值连接到每行的末尾,制作一个 .csv ie

     abc,0,32,45
     def,21,43,23
     ghi,1,49,54

I have a hash table generated which I am then trying to add to a larger hash table, (if unique) for each of multiple files but I'm having trouble with the syntax and keep accidentally calling values or creating a hash of hash. All I want to do is turn:

(The actual $hash key) => $hash{$key};

into

 $compound_hash{$key} = $hash{$key};

Currently I have:

    if ($file_no == 0){
            while (my ($key, $value) = each %hash){
                    $compound_hash{$key} = $value;
            }       

    }else{
            while (my ($key, $value) = each %compound_hash){

                    if (exists $hash{$key}){
                            print "$key: exists\n";
                            $compound_hash{$key} .= ",$hash{$key}";
                    }else{
                          print "$key added\n";  
                          XXXXXXX
                    }

The end result is to concatenate the hash value on to the end of each line, making a .csv ie

     abc,0,32,45
     def,21,43,23
     ghi,1,49,54

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2025-01-05 05:47:15

很难准确地说出,但我认为您正在寻找的是这样的:

for my $key (keys %hash) {  # for all new keys
     if (exists $compound_hash{$key}) {  # if we have seen this key
          $compound_hash{$key} .= ",$hash{$key}"  # append it to the csv
     }
     else {
          $compound_hash{$key} = $hash{$key}  # otherwise create a new entry
     }
}

在我自己的代码中,我可能会设置 %compound_hash 最初填充数组引用,然后将其连接到数据填充后字符串。

for my $key (keys %hash) {
     push @{ $compound_hash{$key} }, $hash{$key}
}

然后稍后

for my $value (values %compound_hash) {
    $value = join ',' => @$value
}

这将比重复将数据附加到复合哈希中包含的字符串更有效。

Its hard to tell exactly, but I think what you are looking for is something like this:

for my $key (keys %hash) {  # for all new keys
     if (exists $compound_hash{$key}) {  # if we have seen this key
          $compound_hash{$key} .= ",$hash{$key}"  # append it to the csv
     }
     else {
          $compound_hash{$key} = $hash{$key}  # otherwise create a new entry
     }
}

In my own code, I might setup %compound_hash to be initially populated with array references, which are then joined down to strings once the data is filled.

for my $key (keys %hash) {
     push @{ $compound_hash{$key} }, $hash{$key}
}

and then later

for my $value (values %compound_hash) {
    $value = join ',' => @$value
}

Which will be more efficient than repeatedly appending data to the strings contained in the compound hash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文