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

Hellow Monks.

I'm learning perl nowadays. It's very interesting.

But I have a problem. I can't solve....

please help me to solve this problem.

I want to merge two files.

but merged new file must have some format. and I want to know how I can do it.

and I don't want to solving problem by using modules, packages.

here's format

---------------------------------------------

file1 format

key key2 var0 var1 var2 data1

A A-0 a10 a11 a12 8

A A-1 a10 a11 a12 12

A A-2 a10 a11 a12 5

A A-3 a10 a11 a12 8

A A-4 a10 a11 a12 2

A A-5 a10 a11 a12 15

A A-6 a10 a11 a12 8

A A-7 a10 a11 a12 12

A A-8 a10 a11 a12 15

A A-9 a10 a11 a12 8

B B-0 a10 a11 a12 7

B B-1 a10 a11 a12 2

B B-2 a10 a11 a12 6

B B-3 a10 a11 a12 9

B B-4 a10 a11 a12 16

B B-5 a10 a11 a12 19

B B-6 a10 a11 a12 10

C C-0 a10 a11 a12 1

C C-1 a10 a11 a12 3

C C-2 a10 a11 a12 16

C C-3 a10 a11 a12 11

C C-4 a10 a11 a12 10

----------------------------------------------

file2 format

key var10 var11 data2

A b00 b01 6

A b00 b01 9

B b00 b01 10

C b00 b01 11

B b00 b01 9

A b00 b01 13

--------------------------------------------------

result file format ( file1 + file2)

key var10 key2 var0 var1 (data2-data1) key2 var0 var1 (data2-data1) key3 var0 var1 (data2-data1)

--------------------------------------------------

there are three key2 because I choose three lists in file1 that is most small in the range -5 <(data2-data1) < 5.

if there are two in the range than you can choose two. If there are four in the range then you can choose three that are most small number (data2-data1)

please. help me.

Replies are listed 'Best First'.
Re: merging 2 files to one problem
by jethro (Monsignor) on Mar 19, 2010 at 12:06 UTC

    What have you done so far? You should divide the task into smaller tasks, then it becomes manageble:

    1. Reading in file 1. Use split() to get at the single values of the line. I would recommend storing the data into a hash (or probably a HashofHashes or HashofArrays). Yes, that is not easy for a beginner, but there is a lot of documentation in perl books or on the net how to handle complex strucutures like HashofHashes and HashofArrays. For example http://perldoc.perl.org/perlreftut.html

    2. Reading in file 2. If you can process each line separately, call a subroutine with each line that does the processing (again to divide the task). If not, storing into a hash might be best to group together all lines with the same key.

    3. Merging. Loop through each key of one the files and check out any corresponding data in the second file. Compare, sort and output to a third file. Use subroutines to divide the task into small steps.

    Sorry for the very general advice, but you left out so much information about your problem we could give you hundreds of different solutions that would all match your problem description and still would not be what you want. Also this is not a code-for-hire website, we help, but we want to see some effort from your side too.

      Thanks for your answer. I want to solve that problem by my self. but Frankly, on no coding expirence. that's too difficult to me. but your advice is very helpful to me. Thank you very much.
Re: merging 2 files to one problem
by kiruthika.bkite (Scribe) on Mar 19, 2010 at 10:33 UTC
    Kindly clearly explain the result file format.