I'm building a class that will take a set of iterators and iterate over the combined output, merging records where appropriate. The client passes in a list of iterators and a comparison function for the expected records from the iterators. The iterators are assumed to be returning their values in some sorted fashion.
The problem I'm having is summarized as so:
- The iterators are destructive.
- Because they're destructive, I need to cache the values that aren't used in a given iteration for use in the next one.
- For those iterators that I do use the value this iteration, I need to remove the value in the cache for those iterators. This is so I can get the next value during the next iteration.
- The sorting routine (as I've defined it thus far) is expected to accept two records and return -1, 0, or 1 (as appropriate)
- But, how do I do something like my @indices = sort $sort_routine 0 .. $#records; when $sort_routine is expecting records, not indices?
The cache is a parallel array of records, each index corresponding to an iterator in @iterators. So, I could do something like:
RECORD:
for my $record (@merged_records)
{
my $index;
for $index (0 .. $#cache)
{
if ($cache[$index] eq $record)
{
$cache[$index] = 0;
next RECORD;
}
}
die "Cannot find record to remove in cache!\n";
}
But, that depends on stringifying the references, and I'm not comfortable with that. Plus, the iterators might not be returning references, so this would fail.
Help!
------
We are the carpenters and bricklayers of the Information Age.
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
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.