|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
create dynamic actionscript syntax froma string
Hi,
I am trying to create a dynamic actionscript to do specific calculation with, for example: stringa="16+14"; stringb=some_function (stringa);//outputting 30 or, if even possible, to be able to perform all actiopnscript, like e.g. stringa=16; stringb="c=String(stringa);c=c.length;"; c=some_function(stringb);//c would output 2 Is this possible in Flash MX? greetings Patrick |
|
#2
|
|||
|
|||
|
because the content of your string is arbitrary I think you would have to parse it looking for operators etc...so this would work:
Code:
stringa="16+14";
stringb=some_function (stringa);//outputting 30
trace(some_function (stringa));
function some_function(arg) {
var operators = ["+","-","*","/","%"];
var op = false;
for (var i=0;i<operators.length;i++) {
if(arg.indexOf(operators[i]) >-1) {
op = operators[i];
var sides = arg.split(op);
break;
}
}
switch(op) {
case "+":
return Number(sides[0])+Number(sides[1]);
case "-":
return Number(sides[0])-Number(sides[1]);
case "*":
return Number(sides[0])*Number(sides[1]);
case "/":
return Number(sides[0])/Number(sides[1]);
case "%":
return Number(sides[0])%Number(sides[1]);
case false:
return false;
}
}
you could do a simliar thing with your second example if you knew the format it was going to be in beforehand, but not just eval(string) - that's not what eval is for. |
|
#3
|
|||
|
|||
|
Thanks for the code-example
![]() It's very helpfull to me! I'll probably be up all night again to do some coding with it... greetings and thanks Patrick |
![]() |
| Viewing: Dev Shed Forums > Web Design > Flash Help > create dynamic actionscript syntax froma string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|