Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Answered How to append incremental integer to a string

runfast

Coder
I have duplicate records which I am trying to append a number at the end of the duplicate record name so that it is no longer a duplicate. I am staging this data for a subsequence process.
For Example: I have this records in an array:
Code:
var myArray = ["apple-DUP-DELETE","apple-DUP-DELETE","apple-DUP-DELETE","Orange-DUP-DELETE","Orange-DUP-DELETE"]
for(i=0;i<myArray.length;i++) {
var reName =myArray[i];
renName = reName + 1;\\ something like this which can initiate the incremental increase (number)
gs.log(reName);}
This would create an output:
output:
Code:
apple-DUP-DELETE1
apple-DUP-DELETE1
apple-DUP-DELETE1
which is still a duplicate. So, I am looking for the Majic trick to get these results:
Code:
apple-DUP-DELETE1
apple-DUP-DELET2
apple-DUP-DELETE3
 
Solution
You have two variables: renName and reName. Are they supposed to be different?
If they're supposed to be the same, then change one of them to the other, and it should work well.
If they're supposed to be different, then just change gs.log(reName);} to gs.log(renName);}
Change that 1 to i, so it will increment with the loop.
Thanks Johna for looking this for me. so i have tried that by changing the 1 to i as showed below:
Code:
var myArray = ["apple-DUP-DELETE","apple-DUP-DELETE","apple-DUP-DELETE","Orange-DUP-DELETE","Orange-DUP-DELETE"]
for(i=0;i<myArray.length;i++) {
var reName =myArray[i];
renName = reName + i;\\ something like this which can initiate the incremental increase (number)
gs.log(reName);}
and here is the output i get when i run it:
Code:
*** Script: apple-DUP-DELETE
*** Script: apple-DUP-DELETE
*** Script: apple-DUP-DELETE
*** Script: Orange-DUP-DELETE
*** Script: Orange-DUP-DELETE
 
You have two variables: renName and reName. Are they supposed to be different?
If they're supposed to be the same, then change one of them to the other, and it should work well.
If they're supposed to be different, then just change gs.log(reName);} to gs.log(renName);}
 
Solution
You have two variables: renName and reName. Are they supposed to be different?
If they're supposed to be the same, then change one of them to the other, and it should work well.
If they're supposed to be different, then just change gs.log(reName);} to gs.log(renName);}
They are supposed be same. Typo on mypart🙂 it works like charm. Thanks again for pointing that out.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom