11 Replies Latest reply on Oct 25, 2006 3:54 PM by Garek007

    Property Lists!

    Garek007 Level 1
      Hi,

      I am trying to use a property list in my Director and because of this, I cannot save or run my movie. Here is an example.

      I have a function that runs on movie start and reads a text file putting the elements into a property list. Later in the move I try to read from this list with x = myList["2280"]

      But because the list isn't created until the movie starts and runs my function getData() the movie won't let me start? It tells me variable used before assigned a value, but if the movie would just start it WOULD have a value!

      What can I do?

      Thanks,
      Stan
        • 1. Re: Property Lists!
          Chunick Level 3
          Have you tested to see if the function is called and the variable containing the property list is properly created?...

          My frist guess would be that you haven't set the variable to be global. If you are not familiar with variable scope then press F1 to bring up the help window and in the search tap type in "scope" (without quotes) and you should see an entry named "Variables" click on that and that should give you a clear explanation.

          The second thing I see is that myList["2280"] means that you are assigning the value of the property "2280" to x. This seems to be a wasteful use of property lists because your property name "2280" doesn't seem to have any significance and if it refers to the position of the entry in the property list then it's redundant. For example:

          a property list:
          pList = ["1": 3334, "2": 12, "3": 87, "4": 9008]
          put pList["4"]
          -- 9008

          a list:
          aList = [3334, 12, 87, 9008]
          put aList[4]
          -- 9008

          There's little to no benefit in that example to use a property list over a plain list. However, if the properties had meaning like this:

          property list:
          pHighScoreList = ["Jack": 3334, "LZR": 12, "Sub Zero": 87, "ACE": 9008]
          put pHighScoreList["ACE"]
          -- 9008

          then we have a good use for property lists over plain lists because I can now get the high score for a particular individual by using that person's name they entered.
          • 2. Re: Property Lists!
            duckets Level 1
            is 'myList' supposed to be a global?

            if so, maybe you forgot to put: "global myList" at the top of your script which is causing the error.

            The error "variable used before assigned a value" can be slightly misleading, because you don't actually have to assign it a value. You just have to declare that it's a property or a global. It then has a value of 'void', which is technically 'unnassigned', but your code should now work,

            hopet this helps,

            - Ben

            • 3. Re: Property Lists!
              the real POTMO Level 1
              there are actually two types of lists.
              Linear lists and property lists.

              the linear list can be accessed by using its index
              myList = ["yeah", "wow", "fresh"]
              put myList[2]
              --wow

              the propertyList can be accessed by using index or key.

              myList = ["propone" : "yeah", "proptwo" : "wow", "helloworld" : "fresh"]
              put = myList[2]
              -- wow
              put = myList["propone"]
              --yeah

              you can also use symbols as keys ("I think its a bit quicker")
              myList = [#propone : "yeah", #proptwo : "wow", #helloworld : "fresh"]
              put myList[#propone]
              --yeah
              or with dot syntax
              put myList.propone
              --yeah

              if you use dot syntax and the key does not exist youll get an error.
              if you use [] syntax youll get void as result with keys not existing.

              myList = [#propone : "yeah", #proptwo : "wow", #helloworld : "fresh"]
              also you can write myList[#somenewprop] = "woha"
              if the key does not exits it will add it and the value.
              put myList
              --[#propone : "yeah", #proptwo : "wow", #helloworld : "fresh", #somenewprop:"woha"]



              remember if you use strings as keys the keys are case sensitive "propOne" <> "propone"

              if you use symbols as keys you cant have a number as first char in the key
              #1223key = BAD
              #key1223 = GOOD
              if you use strings as keys its ok to have numbers as first chars or numbers only
              like "1223key" = OK





              so the soultion to yor problem:
              i think youre accessing the list with x = myList["2280"] instead of x= myList[2280]

              a bit of a overkill help but it might help someone


              • 4. Property Lists!
                Garek007 Level 1
                POTMO, 2280 is actually a string not a reference to a position in the list, so it should still be "2280" no?

                Chunick,
                Maybe I could use a regular list, it would be a hell of a lot easier. Here's what I am trying to do, please tell me what would work best. I have an excel file that I exported to a tab delimited text file. In essence it's a table of values. I need to be able to read those values in and call them up at will. It seems like a property list would make that easier because I can have each line referenced as a category and then call items in that line using the first word of that line, in this case "2280". Am I overcomlicating this?

                By the way, the error I'm getting is for prods. I do not receive one for myFile. prods is declared as global as global prods.
                But when I try to declare prods as global prods[:] I get more errors.

                Last night I tried something new and took all the function brackets out placing the script in an on startMovie handler. Still get errors.
                • 5. Re: Property Lists!
                  Garek007 Level 1
                  So this is what my startMovie script looks like, if it helps.

                  global prods

                  on startMovie

                  repeat with channel = 28 to 41
                  sprite(channel).cursor = 280
                  end repeat



                  MyData = the moviePath & "test.txt"
                  SpecFile = PRegEx_ReadEntireFile(MyData)
                  if not listP(SpecFile) or not stringP(SpecFile[1]) then
                  alert("Read failure of " & MyData)
                  halt
                  end if
                  PRegEx_Replace(SpecFile, "(?:\x0D\x0A)|[\x0D\x0A]", "g", "\n")



                  s=SpecFile[1]
                  nProds=s.lines.count
                  -- check nProds>0
                  prods=[:]
                  the itemDelimiter=TAB
                  repeat with i=1 to nProds
                  t=line i of s
                  if t.items.count<1 then
                  -- incomplete data?
                  alert("Incomplete data at line " & i)
                  next repeat
                  end if
                  cat=item 1 of t
                  prod=item 2 of t
                  address=item 3 of t
                  area=item 4 of t
                  use=item 5 of t
                  numFloors=item 6 of t
                  bldgHeight=item 7 of t
                  constYear=item 8 of t
                  exterior=item 9 of t
                  condition=item 10 of t



                  -- if this category doesn't exist, add it
                  if voidP(getAprop(prods, cat)) then
                  addProp prods, cat, [:]
                  end if

                  -- if this product in this category doesn't exist, add it
                  if voidP(getAprop(prods[cat], prod)) then
                  addProp prods[cat], prod, []
                  end if

                  info=[address,area,use,numFloors,bldgHeight,constYear,exterior,condition]
                  add prods[cat][prod], info


                  end repeat



                  on showCategories
                  nCats=count(prods)
                  repeat with i=1 to nCats
                  put getPropAt(prods, i)
                  end repeat
                  end showCategories

                  on showProds(category)
                  nProds=count(prods[category])
                  repeat with i=1 to nProds
                  put getPropAt(prods[category], i)
                  end repeat
                  end showProds

                  end

                  • 6. Re: Property Lists!
                    Garek007 Level 1
                    Please?
                    • 7. Re: Property Lists!
                      Garek007 Level 1
                      Chunick?
                      • 8. Re: Property Lists!
                        duckets Level 1

                        First thing, it looks like you're trying to define handlers inside your startmovie handler. You should 'end' your startmovie handler before trying to define the 'showCategories' and 'showProds' handlers.

                        Second, maybe you could upload the text file somewhere so we can see? Parsing a tab delimited text file should be pretty simple, but it's difficult to advise what the problem is without seeing the source file.

                        - Ben
                        • 9. Re: Property Lists!
                          Garek007 Level 1
                          Duckets,

                          As you suggested I posted the files here:

                          ftp://ftp.dahlingroup.com/pub/Stan/

                          The director file is there as well as the text file.
                          • 10. Re: Property Lists!
                            duckets Level 1

                            How about something like this (code attached below).

                            I read the text file by simply creating a new temporary text member, and setting its filename to the text file path. I then grab the text into a variable, and delete the temp text member.

                            I've used my own general purpose 'split' handler which splits strings into arrays. Here's the split handler (put it in a moviescript):

                            http://www.robotduck.com/content/articles/director/tools/string/split.txt

                            First I use it to split the text by 'return', to get a list of lines, then I loop through each line and split that by 'tab', to get a list containing each item of data on that line.

                            Finally I use your code to drop each data list into the correct 'cat' and 'prod' sublist of the main 'prods' list.

                            Hope this helps!

                            - Ben


                            • 11. Re: Property Lists!
                              Garek007 Level 1
                              That is genius, thank you so much!