Please… help me with this.
The function above arithmeticParser() by abeall works great BUT,
it doesn’t read variables, if I have a variable name “var1” or an instance name “var1.text”
How can I assign to the array the value instead of the variable string?
actionscript layer1:
<code>
/************
/* String arithmetic parsing function - abeall.com
/* takes a string like “1+(5-2)/7((25+3)2)" and does the arithmetic, respecting parenthesis
/ returns: Number
/************/
function arithmeticParser(str){
if(typeof str!=‘string’)return str;
//clean up
str = str.split(” ").join(’’).split(“Number”).join(’’);
//CORE ALGORITHM///////////////////////////////////
//create nested arithmetic array based on parenthesis
//ex: 1+(5-2)/7((25+3)*2) => ['1','+',['5','-','2'],'/','7',[['25','+','3'],'*','2']]
var arithArr = [];
var currStr = "";
var arrayScope = [arithArr];
for(var i=0 ; i<str.length ; i++){
var char = str.charAt(i);
var currArr = arrayScope[arrayScope.length-1];
if(char=="("){
if(currStr!="")combineArray(currArr,currArr.length,arithStrToArray(currStr.split("(").join()));
currStr = "";
arrayScope.push(currArr[currArr.length]=new Array());
}else if(char==")"){
combineArray(currArr,currArr.length,arithStrToArray(currStr.split(")").join()));
currStr = "";
arrayScope.pop();
}else{
currStr = currStr + char;
}
}
combineArray(currArr,currArr.length,arithStrToArray(currStr));
//dump(arithArr);
str = arrayArithmetic(arithArr);
return str;
//FUNCTIONS////////////////////////////////////////
//splices all elements in the Array 'insertArr' into the Array 'ar' at 'index'
function combineArray(ar,index,insertArr){
for(var i in insertArr){
ar.splice(index,0,insertArr*);
}
}
//performs arithmetic() to 3D arrays from deepest array to the top array
function arrayArithmetic(ar){
for(var i in ar){
if(typeof ar*=='object'){
ar* = arrayArithmetic(ar*);
}
}
return arithmetic(ar);
}
//converts arithmetic string to array
//ex: 7+2/8-12 => ['7','+','2','/','8','-','12']
function arithStrToArray(arithStr){
var ar = [];
var currStr = "";
for(var i=0 ; i<arithStr.length ; i++){
var char = arithStr.charAt(i);
if(char=="/" || char=="*" || char=="-" || char=="+"){
if(currStr!="")ar.push(currStr);
ar.push(char);
currStr = "";
}else{
currStr = currStr+String(char);
}
}
if(currStr!="")ar.push(currStr);
return ar;
}
//performs the four basic arithmetic operations to arithmetic formatted array
function arithmetic(ar){
for(var i=0 ; i<ar.length ; i++){
if(ar*=="/"){
var n = Number(ar[i-1]) / Number(ar[i+1]); //perform operation
ar.splice(i-1,3,n); //splice out the number,operation,number subarray and replace with the answer number
i=0; //since the array has been modified(spliced) the operation must start over to get all operations
}
}
for(var i=0 ; i<ar.length ; i++){
if(ar*=="*"){
var n = Number(ar[i-1]) * Number(ar[i+1]);
ar.splice(i-1,3,n);
i=0;
}
}
for(var i=0 ; i<ar.length ; i++){
if(ar*=="-"){
var n = Number(ar[i-1]) - Number(ar[i+1]);
ar.splice(i-1,3,n);
i=0;
}
}
for(var i=0 ; i<ar.length ; i++){
if(ar*=="+"){
var n = Number(ar[i-1]) + Number(ar[i+1]);
ar.splice(i-1,3,n);
i=0;
}
}
return ar[0]; //the array should be down to one element: the answer
}
}
</code>
actionscript layer2: (usage)
<code>
myString = “(var1 * 2) + 1000”;
result.text= arithmeticParser(myString);
</code>