3 Replies Latest reply on Dec 19, 2007 7:33 PM by Newsgroup_User

    propery list in linear list

    Level 7
      I have a property list inside a linear list. To simplify, here's an
      example:
      listA = [[#data1: "a", #data2: "1", #data3: "a5"], [#data1: "c", #data2:
      "2",#data3: "d6"]]

      I can find #data1 value in the first property list by:
      put listA[1][#data1]
      -- "a"

      Is there a simple way I can tell the position in the linear list where
      "a" is the value to #data? I guess I can do a loop checking each item in
      the linear list. Was wondering if there was a quicker / simpler way.

      Next, if I want to create a new list, say listB, that has #data1 and
      #data2 from each property list inside listA, how would I do this? In
      other words, the result I want to get would be:
      [[#data1: "a", #data2: "1"], [#data1: "c", #data2: "2"]]

      Now for the property list in listA I may have #data4, #data5, #data6.
      So, would not want a 'delete' system but rather a copy of the specific
      entities into a new list.

      Hope that makes sense. Thanks for any help that can be offered.

        • 1. Re: propery list in linear list
          Level 7
          This may be close to what you are looking for. It is a function which
          takes two parameters. The first parameter is the list you want to run
          against. The second is a list of the properties you are searching for.
          The function returns a list.

          on createSubList fromList, whichProps
          subList = []
          repeat with i = 1 to fromList.count
          subListCandidate = [:]
          repeat with j = 1 to fromList .count
          if whichProps.findPos(fromList
          .getPropAt(j)) then
          subListCandidate[fromList .getPropAt(j)] = fromList[j]
          end if
          end repeat
          if subListCandidate.count > 0 then subList.add(subListCandidate)
          end repeat
          return subList
          end createSubList


          A quick test seems to return the expected results. It possibly could
          require some tweaking.

          put createSubList([[#data1: "a", #data2: "1", #data3: "a5"], [#data1:
          "c", #data2:"2",#data3: "d6"]], [#data1,#data2])
          -- [[#data1: "a", #data2: "1"], [#data1: "c", #data2: "2"]]
          • 2. Re: propery list in linear list
            Level 7
            > Is there a simple way I can tell the position in the linear list where
            > "a" is the value to #data? I guess I can do a loop checking each item in
            > the linear list. Was wondering if there was a quicker / simpler way.
            >
            There may be an easier/more efficient way, but I don't know it. I
            believe that you must loop through all of the items in the linear list.
            Easy enough to do. This function returns the item number of the first
            property list in the linear which contains a property who's value
            matches what you pass to the function. It returns zero if no match is
            found in any property list.

            on findFirstOccurrence fromList, whichValue
            firstOccurrence = 0
            repeat with i = 1 to fromList.count
            if fromList .getPos(whichValue) then
            firstOccurrence = i
            exit repeat
            end if
            end repeat
            return firstOccurrence
            end findFirstOccurrence

            testing in the message window:

            put findFirstOccurrence([[#data1: "a", #data2: "1", #data3: "a5"],
            [#data1: "c", #data2:"2",#data3: "d6"]],"a")
            -- 1
            • 3. Re: propery list in linear list
              Level 7
              Thanks Dave. Your replies have been very helpful.