• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Need help to sort index pages using InDesign script

Community Beginner ,
Jun 23, 2024 Jun 23, 2024

Copy link to clipboard

Copied

Hi All,

 

I am looking for the solution to fix Index pages.

I have an array and need pages sorted as per below example:

var myArray = [5, 6, 7, 43, 45, 65, 66, 77, 98, 99, 100, 123, 155, 166]

 

output = [5ff, 43, 45, 65f, 77, 98ff, 123, 155, 166]

 

so logic is if there is page range continue more than 2 thfn "ff" will add and if its just more than one time then "f". I am unable to logic this loop so any help will be appreciated.

 

Thanks,

Shonky

TOPICS
Scripting

Views

138

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 23, 2024 Jun 23, 2024

Something like the following should work

function convertArray(arr) {
    var i = 0;
    while (i < arr.length) {
        var start = i;
        while (i + 1 < arr.length && arr[i] + 1 === arr[i + 1]) {
            i++;
        }
        var end = i;
        if (end - start > 1) {
            arr[start] = arr[start] + 'ff';
            // Remove all elements in the middle of the sequence
            arr.splice(start + 1, end - start);
            i = start + 1; // Move to the next element after 
...

Votes

Translate

Translate
Community Expert ,
Jun 23, 2024 Jun 23, 2024

Copy link to clipboard

Copied

As your array is already sorted - you just need to check if next element is +1 of the current, if it is, then store index of this element, then keep checking - if only +1 - then add "f" to the saved index - if there is more - then add "ff".

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 24, 2024 Jun 24, 2024

Copy link to clipboard

Copied

Thanks Robert!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 23, 2024 Jun 23, 2024

Copy link to clipboard

Copied

Something like the following should work

function convertArray(arr) {
    var i = 0;
    while (i < arr.length) {
        var start = i;
        while (i + 1 < arr.length && arr[i] + 1 === arr[i + 1]) {
            i++;
        }
        var end = i;
        if (end - start > 1) {
            arr[start] = arr[start] + 'ff';
            // Remove all elements in the middle of the sequence
            arr.splice(start + 1, end - start);
            i = start + 1; // Move to the next element after the modified one
        } else if (end - start === 1) {
            arr[start] = arr[start] + 'f';
            // Remove the second element in the sequence
            arr.splice(start + 1, 1);
            i = start + 1; // Move to the next element after the modified one
        } else {
            i++;
        }
    }
    return arr;
}

var arr = [5, 6, 7, 43, 45, 65, 66, 77, 98, 99, 100, 123, 155, 166];
convertArray(arr);
$.writeln(arr);

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 24, 2024 Jun 24, 2024

Copy link to clipboard

Copied

Thanks Manan!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 24, 2024 Jun 24, 2024

Copy link to clipboard

Copied

LATEST

Hi all,

 

For those who use IdExtenso, the module $$.PageRange supports the `f`, `ff` suffixes (reduxPair and reduxMore parameters). Full code here with all options explained:

https://github.com/indiscripts/IdExtenso/blob/master/etc/$$.PageRange.jsxlib#L519

 

(Note: this algorithm is used in IndexMatic³.)

 

Best,

Marc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines