• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

String array to int array.

New Here ,
Sep 05, 2017 Sep 05, 2017

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

337

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Advocate ,
Sep 05, 2017 Sep 05, 2017

Copy link to clipboard

Copied

what about this:

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

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 05, 2017 Sep 05, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2017 Sep 05, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 05, 2017 Sep 05, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2017 Sep 05, 2017

Copy link to clipboard

Copied

LATEST

Neat. learn something new from you all the time.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines