5 Replies Latest reply on May 4, 2009 2:09 PM by fazstp

    Best lingo writing practices (if statement)

    MAC800 Level 1

      Hello

       

      What is the best script if you have a case where you have multi 'if statements'

       

      Here is what I need to do. I have a serial device attached to my PC that can read if contacts have closure. The device has 32 closures. I have all communication (status of the contacts) being reported back as text (100-132  [each contact has its own number starting at 100])entered into a text field. Obviously more than one closure could be reported back as closed at once (so a 'else if' statement would not work)

       

      So the question is as far as best writing practices for lingo .... how does this look?

       

       

      on enterFrame

      if the text of member "contacts" contains "100" then set the text of member "contact1" to "Open"

      if the text of member "contacts" contains "101" then set the text of member "contact2" to "Open"

      if the text of member "contacts" contains "102" then set the text of member "contact3" to "Open"

      if the text of member "contacts" contains "103" then set the text of member "contact4" to "Open"

      if the text of member "contacts" contains "104" then set the text of member "contact5" to "Open"

      if the text of member "contacts" contains "105" then set the text of member "contact6" to "Open"

       

      end if

      end

        • 1. Re: Best lingo writing practices (if statement)
          Sean_Wilson Adobe Community Professional

          The way your code is written suggests no relationship between the clauses. For example, is it possible that  member "contacts" could contain both "100" AND "101"? In this case would you want to change both member "contact1" and member "contact2"? If not, then your code might have been written as an 'if' statement and a series of 'else if's.

          I'll suggest you drop the ancient verbose syntax (you don't say which version of Director you are using) and assume the second - that you only want to change one at a time. Here is your original re-written a little more concisely:

          on enterFrame
            tText = member("contacts").text
            if tText contains "100" then member("contact1").text = "Open"
            if tText contains "101" then member("contact2").text = "Open"
            if tText contains "102" then member("contact3").text = "Open"
            if tText contains "103" then member("contact4").text = "Open"
            if tText contains "104" then member("contact5").text = "Open"
            if tText contains "105" then member("contact6").text = "Open"
          end
          

          and here it is with some relationship between the clauses (so that only one of them will execute - the first that is found to be TRUE):

          on enterFrame
            tText = member("contacts").text
            if      tText contains "100" then member("contact1").text = "Open"
            else if tText contains "101" then member("contact2").text = "Open"
            else if tText contains "102" then member("contact3").text = "Open"
            else if tText contains "103" then member("contact4").text = "Open"
            else if tText contains "104" then member("contact5").text = "Open"
            else if tText contains "105" then member("contact6").text = "Open"
          end
          

          You could write the whole thing in a case statement, but I don't think there's much to be gained by doing so

          on enterFrame
            tText = member("contacts").text
            case TRUE of
              (tText contains "100"): member("contact1").text = "Open"
              (tText contains "101"): member("contact2").text = "Open"
              (tText contains "102"): member("contact3").text = "Open"
              (tText contains "103"): member("contact4").text = "Open"
              (tText contains "104"): member("contact5").text = "Open"
              (tText contains "105"): member("contact6").text = "Open"
            end case
          end
          
          • 2. Re: Best lingo writing practices (if statement)
            Production Monkey Level 3

            I would not run this code in an "EnterFrame" handler. At some point in your code you update the information in member("contacts"). I would not bother with that step and instead update the other fields at the point that the data is generated.

             

            I would rewrite your current code this way.

             

            on enterFrame

               tText = member("contacts").text

               repeat with Cnt = 1 to 32

                  if tText contains String(99 + Cnt) then member("contact"&Cnt).text = "Open"

               end repeat

            end

             

             

            • 3. Re: Best lingo writing practices (if statement)
              MAC800 Level 1

              Sorry .....  Director 11.0 && Windows XP

               

              And yes 100 & 101 could both be TRUE

               

              So.......  I should use the below ?

               

              on enterFrame
                tText = member("contacts").text
                if tText contains "100" then member("contact1").text = "Open"
                if tText contains "101" then member("contact2").text = "Open"
                if tText contains "102" then member("contact3").text = "Open"
                if tText contains "103" then member("contact4").text = "Open"
                if tText contains "104" then member("contact5").text = "Open"
                if tText contains "105" then member("contact6").text = "Open"
              end

               

               

              Thanks for the help

              • 4. Re: Best lingo writing practices (if statement)
                Sean_Wilson Adobe Community Professional
                And yes 100 & 101 could both be TRUE

                 

                So.......  I should use the below ?

                Yes, but I agree that #enterFrame is perhaps not the best event to respond to given how frequently it occurs. You didn't explain where this script is and how often it needs to run, so it's difficult to make any recommendation.

                • 5. Re: Best lingo writing practices (if statement)
                  fazstp Level 2

                  Or as a variation on Production Monkey's routine

                   

                  on enterFrame

                  contacts = member( "contacts" ).text

                   

                  repeat with contact = 1 to 32

                  if ( contacts contains string( contact + 99 ) ) then

                  member( "contact" & contact ).text = "Open"

                  else

                  member( "contact" & contact ).text = "Closed"

                  end if

                  end repeat

                  end