FMX: adding external variables

I understanding how to add, multiply, etc. external, standalone numerical variables:

var salBase = new LoadVars();
salBase.Sal = new Number();
salBase.MA = new Number();
salBase.onLoad = function(ok) {
trace(salBase.Sal+ +salBase.MA);
if (ok) {
sal_txt.text = Number(salBase.MA) + Number(salBase.MA)
}
};
salBase.load(“TeachSals.txt”);

where salBase.MA = 3000.

However, I really want to add salBase.MA to salBase.Sal

where salBase.Sal =

30500%0A30700%0A30900%0A31100%0A31300%0A31500%0A31700%0A32000%0A32300%0A32600%0A32900%0A33200%0A33730%0A34450%0A35230%0A35950%0A36980%0A38220%0A39460%0A40900%0A42300%0A43780%0A45220%0A46590%0A48025

(%0A is unicode for linefeed.)

However, I get NaN – that’s because the %0A makes the variable a non_numerical string, right?

So, what;s the best way?

Thanks,

Andy

do you want Sal to contain multiple numbers? like an array of numbers?

[AS]
var salBase = new LoadVars();
//salBase.Sal = new Number(); //this does nothing, since LoadVars
//salBase.MA = new Number(); // always imports strings
salBase.onLoad = function(ok) {
trace(salBase.Sal+ +salBase.MA);
if (ok) {
//this is where your Number conversions go (as before)
sal_txt.text = Number(salBase.MA) + Number(salBase.MA)
//or if you want multiple values from a string like salBase.MA
//split the string first with delimiter “
” which is %0A
//then convert each of the resulting strings into a Number
var MA_array = salBase.MA.split("
");
for(index in MA_array)
MA_array[index] = Number(MA_array[index]);
//‘new’ is optional with the Number function
//then to add them all together,
var sum = 0;
for(index in MA_array)
sum += MA_array[index];
//it would better to perform both actions with a single for loop
//like this
for(index in MA_array)
sum += Number(MA_array[index]);
sal_txt.text = sum;
}
};
salBase.load(“TeachSals.txt”);[/AS]

Woo! Almost there!

So I modified your script somewhat to my needs:

[AS]var salBase = new LoadVars();
//salBase.Sal = new Number(); //this does nothing, since LoadVars
//salBase.MA = new Number(); // always imports strings
salBase.onLoad = function(ok) {
// trace(salBase.Sal+ +salBase.MA);
if (ok) {
//this is where your Number conversions go (as before)
//sal_txt.text = Number(salBase.Sal) + Number(salBase.MA)
//or if you want multiple values from a string like salBase.MA
//split the string first with delimiter “
” which is %0A
//then convert each of the resulting strings into a Number
var Sal_array = salBase.Sal.split("###");
for(index in Sal_array)
Sal_array[index] = Number(Sal_array[index]);
//‘new’ is optional with the Number function
//then to add them all together,
var sum = 0;
// for(index in Sal_array)
// sum += Sal_array[index];
//it would better to perform both actions with a single for loop
//like this
for(index in Sal_array)
sum += Number(Sal_array[index]);
sal_txt.text = sum;
trace(salBase.Sal.split("###"));
trace(Number(Sal_array[0]) + Number(salBase.MA));
}
};
salBase.load(“TeachSals.txt”);
[/AS]

However, I don’t want to add all the values of Sal together; instead, I would like to add the 1 value of MA to each Sal value:

&Sal=30500###30700###30900###31100###31300###31500###31700###

32000###32300###32600###32900###33200###33730###34450###35230###35950###36980###38220###39460###40900###42300###43780###45220###46590###48025&MA=3000&SP=4000&PhD=5000

Also, what is the best way to format this into currency when I’m done?

I know it would be quicker/easier to just list the already computated values, but I already know how to do that and want to learn more. So thank you for your time and help.

Thanks,

Andy