将两个文件中的哈希值合并为一个文件(combine hashes from two files into a single file)

编程入门 行业动态 更新时间:2024-10-14 14:22:25
将两个文件中的哈希值合并为一个文件(combine hashes from two files into a single file)

我有两个包含这样的数据的文件:

FILE1包含组号(第一列)和切换另一组(第二列)的频率(第三列):

FILE1:

1 2 0.6 2 1 0.6 3 1 0.4 1 3 0.4 2 3 0.2

等等...

FILE2包含组编号(第一列)及其出现频率(第二列)。

FILE2:

1 0.9 2 0.7 3 0.5

等等...

我想创建另一个包含FILE2的文件,其中包含来自FILE1的每个开关的值,如下所示:

1 0.9 2 0.6 3 0.4 ... 2 0.7 1 0.6 3 0.2 ...

基本上,我希望第一列是组号,第二列是它出现的频率,然后是它们切换到的组和该开关的频率,然后接下来在该特定组的同一行中切换,然后是下一行 - 组2等

所以我想在FILE1中读取,为每个组创建一个数组哈希,其中键是组号,值是他们切换到的组以及该开关的频率。 我将为每个组包含一个大数组,其中包含他们切换到的每个组的子阵列和频率。 然后我想用与第一个哈希中相同的键创建另一个哈希,但是使用FILE2中第一列的数字和FILE2的第二列中的值。 然后我将打印出“hash2 key hash2 value hash1 full array for the key”。 这是我使用Perl的尝试:

#!/usr/bin/perl -W $input1= $ARGV[0]; $input2 = $ARGV[1]; $output = $ARGV[2]; %switches=(); open (IN1, "$input1"); while (<IN1>) { @tmp = split (/\s+/, $_); chomp @tmp; $group = shift @tmp; $switches{$group} = [@tmp]; push (@{$switches{$group}}, [@tmp]); } close IN1; %groups=(); open (IN2, "$input2"); while (<IN2>) { chomp $_; ($group, $pop) = split (/\s+/, $_); $groups{$group} = $pop; } close IN2; open (OUT, ">$output"); foreach $group (keys %groups) { print OUT "$group $pop @{$switches{$group}}\n" } close OUT;

我得到的输出包含如下内容:

1 0.1 2 0.1 ARRAY(0x100832330) 2 0.3 5 0.2 ARRAY(0x1008325d0)

所以基本上:

“组”“最后一个频率号码”“该组切换到的最后一组”“最后一个开关频率”“像ARRAY(0x100832330)一样”

我假设我在使用FILE1时将所有开关推入数组的散列并且在打印时最后使用解除引用来解决问题。

请帮忙,谢谢!

I have two files containing data like this:

FILE1 contains group numbers (first column) and the frequency (third column) of their switching another group (second column):

FILE1:

1 2 0.6 2 1 0.6 3 1 0.4 1 3 0.4 2 3 0.2

etc...

FILE2 contains group numbers (first columns) and their frequency of occurrence (second column).

FILE2:

1 0.9 2 0.7 3 0.5

etc...

I want to make another file containing FILE2 with the values for each switch from FILE1 like this:

1 0.9 2 0.6 3 0.4 ... 2 0.7 1 0.6 3 0.2 ...

Basically, I want first column to be the group number, second the frequency of its occurrence, then the group they switch to and the frequency of that switch, then next switch all in the same line for that particular group, then next line - group 2 etc.

So I want to read in FILE1, make a hash of arrays for each group with keys being group numbers and the values being the group they switch to and the frequency of that switch. I will have one big array for each group containing subarrays of each group they switch to and frequency. Then I want to make another hash with the same keys as in the first hash but with the numbers from the first column in FILE2 and values from the second column of FILE2. Then I will print out "hash2 key hash2 value hash1 whole array for that key". This is my attempt using Perl:

#!/usr/bin/perl -W $input1= $ARGV[0]; $input2 = $ARGV[1]; $output = $ARGV[2]; %switches=(); open (IN1, "$input1"); while (<IN1>) { @tmp = split (/\s+/, $_); chomp @tmp; $group = shift @tmp; $switches{$group} = [@tmp]; push (@{$switches{$group}}, [@tmp]); } close IN1; %groups=(); open (IN2, "$input2"); while (<IN2>) { chomp $_; ($group, $pop) = split (/\s+/, $_); $groups{$group} = $pop; } close IN2; open (OUT, ">$output"); foreach $group (keys %groups) { print OUT "$group $pop @{$switches{$group}}\n" } close OUT;

The output I get contains something like:

1 0.1 2 0.1 ARRAY(0x100832330) 2 0.3 5 0.2 ARRAY(0x1008325d0)

So basically:

"group" "one last frequency number" "one last group that that group switches to" "one last switch frequency" "smth like ARRAY(0x100832330)"

I assume I am doing smth wrong with pushing all switches into the hash of arrays while in FILE1 and also with dereferencing at the end when I print out.

Please help, Thanks!

最满意答案

您的%switches哈希包含冗余信息; 只需使用push 。 此外,您需要做更多工作来打印出您想要的内容。 这是您的代码,只有很少的更改:

$input1= $ARGV[0]; $input2 = $ARGV[1]; $output = $ARGV[2]; %switches=(); open (IN1, "$input1"); while (<IN1>) { @tmp = split (/\s+/, $_); chomp @tmp; $group = shift @tmp; push (@{$switches{$group}}, [@tmp]); } close IN1; %groups=(); open (IN2, "$input2"); while (<IN2>) { chomp $_; ($group, $pop) = split (/\s+/, $_); $groups{$group} = $pop; } close IN2; open (OUT, ">$output"); foreach $group (sort {$a <=> $b} keys %groups) { print OUT "$group $groups{$group} "; for my $aref (@{$switches{$group}}) { print OUT "@{$aref}"; } print OUT "\n"; } close OUT; __END__ 1 0.9 2 0.63 0.4 2 0.7 1 0.63 0.2 3 0.5 1 0.4

另请参见perldoc perldsc和perldoc Data :: Dumper

Your %switches hash contains redundant information; just use the push. Also, you need to do more work to print out what you want. Here is your code with minimal changes:

$input1= $ARGV[0]; $input2 = $ARGV[1]; $output = $ARGV[2]; %switches=(); open (IN1, "$input1"); while (<IN1>) { @tmp = split (/\s+/, $_); chomp @tmp; $group = shift @tmp; push (@{$switches{$group}}, [@tmp]); } close IN1; %groups=(); open (IN2, "$input2"); while (<IN2>) { chomp $_; ($group, $pop) = split (/\s+/, $_); $groups{$group} = $pop; } close IN2; open (OUT, ">$output"); foreach $group (sort {$a <=> $b} keys %groups) { print OUT "$group $groups{$group} "; for my $aref (@{$switches{$group}}) { print OUT "@{$aref}"; } print OUT "\n"; } close OUT; __END__ 1 0.9 2 0.63 0.4 2 0.7 1 0.63 0.2 3 0.5 1 0.4

See also perldoc perldsc and perldoc Data::Dumper

更多推荐

本文发布于:2023-04-29 04:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   并为   两个   哈希值合   combine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!