5 Replies Latest reply on Sep 5, 2017 10:01 AM by Chuck Uebele

    String array to int array.

    kamilz8948604

      Hello. In my aplication i save progress in txt files. Where i save all variables

      ....//example

      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

      0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

      0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

      0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

      0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

      0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

      ....

       

      My script load all fast and good, But problem is one. Its loades as String not Number

             var variablesList = [];  
            
              var variableFile= new File("C://Users//FCT//Documents//testVar.txt"); //open order.txt file
              variableFile.open('r');
              while (!variableFile.eof) { //get all lines from order txt file
      
      
      
      
              st = variableFile.readln();
              variablesList.push(st);
      
      
        } //end of all lines from order txt file 
              variableFile.close();
           alert(startFrom);
      apronBlackCounter=variablesList[0].split(',');
      apronFuchsiaCounter=variablesList[1].split(',');
      apronRedCounter=variablesList[2].split(',');
      apronRoyalBlueCounter=variablesList[3].split(',');
      

       

       

      How can i conver that to number when i cant use .map(Number);???

      I shuld make" for (var i=0;apronBlackCounter.length; i++){    apronBlackCounter[i] = parseInt(apronBlackCounter[i]);} ". But it wont work...

        • 1. Re: String array to int array.
          Tomas Sinkunas Adobe Community Professional

          what about this:

          var myArray = myString.split(",");
          

           

          This will convert string to array. Replace "," if you use other character as a separator.

          • 2. Re: String array to int array.
            SuperMerlin Level 5

            This may help.

            var variablesList = [];    
                   var variableFile= new File("C://Users//FCT//Documents//testVar.txt"); //open order.txt file  
                    variableFile.open('r');  
                    while (!variableFile.eof) { //get all lines from order txt file  
                    st = variableFile.readln();  
                    variablesList.push(st);   
              } //end of all lines from order txt file   
                    variableFile.close();  
               //  alert(startFrom);  
            apronBlackCounter=variablesList[0].split(',');  
            apronFuchsiaCounter=variablesList[1].split(',');  
            apronRedCounter=variablesList[2].split(',');  
            apronRoyalBlueCounter=variablesList[3].split(',');  
            alert(typeof(apronBlackCounter[0]));
            alert(typeof(Number(apronBlackCounter[0])));
            
            
            • 3. Re: String array to int array.
              Chuck Uebele Adobe Community Professional & MVP

              You can use several things to convert a string to a number. For what you're doing, I use:

               

              var myNum = parseInt('0')//will give an interger. This will strip any string elements like px after the number.

               

              var myNum = parseFloat('0')//will give an floating number

               

              var myNum = Number('0')//will not give just an integer if the original number is a floating point number. Will give NaN error if any string elements are included.

               

              var myNum = Math.round('0')//will give an interger and round the number.

               

              var myNum = Math.ceil('0')//will give an interger and always round up.

               

              var myNum = Math.floor('0')//will give an interger and always round down.

              • 4. Re: String array to int array.
                Jarda Bereza Level 4

                Here is polyfill for map: Array.prototype.map() - JavaScript | MDN

                1 person found this helpful
                • 5. Re: String array to int array.
                  Chuck Uebele Adobe Community Professional & MVP

                  Neat. learn something new from you all the time.