# Convert string into Actionscript Code II

**URL:** <https://forum.kirupa.com/t/convert-string-into-actionscript-code-ii/184344>\
**Category:** flash\
**Created:** [April 4, 2006, 3:00pm UTC](https://forum.kirupa.com/t/convert-string-into-actionscript-code-ii/184344 "2006-04-04T15:00:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![saavedrajj](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/saavedrajj/32/1984_2.png) [@saavedrajj](https://forum.kirupa.com/u/saavedrajj)\
**Post date:** [April 4, 2006, 3:00pm UTC](https://forum.kirupa.com/t/convert-string-into-actionscript-code-ii/184344/1 "2006-04-04T15:00:59Z")

</div>

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](http://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) =&gt; ['1','+',['5','-','2'],'/','7',[['25','+','3'],'*','2']]
var arithArr = [];
var currStr = "";
var arrayScope = [arithArr];
for(var i=0 ; i&lt;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 =&gt; ['7','+','2','/','8','-','12']
function arithStrToArray(arithStr){
    var ar = [];
    var currStr = "";
    for(var i=0 ; i&lt;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&lt;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&lt;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&lt;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&lt;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\>
