4 Replies Latest reply on Jun 28, 2011 5:35 PM by BhSimonP

# Contraction of series of numbers

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.

• ###### 1. Re: Contraction of series of numbers

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[i]));
}

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

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

Regards,

Kasyan

• ###### 2. Re: Contraction of series of numbers

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?)