w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
combine hashes from two files into a single file

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 "
";
}

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





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.