Skip to main content
HWDP
Participating Frequently
September 5, 2017
Question

String array to int array.

  • September 5, 2017
  • 4 replies
  • 462 views

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 = parseInt(apronBlackCounter);} ". But it wont work...

This topic has been closed for replies.

4 replies

Jarda Bereza
Inspiring
September 5, 2017

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

Chuck Uebele
Community Expert
Community Expert
September 5, 2017

Neat. learn something new from you all the time.

Chuck Uebele
Community Expert
Community Expert
September 5, 2017

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.

SuperMerlin
Inspiring
September 5, 2017

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])));

Tomas Sinkunas
Legend
September 5, 2017

what about this:

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

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