CreativePro Forum
Join our community of graphic designers, publishers, and production artists from around the world. Our members-only forum is a great place to discuss challenges and find solutions!
- You must be logged in to reply to this topic.Login
Stripping out the initial common numbers
- This topic has 11 replies, 3 voices, and was last updated 8 years, 4 months ago by
Masood Ahmad.
-
AuthorPosts
-
-
September 8, 2017 at 9:20 am #97895
Masood Ahmad
ParticipantHello Everyone,
I have a scenario wherein I have to manually remove the common first three digits from the secondary groups if they matches with the previous group. For example:
If the source numbers are: 405354, 405358, 405359
then it will become: 405354, 358, 359
i.e. all the common first three digits ‘405’ has been truncated from the second and third group.There might be different numbers in a given series. The group is always 6-digit numbers separated by a comma and a space. Below listed example shows the before and after for better understanding.
Source (Before):
405354, 405358, 405359, 405369, 305046, 305047, 405360, 405381, 405382, 405378Final (After):
405354, 358, 359, 369, 305046, 047, 405360, 381, 382, 378May I request some sort of GREP or a small JavaScript to achieve this. Meanwhile I’ll try more to have some GREP code.
Thanks in advance.
-
September 8, 2017 at 10:00 am #97898
Masood Ahmad
ParticipantI came up with this code, but it removes the duplicate initial numbers from the second group only.
GREP Find/Change:
Find What:(\d\d\d)(\d\d\d), \1
Change to:$1$2,…I’ll try more
-
September 8, 2017 at 10:37 am #97899
Mike Dean
MemberIf you can find an entire series of numbers with GREP in a script, you should be able to split them into an array, loop through them to compare each number with the preceding number, then add them to a new array. So if the first 3 of the current number match the first 3 of the preceding number, only add the last 3 numbers to the new array. Otherwise, add the number as is (hopefully that makes sense).
I’m extra bored today, so I threw together a function to handle the number checking (I’m sure there’s a cooler way to do this, but this seems to work). If the function works as you expect, you could expand the script to loop through multiple number series in the document, feed them to this function, and use the returned array to update the document.
var sourceNumbers = [405354, 405358, 405359, 405369, 305046, 305047, 405360, 405381, 405382, 405378];var numbers = removeCommonPrefix(sourceNumbers);
$.writeln(numbers);function removeCommonPrefix(numbers){
var updatedNumbers = [];var i = numbers.length-1;
while(i>=1){
var currentNumber = numbers[i];
var currentNumberPrefix = numbers[i].toString().slice(0,3);
var preceedingNumberPrefix = numbers[i-1].toString().slice(0,3);
var numberSuffix = numbers[i].toString().slice(3,6);if(currentNumberPrefix==preceedingNumberPrefix){
var number = numberSuffix;
}
else{
var number = currentNumber;
}updatedNumbers.unshift(number);
i--;
}//the first number will always need the initial 3, so at the end of the loop add it to the start
updatedNumbers.unshift(numbers[0]);return updatedNumbers;
}
-
September 9, 2017 at 1:36 am #97946
Masood Ahmad
ParticipantHi Mike, thanks for the code and suggestions. It seems interesting and hope it will work as expected. I’m away from my computer right now so I’ll test it on Monday.
Though I am not a script writer but it looks that you have taken the source numbers as constant values whereas they are not fix. They are of varied length and numbers. So I need a way out to feed the source numbers either from a selection or through input message box.
Hope I’m able to explain. -
September 9, 2017 at 7:24 am #97947
Michel Allio for FRIdNGE
ParticipantHi Masood,
I know you love videos! ;-)
(^/)
-
September 9, 2017 at 8:07 am #97948
Michel Allio for FRIdNGE
Participant… Of course, I’ve not used Mike’s code (even if it’s interesting!)! =D
(^/)
-
September 9, 2017 at 10:47 am #97949
Masood Ahmad
ParticipantOf course I love videos and this one is really good and it could be great if I could see the code :)
-
-
September 11, 2017 at 10:26 am #97969
Masood Ahmad
ParticipantHi Mike, The script is not working. As I requested earlier that I need a way out to feed the source numbers either from a selection or through input message box. Can you please look into this again and help me out with some updated and working code.
Thanks in advance.
-
September 11, 2017 at 1:05 pm #97975
Mike Dean
MemberAh, my mistake. It’s just a few more lines to run this based on a selection, so I’ve updated the script below. Just select a series of numbers and run the script and it should clean up the series. It should also work on numbers greater than 6 digits as long as it’s always the first 3 numbers that are common.
(function(){
var selection = app.selection[0];if(selection!="[object Text]" && selection!="[object Paragraph]" ){
alert("Please select text and try again.");
return;
}selection.contents = removeCommonPrefix(selection.contents.split(", ")).join(", ");
function removeCommonPrefix(numbers) {
var updatedNumbers =[];
var i = numbers.length -1;
while (i >= 1) {
var currentNumber = numbers[i];
var currentNumberPrefix = numbers[i].toString().slice(0, 3);
var preceedingNumberPrefix = numbers[i -1].toString().slice(0, 3);
var numberSuffix = numbers[i].toString().slice(3);
if (currentNumberPrefix == preceedingNumberPrefix) {
var number = numberSuffix;
} else {
var number = currentNumber;
}
updatedNumbers.unshift(number);
i--;
}
//the first number will always need the initial 3, so at the end of the loop add it to the start
updatedNumbers.unshift(numbers[0]);
return updatedNumbers;
}
})();
-
September 11, 2017 at 1:24 pm #97976
Michel Allio for FRIdNGE
ParticipantCome on Mike, one last little effort to deal with the whole document, as I did! ;-)
(^/)
-
September 12, 2017 at 5:51 am #97994
Mike Dean
MemberOk, ok, I’ll quit being lazy. :)
This has been updated to attempt to find all number series using GREP and update.
(function(){
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(d+, )((d)[, ]*)+";
var found = app.activeDocument.findGrep();for (var i=found.length-1; i>=0; i--){
var newText = removeCommonPrefix(found[i].contents.split(", ")).join(", ");
$.writeln("Replacing " + found[i].contents + "with " + newText)
found[i].contents = newText;
}function removeCommonPrefix(numbers) {
var updatedNumbers =[];
var i = numbers.length -1;
while (i >= 1) {
var currentNumber = numbers[i];
var currentNumberPrefix = numbers[i].toString().slice(0, 3);
var preceedingNumberPrefix = numbers[i -1].toString().slice(0, 3);
var numberSuffix = numbers[i].toString().slice(3);
if (currentNumberPrefix == preceedingNumberPrefix) {
var number = numberSuffix;
} else {
var number = currentNumber;
}
updatedNumbers.unshift(number);
i--;
}
//the first number will always need the initial 3, so at the end of the loop add it to the start
updatedNumbers.unshift(numbers[0]);
return updatedNumbers;
}
})();
-
September 12, 2017 at 9:57 am #97997
Masood Ahmad
ParticipantHi Mike,
Both the scripts worked like a charm, but I’ll prefer to use the first one. It will help me to replace and check my content.
As I said in my initial post, I’ll also try some GREP code. It seems I’m lucky to achieve it. I came up with three Grep Code sequence to run one by one. I wonder I might have to add subsequent codes, if the numbers are too lengthy. However, I’m happy as it is working on the existing text.
1st Run) GREP Find/Change:
Find What:(\d\d\d)(\d\d\d), \1
Change to:$1$2,2nd Run) GREP Find/Change:
Find What:(\d\d\d)(\d\d\d), (\d\d\d), \1
Change to:$1$2, $3,3rd Run) GREP Find/Change:
Find What:(\d\d\d)(\d\d\d), (\d\d\d), (\d\d\d), (\d\d\d), \1
Change to:$1$2, $3, $4, $5,Thanks Mike for giving me an instant solution. The script is going to help me a lot.
-
-
AuthorPosts
- You must be logged in to reply to this topic.
