Jump to content

Output random assortment of records


Recommended Posts

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?

Edited by esmith
Link to comment
Share on other sites

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.inputRecordNumber);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...