PDA

View Full Version : Output random assortment of records


esmith
February 8th, 2012, 02:35 PM
At some point in the past I am pretty certain that Mr Korn assisted us by providing the following code which effectively allows you to output a range of records, much like any graphics program would allow such as "3, 7, 9-11, 14". We use it for customer-requested reprints of various records (very awesome):

// enter single records separated by comma (#,)
// and ranges within subarrays ([#,#])
var ranges = [ 250, 291, [264,266], 400 ];
// do not edit code below this line
function CheckRanges(val)
{
for (var i in ranges)
if (val == ranges[i] || ((ranges[i] instanceof Array) && val >= ranges[i][0] && val <= ranges[i][1]))
return true;
return false;
}
FusionPro.Composition.composeThisRecord = CheckRanges(CurrentRecordNumber());
Now, a few years later, I would like to modify that code to not only output ranges of records, but also assign a unique quantity to each one. To do this, I altered the above code as follows:

// [record,quantity] to reprint
var ranges = [ [250,1], [264,2], [266,1], [291,4], [400,4] ];
function CheckRanges(val)
{
for (var i in ranges)
if (val == ranges[i][0])
return [true,ranges[i][1]];
return [false,1];
}
var result = CheckRanges(CurrentRecordNumber());
FusionPro.Composition.composeThisRecord = result[0];
FusionPro.Composition.repeatRecordCount = result[1];
If I add a return at the end of the above OnRecordStart rule (for debugging purposes), "result" correctly returns as an array when validating, but composition returns a single page for each record AND the records returned do not match those in the ranges array. Is this another instance of the composition JavaScript engine not recognizing this code, or am I not using correct syntax somewhere?

mcmahand
February 9th, 2012, 08:26 AM
I think the problem is because of the repeat records. Try replacing this line:


var result = CheckRanges(CurrentRecordNumber());


Instead of CurrentRecordNumber, use FusionPro.Composition.inputRecordNumber


var result = CheckRanges(FusionPro.Composition.inputRecordNumbe r);

Dan Korn
February 9th, 2012, 10:17 AM
You've changed the meaning of the second value in the inner array, from an ending record number in a range to a repeat count, but the function isn't handling that properly.

If you're only reprinting individual pages anyway, and not ranges, then having an array and special handling for inner arrays isn't really necessary. Instead, I would just use a simple object to map page numbers to reprint counts, something like this:
var reprintCounts = {
250: 1,
264: 2,
266: 1,
291: 4,
400: 4,
}

FusionPro.Composition.repeatRecordCount = Int(reprintCounts[CurrentRecordNumber()]);
FusionPro.Composition.composeThisRecord = !!FusionPro.Composition.repeatRecordCount;Note that in FusionPro 8, you don't need that last line, as a repeat of zero suppresses the record automatically.

You could also write the rule to read the reprint information, including the counts, from an external data file.

P.S. I wouldn't call this outputting a "random assortment of records." It's merely a well-defined arbitrary set. Outputting a truly random set is possible, of course, and there are examples on this forum of doing that for things like lottery tickets, but that's not what you really want here.

esmith
February 9th, 2012, 11:03 AM
P.S. I wouldn't call this outputting a "random assortment of records." It's merely a well-defined arbitrary set.
Semantics nazi. ;)