Copy link to clipboard
Copied
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 ?
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 {
...
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);
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
...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
Clever to force the integer type in the for statement. Returning a mix of integers and strings could be dicey, but yes, nice idea.
Copy link to clipboard
Copied
When I try to $.writeln(nums); to see the result I get the error nums in undefined.
Copy link to clipboard
Copied
Sorry last line should be:
var nums = getFullRange(str);
Then,
$.writeln(nums);
Copy link to clipboard
Copied
Does my version not work for you? Or haven't you tried yet?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
@brianp311 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.
Copy link to clipboard
Copied
@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;
Copy link to clipboard
Copied
Thank you for this explanation.
Copy link to clipboard
Copied
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'))
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
No changes. It worked at first with an occasional crash, and then it just started to crash everytime.
Copy link to clipboard
Copied
Can you post the code please? I don't understand why it might crash. What OS are you using?
Copy link to clipboard
Copied
OK.
Break;
Please continue the discussion about the other (complete) script in the other thread.
----------------------------------------------------------------------------------------------------------------
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"
Have fun
😉
Copy link to clipboard
Copied
ok, continue the discussion here:
https://community.adobe.com/t5/indesign/start-page-number/m-p/11700186