If you're on a *nix system and don't absolutely
have to do this with perl, the
join command would take care of this quite simply... Dump the tables to files (sorted on Field1, as
join does require input files to be sorted), then something like
join -a1 -a2 -t\| file1 file2 should do it. The main disadvantage of this method is that
join only works on two files at a time.
To do it in perl, your first idea is on the right track, and is one of the fastest/most efficient ways to do this with a couple minor optimizations:
- Make a single list of all unique ID values from all tables. As already suggested, a hash would be ideal for this, since it will eliminate any duplicates.
- Sort this list and sort all tables by ID.
- Walk the list of unique IDs and grab the matching record (if any) from each table. Since everything's sorted, you can just run a single query against each table and only advance a record when a match is found.
This gives you a total of two queries against each table, one to collect all IDs at the start, then one to grab the data by ID after that list is built.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.