Skip to main content
the0bot
Known Participant
January 7, 2012
Question

interpret strings as literals...

  • January 7, 2012
  • 1 reply
  • 654 views

Is there a simple way to interpret a string value literally? I have "( object.propery.value <= object2.propery.value )" as user input in a text field and need to come up with the literal boolean value of (object.propery.value <= object2.propery.value).  The format is always the same, but I am having trouble with the boolean always being true for a non-empty string.

I tried

private function ifCompare(str:String) : Boolean

                    {

  var params:Object = str.split(" ");

                              var param1:Object = getValue(params[1]);

                              var operator:String = params[2];

                              var param2:Object = getValue(params[3]);

                              var eval:Boolean = false;

 

                              switch(operator)

                              {

                                             case ">=":

                                                            eval = (param1 >= param2);

                                                            break;

        case ">":

                                                            eval = (param1 > param2);

             break;

                                             case "<=":

                                                            eval = (param1 <= param2);

                                                             break;

                                             case "<":

                                                            eval = (param1 < param2);

             break;

                                             case "==":

                                              case "=":

                                                            eval = (param1 == param2);

                                                             break;

                                             case "!=":

                                              case "<>":

                                                            eval = (param1 != param2);

                                                             break;

                              }

                              return eval;

                    }

This returns mixed results and I still have the && and || to deal with.  It would be so much easier to be able to say "This is not a string! It is a literal!" Can I do that?

This topic has been closed for replies.

1 reply

_spoboyle
Inspiring
January 7, 2012

I have a demo here you might find useful

MyClass.as

package 

{

    import flash.text.TextField;

   

    public class MyClass

    {

        public var tf:TextField;

       

        public function MyClass()

        {

            tf = new TextField();

            tf.text = "somthing or other";

        }  

    }

}

and

package

{

    import flash.display.Sprite;

    import flash.events.Event;

    [SWF(backgroundColor="#ffffff", width=400, height=400, frameRate=60)]

    public class Main extends Sprite

    {

        public function Main()

        {

            if (stage)

            {

                init();

            }

            else

            {

                addEventListener(Event.ADDED_TO_STAGE, init);

            }

        }

       

        public function init(event:Event=null):void

        {

            removeEventListener(Event.ADDED_TO_STAGE, init);

           

            var myClass:MyClass = new MyClass();

           

            var string:String = "tf.text.length";

            var values:Array = string.split(".");

           

            trace(myClass[values[0]][values[1]][values[2]]);

        }

    }

}

I can't currently think of a way to get hold of the myClass instance though

the0bot
the0botAuthor
Known Participant
January 7, 2012

I will try this right now and let you know how it works