kjsunny has asked for the wisdom of the Perl Monks concerning the following question:

Hello

I have a question. I want to merge 2 files("re_file.txt and re_mplus1.txt") to 1 file

The re_file.txt is a list of some keys and data..... maybe 3,000 lines.

The re_mplus1.txt file is a list of data and have same key of re_file.txt ...... maybe 1,000,000 lines

What I want to do is add $d3 to %data1 in the last location.

$d3 is the value "$d2 in the re_file1.txt - $d1 in the re_mplus1.txt"

Is there any way to do it?

The format of result must be...

id c7 c8 id2 p10 p12 d2 d3.......id2~d3 are placed 5 times in case ($d3 <= 5 )

If the list ($d3 <= 5 ) is 4 , than print 4 times

I'm not good at English and I hope I clearly explained my problem to you.

Frankly the source in the below is not mine..T.T .I am just modify original source.

#!/usr/bin/perl open(REFILE, 're_file.txt') || die $!; my %data1; while(<REFILE>) { chomp; next unless $_; my($id1, $id2, $p10, $p11, $p12, $d1) = split; $data1{$id1}->{$id2} = [$id2, $p10, $p11, $p12, $d1]; # referenc +e !! } close(REFILE); # open(PRT,">re_file2.txt"); open(DATAFILE,"re_mplus1.txt"); while(<DATAFILE>) { chomp; next unless $_; my($id, $c7, $c8, $d2) = split(/\s+/, $_); foreach $datafile ( keys %{$data1{$id}}) { $d3 = abs($data1{$id}->{$datafile}[4]-$d2); if($d3 <= 5 ) { # print "$id, abs($data1{$id}->{$datafile}[4]-$d2),$d +3\n"; # %myhash1 = [$data1{$id}->{$datafile}[0],$data1{$id}->{ +$datafile}[1],$data1{$id}->{$datafile}[2],$data1{$id}->{$datafile}[3] +,$data1{$id }->{$datafile}[4],$d3]; } } my @arr = sort { abs($a->[4]) <=> abs($b->[4]) } values %{$data1 +{$id}}; print "$id $c7 $c8 "; print join(" ", map { join(' ', @{$_}[0,1,4]) } splice(@arr, 0, +5 ) ) . "\n"; } close(DATAFILE); # close(PRT);

Replies are listed 'Best First'.
Re: hash problem
by moritz (Cardinal) on Mar 23, 2010 at 16:25 UTC
    I'm not good at English and I hope I clearly explained my problem to you.

    I'm afraid you did not. You neither provided samples of the input and output files, nor did you tell us how the current output differs from the expected output.

    You also didn't tell us what you have tried, and how you failed. We can help you learning and understanding Perl, but we won't solve problems for you for free.

    Perl 6 - links to (nearly) everything that is Perl 6.