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

break a range of numbers into an array from dialog box

Engaged ,
Jan 06, 2021 Jan 06, 2021

I'm getting numbers from a dialog and they may be like this, 1-10, 20, 51,60-62 etc. How would I go about checking for the hyphen and creating an array like: 1,2,3,4,5,6,7,8,9,10,20,51,60,61,62 ?

TOPICS
Scripting
1.5K
Translate
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 3 Correct answers

Community Expert , Jan 06, 2021 Jan 06, 2021

You need to roll your own function: 

 

var getInbetweens = function(first, last, nums) {
  for (var i = first; i <= last; i++) {
     nums.push(i.toString());
  }
}

var getFullRange = function(str) {
   var nums = [];
   var hyphenSplit;
   var strArr = str.split(",");
   for (var i = 0; i < strArr.length; i++) {
      if (strArr[i].indexOf("-") >=0) { 
         hyphenSplit = strArr[i].split("-");
         getInbetweens(parseInt(hyphenSplit[0]), parseInt(hyphenSplit[1]), nums);
      } else {
 
...
Translate
Community Expert , Jan 06, 2021 Jan 06, 2021

The same - or not?

😉

 

var str = "1-10, 20, 51, 60-62";

str = str.replace (/ /g, "");
var arr = str.split(",");
var part = null;

var result = [];
for (i=0; i<=arr.length-1; i++) {
    if ( arr[i].match (/\d+-\d+/) ) {
        part = arr[i].split("-");        
        for (n = part[0]; n <= part[1]; n++) {
            result.push(n);
        }
    } else {
        result.push(arr[i]);
        }
    }

alert(result);

 

Translate
Participant , Jan 07, 2021 Jan 07, 2021

Hi, earlier I already make a script for you with a similar task. Remember?

https://community.adobe.com/t5/indesign/start-page-number/m-p/11700186?page=1

You can take one of functions and receive somthing like this:

Array.prototype.indexOf = function(item){
	for(var i = 0; i < this.length; i++){if(this[i] === item){return i}}
	return -1
}

function parse(mstr){
	var rez = []
	for(var i = 0; i < mstr.split(',').length; i++){
		if(mstr.split(',')[i] == ''){continue}

		var mcur = mstr.split(',')[i].sp
...
Translate
Community Expert ,
Jan 06, 2021 Jan 06, 2021

You need to roll your own function: 

 

var getInbetweens = function(first, last, nums) {
  for (var i = first; i <= last; i++) {
     nums.push(i.toString());
  }
}

var getFullRange = function(str) {
   var nums = [];
   var hyphenSplit;
   var strArr = str.split(",");
   for (var i = 0; i < strArr.length; i++) {
      if (strArr[i].indexOf("-") >=0) { 
         hyphenSplit = strArr[i].split("-");
         getInbetweens(parseInt(hyphenSplit[0]), parseInt(hyphenSplit[1]), nums);
      } else {
         nums.push(strArr[i]);
      }
   }
   return nums;
}

var str = "1-10,20,51,60-62";
var nums = getFullRange(str);

 

You'll probably also want to handle user input errors. Not sure if a string is being return from the dialog box, but you'll probably want to do a .replace on spaces and validate with isNaN. 

Translate
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 ,
Jan 06, 2021 Jan 06, 2021

The same - or not?

😉

 

var str = "1-10, 20, 51, 60-62";

str = str.replace (/ /g, "");
var arr = str.split(",");
var part = null;

var result = [];
for (i=0; i<=arr.length-1; i++) {
    if ( arr[i].match (/\d+-\d+/) ) {
        part = arr[i].split("-");        
        for (n = part[0]; n <= part[1]; n++) {
            result.push(n);
        }
    } else {
        result.push(arr[i]);
        }
    }

alert(result);

 

Translate
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 ,
Jan 07, 2021 Jan 07, 2021

Clever to force the integer type in the for statement. Returning a mix of integers and strings could be dicey, but yes, nice idea. 

Translate
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
Engaged ,
Jan 06, 2021 Jan 06, 2021

When I try to $.writeln(nums); to see the result I get the error nums in undefined. 

Translate
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 ,
Jan 06, 2021 Jan 06, 2021

Sorry last line should be: 

 

var nums = getFullRange(str); 

 

Then, 

$.writeln(nums);

Translate
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 ,
Jan 07, 2021 Jan 07, 2021

Does my version not work for you? Or haven't you tried yet?

Translate
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
Engaged ,
Jan 07, 2021 Jan 07, 2021

Yes your version worked, thank you. I just wanted to go through both to understand how they worked, which is apparently just outside my ability. 

This part confuses me.

 if ( arr[i].match (/\d+-\d+/) ) {
        part = arr[i].split("-");        
        for (n = part[0]; n <= part[1]; n++) {
            result.push(n);
        }

 

I think... you find digits seperated by a hyphen, then split them by the hypens into seperate strings and assign it to part. Then loop, set n to the first string, and if it's less than or equal to the second sting then add 1. How does it break up the second instance of the hyphn? If I have 1-6, 10-20 wouldn't part[0] be 1 and part[1] be 2?

Translate
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 ,
Jan 07, 2021 Jan 07, 2021

 

@brian_p_dts  wrote:
"Clever to force the integer type in the for statement. Returning a mix of integers and strings could be dicey, but yes, nice idea. "


If there is error management (to exclude everything but digits) there is an easy way to get digits (just replace in this part with the comments):

 

// the first lines of my code
        for (n = part[0]; n <= part[1]; n++) {
            result.push(n); // n*1 for numbers
        }
    } else {
        result.push(arr[i]); // arr[i]*1 for numbers
        }
// the rest of my code

 

 

Or you use  result.push(Number(arr[i])) or other similar technique for that.

But then it must be ensured that only numbers, spaces and hyphens are passed. Otherwise you'll get a NAN error message.

Translate
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 ,
Jan 07, 2021 Jan 07, 2021



@davidn5918184  wrote:

"… How does it break up the second instance of the hyphn? If I have 1-6, 10-20 wouldn't part[0] be 1 and part[1] be 2? …"

 

 if ( arr[i].match (/\d+-\d+/) ) {
        part = arr[i].split("-"); 

This creates an array - the string has been divided by the hyphen and the hyphen is removed.

For arr[0] = "1-10";

you will get

part = [1, 10];

part[0] = 1;

part[1] = 10;

Translate
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
Engaged ,
Jan 07, 2021 Jan 07, 2021
LATEST

Thank you for this explanation. 

 

Translate
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
Participant ,
Jan 07, 2021 Jan 07, 2021

Hi, earlier I already make a script for you with a similar task. Remember?

https://community.adobe.com/t5/indesign/start-page-number/m-p/11700186?page=1

You can take one of functions and receive somthing like this:

Array.prototype.indexOf = function(item){
	for(var i = 0; i < this.length; i++){if(this[i] === item){return i}}
	return -1
}

function parse(mstr){
	var rez = []
	for(var i = 0; i < mstr.split(',').length; i++){
		if(mstr.split(',')[i] == ''){continue}

		var mcur = mstr.split(',')[i].split('-')
		if(mcur[0] == '' && mcur[1] == ''){continue}
		
		var start = mcur[0] == '' ? mcur[1] : mcur[0]
		var end = mcur[1] == '' || typeof mcur[1] == 'undefined' ? mcur[0] : mcur[1]
		
		for(var ii = Number(start); ii <= Number(end); ii++){
			if(rez.indexOf(ii) == -1){rez.push(ii)}
		}
	}
	return rez
}

alert(parse('1-10, 20, 51,60-62'))
Translate
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
Engaged ,
Jan 07, 2021 Jan 07, 2021

Yes I remember and thank you.

It does work and I have spent hours trying to understand it.

But I've run into an issue,  your script keeps crashing InDesign and it is outside of my ability to understand why so what I'm really trying to do is figure out why. How it works, how the loop processes it. Ultimately my goal is to get a deeper understanding.

Translate
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
Participant ,
Jan 07, 2021 Jan 07, 2021

Has my script crashed?

https://community.adobe.com/t5/indesign/start-page-number/m-p/11700186?page=1

Have you changed the code?
You can write this instead of the last line:

alert(pages)

start the script, insert into textbox 1-10, 20, 51,60-62 and ... what is happened? I have no problem. 

Translate
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
Engaged ,
Jan 07, 2021 Jan 07, 2021

No changes. It worked at first with an occasional crash, and then it just started to crash everytime. 

Translate
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
Participant ,
Jan 07, 2021 Jan 07, 2021

Can you post the code please? I don't understand why it might crash. What OS are you using?

Translate
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 ,
Jan 07, 2021 Jan 07, 2021

OK.

 

Break;

 

Please continue the discussion about the other (complete) script in the other thread.

 

 

----------------------------------------------------------------------------------------------------------------

 

@davidn5918184 

Now you have three correct answers here in this thread. If you have any questions about any or all of these variants, please ask. Otherwise "you should"

  1. Understand how these variants work and
  2. Mark each of these three answers as Correct Answer.

 

Have fun

😉

 

Translate
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
Participant ,
Jan 07, 2021 Jan 07, 2021
Translate
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