perl:将哈希对添加到更大的哈希中
我生成了一个哈希表,然后尝试将多个文件中的每个文件添加到更大的哈希表(如果唯一),但我在语法上遇到了问题,并且不断意外调用值或创建哈希值。我想做的就是把:
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难准确地说出,但我认为您正在寻找的是这样的:
在我自己的代码中,我可能会设置
%compound_hash
最初填充数组引用,然后将其连接到数据填充后字符串。然后稍后
这将比重复将数据附加到复合哈希中包含的字符串更有效。
Its hard to tell exactly, but I think what you are looking for is something like this:
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.and then later
Which will be more efficient than repeatedly appending data to the strings contained in the compound hash.