[Perl] merge DB problem

Offer Kaye oferk at oren.co.il
Tue Oct 22 06:45:07 PDT 2002


>
>
> I want record "b" from file 1 to be added after record "a".  How can I
> change the original solution (using 2 hashes and merge and checking
> existance of keys) to suit this one too? I need a simple solution as
> possible.
>
> Thanks
>
> Yossi
>
>

Use an array to hold the values of the "Name" lines as you read them, that
will keep them in order as you read them from file number 1. Use another
array for file 2. Then when printing from the hashes, use the order from the
arrays, and don't print twice Names that are both in array 1 and 2.
BTW, why are you using 2 hashes? One should be enough- the key is the name,
and the val is a reference to an array holding the values belonging to that
name. That way, after reading in file #1, start reading in file #2, and if
exists $hash{$current_name} don't save it or it's vals. Then when printing
you only have to print that hash, with the keys order specified by the
arrays I mentioned earlier.

Hope this helps :-)
Offer Kaye




More information about the Perl mailing list