8 Replies Latest reply on Apr 13, 2008 2:10 PM by Newsgroup_User

    Variable to property

    Level 7
      Okay, say I have a property list:

      Names = [#Alice: 13, #Bob: 24, #Carol: 41]

      If I want to access #Bob, it's as easy as saying:

      put Names.Bob
      -- 24

      Yay. So, say I have a variable that I want to use as the property to check:

      var = "Bob"

      How do I make it check that property in the list? I can't use "Names.var",
      because "var" isn't a property on that list. "getAt" and "getOne" return
      nothing. "getProp", "getaProp", and "getPropAt" just error. There's got to
      be some simple way to do this that I'm overlooking...


        • 1. Re: Variable to property
          Level 7
          put Names[var]

          -- Brian

          "Darrel Hoffman" <no.address@all.com> wrote in message
          news:ftqqdt$9uu$1@forums.macromedia.com...
          > Okay, say I have a property list:
          >
          > Names = [#Alice: 13, #Bob: 24, #Carol: 41]
          >
          > If I want to access #Bob, it's as easy as saying:
          >
          > put Names.Bob
          > -- 24
          >
          > Yay. So, say I have a variable that I want to use as the property to
          > check:
          >
          > var = "Bob"
          >
          > How do I make it check that property in the list? I can't use
          > "Names.var", because "var" isn't a property on that list. "getAt" and
          > "getOne" return nothing. "getProp", "getaProp", and "getPropAt" just
          > error. There's got to be some simple way to do this that I'm
          > overlooking...
          >


          • 2. Re: Variable to property
            Level 7
            > put Names[var]

            *sputters* Dammit, I thought I tried that! And wait - yes, yes as a matter
            of fact I did try that, except I believe I've stumbled onto the one and only
            case where Director is actually case-sensitive.

            Names = [#Alice: 13, #Bob: 24, #Carol: 41]
            put Names.Bob
            -- 24
            put names.bob
            -- 24
            put nAmEs.BOB
            -- 24
            put Names["Bob"]
            -- 24
            put Names["bob"]
            -- <VOID>

            *slaps self on forehead and wanders off muttering...*


            • 3. Re: Variable to property
              Level 7
              Director is always case sensitive when it comes to symbols (the hash
              sign in front of the name). It is supremely annoying when you are not
              expecting it.

              You actually should be using

              put names[#Bob]

              but Director is letting you get away with being sloppy with strings. If
              you want to use a variable, then it should be

              var=#Bob

              or

              var ="Bob"
              put names[symbol(var)]
              • 4. Re: Variable to property
                Level 7
                > Director is always case sensitive when it comes to symbols (the hash sign
                > in front of the name). It is supremely annoying when you are not
                > expecting it.

                Well, for the sake of completeness:

                put Names[#Bob]
                -- 24
                put Names[#bob]
                -- 24
                put Names[symbol("Bob")]
                -- 24
                put Names[symbol("bob")]
                -- 24

                The single solitary case that doesn't work is if you try to use:

                put Names["bob"]
                -- <VOID>

                Which happened to be exactly the situation I was in. Any other method of
                formatting doesn't seem to care if your case is incorrect, but this one
                does. Go figure...


                • 5. Re: Variable to property
                  Level 7
                  > put Names["bob"]
                  > -- <VOID>
                  >
                  > Which happened to be exactly the situation I was in. Any other method of
                  > formatting doesn't seem to care if your case is incorrect, but this one
                  > does. Go figure...

                  It's more important that you understand why this happens. You created a
                  property list using symbols as keys, but then start trying to reference
                  the property names as strings. They are /completely/ different data
                  types. It's stranger that it works in *any* case as opposed to failing
                  in the case you cite.
                  • 6. Re: Variable to property
                    Level 7
                    > It's more important that you understand why this happens. You created a
                    > property list using symbols as keys, but then start trying to reference
                    > the property names as strings. They are /completely/ different data types.
                    > It's stranger that it works in *any* case as opposed to failing in the
                    > case you cite.

                    Well, I guess another lesson to take from this is that just because the
                    program lets you use a "reserved" word as a property name, you still
                    shouldn't do it. I had "Build" as a property in this list. It worked until
                    now, and it's not interfering with the intended meaning of the "build()"
                    command (something to do with 3D, which I haven't touched), but one
                    side-effect is that it automatically changed it from "Build" to "build",
                    which made the function that worked on everything else fail in this case.
                    (It sometimes seems every new version of Director adds a bunch of new
                    keywords, and all the good ones are being used up. I've had problems
                    updating old projects made in previous versions because of stuff like this
                    before.)


                    • 7. Re: Variable to property
                      Level 7
                      The standard way around this naming issue is to apply a standard prefix
                      to the variable name so it is nothing like anything that would be a
                      reserved name. That's why you see folks naming variables like iData or
                      gDataList or vPlatform.

                      In fat I have a fairly comprehensive method of using the prefix to
                      identify the scope of a variable, so the name of the variable is doubly
                      useful. For example, g is for globals, p is for properties, v is for
                      the input that comes into a handler, and i is for variables whose scope
                      is only inside the current handler.

                      That way, when I see a variable named gData, I instantly know that it
                      has global scope and adjusting it may affect other scripts.
                      • 8. Re: Variable to property
                        Level 7
                        > but one
                        > side-effect is that it automatically changed it from "Build" to "build",
                        > which made the function that worked on everything else fail in this case.

                        Again, this suggests you are trying to rely on the case-sensitivity of
                        symbols, or for Director to auto-cast strings to symbols for you; both
                        of which are programmer error, not application error.