4 Replies Latest reply on Sep 27, 2009 7:14 PM by myenters69

    Can you make this script shorter

    myenters69

      Can you make this script shorter,

      after

       

      //trace("j= "+j+" , k= "+k);

       

      in my attach file

      ....

        • 1. Re: Can you make this script shorter
          Ned Murphy Adobe Community Professional & MVP

          You can try the following, but don't throw away what you have.  Something like this might require some tweaking to determine if it answers the need.  I have to question the use of OUT_ARRAY values that might not exist yet, which is evident in your code.  But since this is AS2, it will likely compile and run okay, just showing undefined everywhere... AS3 would likely cough it up readily. I have replaced the 101 of them with an array. Someone else may come up with a different version, or possibly a correct one if this is flawed.

           

           

          var OUT_ARRAY:Array;
          var outputText = "";

           

          for(n=0; n<101; n++){
                if ((inputArray[n] == codeArray[j]) == true) {
                      OUT_ARRAY[n+1] = codeArray[k];
                      for (m=1; m<102; m++){
                            outputText += OUT_ARRAY[m];
                            if(m<101) outputText += " ";
                      }
                }
          }

          • 2. Re: Can you make this script shorter
            myenters69 Level 1

            I can't just declare that the OUT_ARRAY with [n] or [n+1], because i want to check an "input_text" text field with "code_text" text field that ....(i can write in english about it, let me show u what im doing with my script)

             

            if u copy this script in time line:

             

            //-----code for textField-----//
            this.createTextField("code_txt",this.getNextHighestDepth(),100,80,800,100);
            this.createTextField("input_txt",this.getNextHighestDepth(),100,260,800,100);
            this.createTextField("output_txt",this.getNextHighestDepth(),100,450,800,100);
            var txtFmt:TextFormat = new TextFormat();
            var txtFmt1:TextFormat = new TextFormat();
            var txtFmt2:TextFormat = new TextFormat();
            code_txt.type = "input";
            input_txt.type = "input";
            output_txt.type = "dynamic";
            code_txt.variable = "codeText";
            input_txt.variable = "inputText";
            output_txt.variable = "outputText";
            code_txt.border = true;
            input_txt.border = true;
            output_txt.border = true;
            code_txt.borderColor = 0xFFFFFF;
            input_txt.borderColor = 0xFFFFFF;
            output_txt.borderColor = 0xFFFFFF;
            code_txt.selectable = true;
            input_txt.selectable = true;
            output_txt.selectable = true;
            txtFmt.font = "Times New Roman";
            txtFmt1.font = "Times New Roman";
            txtFmt2.font = "Times New Roman";
            txtFmt.size = 12;
            txtFmt1.size = 12;
            txtFmt2.size = 12;
            txtFmt.color = 0xFFFFFF;
            txtFmt1.color = 0xFFFFFF;
            txtFmt2.color = 0xFFFFFF;
            code_txt.setNewTextFormat(txtFmt);
            input_txt.setNewTextFormat(txtFmt1);
            output_txt.setNewTextFormat(txtFmt2);
            //-----code for textField-----//
            //-----code for proccess_Btn-----//
            proccess_Btn.onRelease = function() {
            var code:String = codeText;
            var input:String = inputText;
            var codeArray:Array = code.split(" ");
            var inputArray:Array = input.split(" ");
            var codeArrayLength:Number = Math.floor(((codeArray.length)/2)-1);
            var inputArrayLength:Number = (inputArray.length);
            //trace(inputArray);
            //trace(inputArrayLength);
            //-----code for proccessor-----//
            for (i=0; i<=codeArrayLength; i++) {
              var j:Number = i*2;
              var k:Number = j+1;
              //trace("j= "+j+" , k= "+k);
              if ((inputArray[0] == codeArray[j]) == true) {
              var OUT_ARRAY1:Array = codeArray[k];
              outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
              //trace(inputArray[0]);
              }
              if ((inputArray[1] == codeArray[j]) == true) {
              var OUT_ARRAY2:Array = codeArray[k];
              outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
              }
              if ((inputArray[2] == codeArray[j]) == true) {
              var OUT_ARRAY3:Array = codeArray[k];
              outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
              }
              if ((inputArray[3] == codeArray[j]) == true) {
              var OUT_ARRAY4:Array = codeArray[k];
              outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
              }
              if ((inputArray[4] == codeArray[j]) == true) {
              var OUT_ARRAY5:Array = codeArray[k];
              outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
              }
              if ((inputArray[5] == codeArray[j]) == true) {
              var OUT_ARRAY6:Array = codeArray[k];
              outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
              }
              if ((inputArray[6] == codeArray[j]) == true) {
              var OUT_ARRAY7:Array = codeArray[k];
              outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
              }
              if ((inputArray[7] == codeArray[j]) == true) {
              var OUT_ARRAY8:Array = codeArray[k];
              outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
              }
              if ((inputArray[8] == codeArray[j]) == true) {
              var OUT_ARRAY9:Array = codeArray[k];
              outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
              }
              if ((inputArray[9] == codeArray[j]) == true) {
              var OUT_ARRAY10:Array = codeArray[k];
              outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
              }
              if ((inputArray[10] == codeArray[j]) == true) {
              var OUT_ARRAY11:Array = codeArray[k];
              outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
              }
            }
            //-----code for proccessor-----//
            };

             

            Drop a button in field and give that button an instance name "proccess_Btn", publish movie

            and then write down 20 words in "code_text"(first text box from above) with space between words ex. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

            and then write down 10 words in "input_text"(second text box from above) with the same words that have red color in "code_text"  ex. 7 7 9 15 17 19 11 1 5 3

             

            u will see that the last text field (from above) will hav an output like this: 8 8 10 16 18 20 12 2 6 4

             

            thats way i cant just write OUT_ARRAY with [variable]. Can u make that script shorter, but with the same result?,  i begging u...

            • 3. Re: Can you make this script shorter
              Ned Murphy Adobe Community Professional & MVP

              Did you try using what I provided or are you just assuming it won't work?

               

              I do not understand what you are trying to accomplish with this code, so I can only try to follow the pattern you repeat with it.  For what I see, you are using variables that do not exist all the way up thru the last 'if' conditional since you are declaring them as you go along.... for example, in the first conditional, all the variables I have colored red do not exist...

               

                if ((inputArray[0] == codeArray[j]) == true) {
                var OUT_ARRAY1:Array = codeArray[k];
                outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;
                }

              Ultimately, that whole line of code being assigned to the variable outputText does not need to be done until all of your looping is complete because whatever it ends up being will be defined by the last conditional to assign it.

               

              for (i=0; i<=codeArrayLength; i++) {
                var j:Number = i*2;
                var k:Number = j+1;
                if ((inputArray[0] == codeArray[j]) == true) {
                     var OUT_ARRAY1:Array = codeArray[k];
                }
                if ((inputArray[1] == codeArray[j]) == true) {
                     var OUT_ARRAY2:Array = codeArray[k];
                }
                if ((inputArray[2] == codeArray[j]) == true) {
                     var OUT_ARRAY3:Array = codeArray[k];
                }
                if ((inputArray[3] == codeArray[j]) == true) {
                     var OUT_ARRAY4:Array = codeArray[k];
                }
                if ((inputArray[4] == codeArray[j]) == true) {
                     var OUT_ARRAY5:Array = codeArray[k];
                }
                if ((inputArray[5] == codeArray[j]) == true) {
                     var OUT_ARRAY6:Array = codeArray[k];
                }
                if ((inputArray[6] == codeArray[j]) == true) {
                     var OUT_ARRAY7:Array = codeArray[k];
                }
                if ((inputArray[7] == codeArray[j]) == true) {
                     var OUT_ARRAY8:Array = codeArray[k];
                }
                if ((inputArray[8] == codeArray[j]) == true) {
                     var OUT_ARRAY9:Array = codeArray[k];
                }
                if ((inputArray[9] == codeArray[j]) == true) {
                     var OUT_ARRAY10:Array = codeArray[k];
                }
                if ((inputArray[10] == codeArray[j]) == true) {
                     var OUT_ARRAY11:Array = codeArray[k];
                }
              }

                outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;

              1 person found this helpful
              • 4. Re: Can you make this script shorter
                myenters69 Level 1

                i have try your code but still can not work,

                but this time youre right. i declaring variables that do not exist all the way up thru the last 'if', hehehe.

                i dont know that

                 

                outputText = OUT_ARRAY1+" "+OUT_ARRAY2+" "+OUT_ARRAY3+" "+OUT_ARRAY4+" "+OUT_ARRAY5+" "+OUT_ARRAY6+" "+OUT_ARRAY7+" "+OUT_ARRAY8+" "+OUT_ARRAY9+" "+OUT_ARRAY10;

                 

                can work hehehe.

                thx ned,

                but my code still too long

                ...