Skip to main content
Participant
June 28, 2011
Question

Contraction of series of numbers

  • June 28, 2011
  • 1 reply
  • 507 views

I'm sure I've seen this script somewhere in the forums, but I haven't managed to find it regardless of the search terms I use.

Basically I'm trying to contract a series of numbers into a span of numbers.

For example:

     41, 42, 43, 44, 45

will become:

     41-45

After that I will write a script to further reduce that to '41-5' as suitable for indexes.

Of course, this is more of a pure javascript problem than a pure InDesign problem since we are just dealing with a string, then converting to an array of numbers, then contraction. But I'm sure I found it here... anyway.

Any help with finding this script/thread is much appreciated.

This topic has been closed for replies.

1 reply

Kasyan Servetsky
Legend
June 28, 2011

Here is what I wrote from the top of my head. There must be a more elegant solution.

var str = "50, 41, 42, 5, 43, 44, 45";
var arrStr = str.split(",");
var arrNum = [];

for (var i = 0; i < arrStr.length; i++) {
     arrNum.push(parseInt(arrStr));
}

arrNum.sort(sortNumber);
var range = arrNum[0] + " - " + arrNum[arrNum.length-1];

alert(range);

function sortNumber(a,b) {
     return a - b;
}

Regards,

Kasyan

BhSimonAuthor
Participant
June 28, 2011

Thank you for your prompt attention to my problem. The result should be an array of strings, rather than a number. As such, this is far from solved.

Anyone able to address the original problem of contracting spans of numbers?

I'm sure we've dealt with this before. My search terms are wanton.

In more detail: when finished with index generation in InDesign we often end up with a series of sequential numbers. The pure way to deal with this is to modify the index entires to deal with the relevant spans (via styles, page count, etc), but without the time (or client budget) I'm looking for a way to reduce a series of numbers into an array of number spans.

(Yes, I realise we are converting strings to numbers and then re-combining as strings, but I'm sure we've dealt with this before. Brain's trust, haven't we done this before?)

Inspiring
June 28, 2011

http://forums.adobe.com/thread/721494

Jeff