Stripping out the initial common numbers

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Stripping out the initial common numbers

Viewing 9 reply threads
  • Author
    Posts
    • #97895
      Masood Ahmad
      Participant

      Hello 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, 405378

      Final (After):
      405354, 358, 359, 369, 305046, 047, 405360, 381, 382, 378

      May 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.

    • #97898
      Masood Ahmad
      Participant

      I 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

    • #97899
      Mike Dean
      Member

      If 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;
      }

    • #97946
      Masood Ahmad
      Participant

      Hi 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.

    • #97947

      Hi Masood,

      I know you love videos! ;-)

      (^/)

      • #97948

        … Of course, I’ve not used Mike’s code (even if it’s interesting!)! =D

        (^/)

      • #97949
        Masood Ahmad
        Participant

        Of course I love videos and this one is really good and it could be great if I could see the code :)

    • #97969
      Masood Ahmad
      Participant

      Hi 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.

    • #97975
      Mike Dean
      Member

      Ah, 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;
      }
      })();

    • #97976

      Come on Mike, one last little effort to deal with the whole document, as I did! ;-)

      (^/)

    • #97994
      Mike Dean
      Member

      Ok, 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 \n" + found[i].contents + "\nwith \n" + 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;
      }
      })();

    • #97997
      Masood Ahmad
      Participant

      Hi 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.

Viewing 9 reply threads
  • You must be logged in to reply to this topic.
>