6 Replies Latest reply on Oct 24, 2015 12:22 PM by try67

    identify numbers in a string

    MatLac Level 1

      I have a field where users input alphabetical characters which are later put in an array and counted.  For example, If I enter the string "ABCABC", the algorythm determines that there are 2A's, 2B's and 2C's.  AABBCC will give the same result.

       

      I want to refine this algorythm where I will allow the user to input numbers and use algebra to input the same thing.  In our example, 2ABC would provide the desired result.

       

      I figure I need a way to identify numbers within a string.  Then I need to isolate each algebric statement so that 2AB3CD would be interpreted as 2xAB and 3xCD.  It also need to take into acount that the user could input 2-digits number such as 10AB.

       

      What I had in mind is to loop trought each character and try to force a number Type on them Number().  I guess letters would return errors.  Then I guess logging the index of each character and using some sort of reference scheme to rebuild each algebric statement would be the way to do it.

       

      I could also try the split() method to split at whaterver number encoutered....

       

      All in all, I just need to transform a string containing numbers to a string with only letters.  The final order is irrelevant so a simple push() at the end would be fine.

       

      Any thougths?

        • 1. Re: identify numbers in a string
          try67 MVP & Adobe Community Professional

          First of all you need to define very well how it is going to work. You wrote:

          2AB3CD would be interpreted as 2xAB and 3xCD

          Why? Maybe it's AABCCCD? If you want to allow the numbers to apply to a group of letters then you need to either specify the number for single letters as well (eg. 2AB3C1D = ABABCCCD), or use some kind of delimiter for longer groups (eg. 2[AB]3CD = ABABCCCD). Otherwise it's too ambiguous.

          • 2. Re: identify numbers in a string
            Test Screen Name Most Valuable Participant

            In JavaScript you can test if something is a digit by seeing if it is between '0' and '9' inclusive. No other characters will match.

            • 3. Re: identify numbers in a string
              MatLac Level 1

              Of course, I haven't thougth of that.  I think I am going to allow spaces and specify the users that a letter that is part of a block stating with a number will be multiplied by that number.  That way

              2AB = AABB

              2A B = AAB

              2A BC = AABC

              2AB C = AABBC

              2ABC =AABBCC

              A2BC = ABBCC

               

              It will use only one space instead of 2 () and eliminate margin for errors such as entering only ( or ))

              • 4. Re: identify numbers in a string
                try67 MVP & Adobe Community Professional

                Using spaces is a good idea, but then the last one needs to be "A 2BC".

                If you do it like that then you can use split(" ") to separate the items and then a simple RegExp to identify the numbers.

                After that it should be pretty simple to do the rest.

                • 5. Re: identify numbers in a string
                  MatLac Level 1

                  @try67 That's excatly what I was trying to avoid.  That is having the user to "have" to separate expressions if it is not absolutly needed.  Basically, I wanted to evaluate A2BC, A 2BC or A2B2C to the same result being ABBCC.  The only reason a whitespace should be entered is to separate a letter FOLLOWING a number that should not get multiplied by that number.  So once expressions are separated by their spaces, I needed to address these problems:

                   

                  Letters before numbers A3B -> ABBB

                  Double digit numbers  A10B -> ABBBBBBBBBB and not 0 times B

                  Expressions not separated by white space 3A3B -> AAABBB

                   

                  Luckily, I worked on it this morning and managed to have quite a good piece of code.  I tried many combinations and it didn't fail.  The workflow is as following.

                  1-Separating expressions by splitting at spaces;

                  2-Pushing complete expressions without multipliers;

                  3-Pushing letters that are in front of numbers (handles events like A2BC)

                  4-Once only algebraic expessions are left, separate multipliers from letters in two separate arrays

                  5-Push letters a number of time equal to their corresponding multiplier (at same index)

                   

                  here's the code with explanations and results printed to the console:

                   

                  //digits

                  console.clear();

                  var re = /\d/;

                   

                   

                  //Creating a final array

                  var finalArray = [];

                  //extract the original string

                  var vField = this.getField("stat").valueAsString;

                  console.println("Field value is: "+vField);

                  //Isolating expressions in an Array

                  var aExpress = vField.split(" ");

                  console.println("Expressions are: "+aExpress);

                   

                   

                  //working on each expression one at a time

                  for (i in aExpress){

                    var myExpress = aExpress[i];

                   

                    //Pushing chars to finalArray until a multiplier is found

                    var j = 0;

                    var myChar = myExpress.charAt(j);

                    for (; j <= myExpress.length-1; j++){

                    myChar = myExpress.charAt(j);

                    if (!re.test(myChar)) finalArray.push(myChar);

                    else break;

                    }

                   

                    //as soon as a multiplier is found

                    var aMulti = [];

                    var aChar = [];

                    //separate multipliers from chars in two arrays

                    //with corresponding indexes

                    while (j <= myExpress.length-1){

                    var sMulti = "";

                    var sChar = "";

                    //Push numbers to the multiplier array until a char is found

                    do{

                    myChar = myExpress.charAt(j);

                    sMulti += myChar;

                    j++;

                    myChar = myExpress.charAt(j);

                    } while ((re.test(myChar))&&(j <= myExpress.length-1))

                    //change strings to array of numbers

                    aMulti.push(Number(sMulti));

                   

                    //Push letters to the char array until a number is found

                    do {

                    myChar = myExpress.charAt(j);

                    sChar += myChar;

                    j++;

                    myChar = myExpress.charAt(j);

                    } while ((!re.test(myChar))&&(j <= myExpress.length-1))

                    //change strings to arrays of arrays

                    var sCharSplit = sChar.split("");

                    aChar.push(sCharSplit);

                    }

                   

                    //for each expression in the array of expressions

                    for (var k in aChar){

                    var letterBlock = aChar[k];

                    //for each character in the expression

                    for (var l in letterBlock){

                    var myMultiplier = aMulti[k];

                    var myCharacter = letterBlock[l];

                    //push it to the finalArray

                    //until you reach the value of the multiplier

                    for (;myMultiplier > 0; myMultiplier--) finalArray.push(myCharacter);

                    }

                    }

                    console.println("Multipliers are: "+aMulti);

                    console.println("Characters are: "+aChar);

                  }

                  console.println("FinalArray is: "+finalArray);

                  • 6. Re: identify numbers in a string
                    try67 MVP & Adobe Community Professional

                    There's always a trade-off between the amount of flexibility you want to give the users and the complexity it adds to the code.

                    At any rate, it's good to hear you were able to come up with a solution that works for you...