13 Replies Latest reply on Mar 4, 2008 7:47 AM by Applied CD

    Chunk expressions: Need the first and last char position from member.word[x]?

    Applied CD Level 1
      I need to build a linear list of the string positions of the first and last character of each word in a string. For example if I have the string myPet = “DOG CAT FISH” then myList = [[1,3],[5,7],[9,12]]

      myPet.word[x] will let me access the individual words but I’m not sure how to get the char position of the beginning and end of each. Please help, it’s Friday and my brain has left for the weekend.
        • 1. Re: Chunk expressions: Need the first and last char position   from   member.word[x]?
          Level 7
          on getchars vString
          theList=[]
          current=1
          repeat with iWord=1 to vString.word.count
          thisWord=vString.word[iWord]
          thisChars=[current,current+thisWord.char.count-1]
          theList.append(thisChars)

          current=current+thisWord.char.count+1
          end repeat

          return theList
          end
          • 2. Re: Chunk expressions: Need the first and last char position from member.word[x]?
            Level 7


            theListYoureLookingFor = MakeListFromString ("DOG CAT FISH")

            -- put this handler in a movie script
            on MakeListFromString str
            wordCount = str.word.count
            listResult = []
            repeat with x = 1 to wordCount
            nextWord = str.word [x]
            firstCharOfNextWord = offset (nextWord, str)
            lastCharOfNextWord = firstCharOfNextWord + nextWord.length - 1
            nextList = [firstCharOfNextWord, lastCharOfNextWord]
            listResult.add (nextList)
            end repeat

            return listResult
            end

            "Applied CD" <webforumsuser@macromedia.com> wrote in message
            news:fqa4dq$7eg$1@forums.macromedia.com...
            >I need to build a linear list of the string positions of the first and last
            > character of each word in a string. For example if I have the string myPet
            > =
            > ?DOG CAT FISH? then myList = [[1,3],[5,7],[9,12]]
            >
            > myPet.word[x] will let me access the individual words but I?m not sure how
            > to
            > get the char position of the beginning and end of each. Please help, it?s
            > Friday and my brain has left for the weekend.
            >


            • 3. Re: Chunk expressions: Need the first and last char position   from member.word[x]?
              Level 7
              Don't put any extra spaces between words or this one fails ;)

              Craig

              "Mike Blaustein" <mblaustein@gmail.com> wrote in message
              news:fqa5ef$8h6$1@forums.macromedia.com...
              > on getchars vString
              > theList=[]
              > current=1
              > repeat with iWord=1 to vString.word.count
              > thisWord=vString.word[iWord]
              > thisChars=[current,current+thisWord.char.count-1]
              > theList.append(thisChars)
              >
              > current=current+thisWord.char.count+1
              > end repeat
              >
              > return theList
              > end


              • 4. Re: Chunk expressions: Need the first and last char position from   member.word[x]?
                Level 7
                Hi Craig,

                Your's fails if any word is repeated. Try it with "DOG CAT FISH DOG"

                This one seems to work, although there are probably more efficient ways
                to go about it. A regular expression and the PRegEx xtra would certainly
                be quicker, especially as the string gets longer

                on mGetWordBoundaries aString
                -- basic error check
                if stringP(aString) = 0 then return []
                if length(aString) = 0 then return []

                lWhitespace = [SPACE, TAB, RETURN, numToChar(10)]
                tStart = 1
                tChar = aString.char[tStart]
                repeat while lWhitespace.getPos(tChar)
                tStart = tStart + 1
                tChar = aString.char[tStart]
                end repeat

                lPositions = []
                repeat with w = 1 to aString.word.count
                tEnd = tStart + aString.word[w].char.count - 1
                lPositions.append([tStart, tEnd])
                tStart = tEnd + 1
                tChar = aString.char[tStart]
                repeat while lWhitespace.getPos(tChar)
                tStart = tStart + 1
                tChar = aString.char[tStart]
                end repeat
                end repeat
                return lPositions
                end
                • 5. Re: Chunk expressions: Need the first and last char position from     member.word[x]?
                  Level 7
                  > A regular expression and the PRegEx xtra would certainly
                  > be quicker, especially as the string gets longer

                  Sure enough, start throwing a couple of paragraphs at it and the
                  following nudges ahead in a speed race:

                  on mGetWordBoundaries_regex aString
                  lPositions = []
                  PRegEx_SetSearchString([aString])
                  tEnd = 0
                  nWords = aString.word.count
                  repeat with w = 1 to nWords
                  OK = PRegEx_SetMatchPattern("\w")
                  PRegEx_SetPos(tEnd)
                  found = PRegEx_GetNextMatch()
                  tStart = PRegEx_GetMatchStart() + 1

                  OK = PRegEx_SetMatchPattern("\s|\n|$")
                  PRegEx_SetPos(tStart)
                  found = PRegEx_GetNextMatch()
                  tEnd = PRegEx_GetMatchStart()

                  lPositions.append([tStart, tEnd])
                  end repeat
                  return lPositions
                  end
                  • 6. Re: Chunk expressions: Need the first and last char position from member.word[x]?
                    BG Solutions
                    Thanks for the responses all. I’ll try some speed tests once I’m back in the office on Monday and report back.

                    For the curious my text member is actually the text calendar created by Openspark’s behavior. The “words” are the date numerals (so there is no repetition). Openspark’s behavior automatically creates the list I need if hyperlinks are on … every word (date) is defined as a hyperlink and the list is then simply obtained from myList = member.hyperlinks. My problem is that I need the list even with hyperlinks off, so another option available to me is to create dummy hyperlinks, capture the list, then remove the links from the member (the maximum loop size for any given month would be 31 x 2)
                    • 7. Re: Chunk expressions: Need the first and last char position from member.word[x]?
                      McFazstp Level 1
                      If the string is in a member you can get the range

                      myList = []
                      myList.add( member( "Some text member" ).word[1].range )
                      myList.add( member( "Some text member" ).word[2].range )
                      myList.add( member( "Some text member" ).word[3].range )
                      • 8. Re: Chunk expressions: Need the first and last char position from member.word[x]?
                        Level 7
                        Touche, Sean.

                        "Sean Wilson" <webforumsuser@macromedia.com> wrote in message
                        news:fqa8ap$bga$1@forums.macromedia.com...
                        > Hi Craig,
                        >
                        > Your's fails if any word is repeated. Try it with "DOG CAT FISH DOG"
                        >
                        > This one seems to work, although there are probably more efficient ways to
                        > go about it. A regular expression and the PRegEx xtra would certainly be
                        > quicker, especially as the string gets longer
                        >
                        > on mGetWordBoundaries aString
                        > -- basic error check
                        > if stringP(aString) = 0 then return []
                        > if length(aString) = 0 then return []
                        >
                        > lWhitespace = [SPACE, TAB, RETURN, numToChar(10)]
                        > tStart = 1
                        > tChar = aString.char[tStart]
                        > repeat while lWhitespace.getPos(tChar)
                        > tStart = tStart + 1
                        > tChar = aString.char[tStart]
                        > end repeat
                        >
                        > lPositions = []
                        > repeat with w = 1 to aString.word.count
                        > tEnd = tStart + aString.word[w].char.count - 1
                        > lPositions.append([tStart, tEnd])
                        > tStart = tEnd + 1
                        > tChar = aString.char[tStart]
                        > repeat while lWhitespace.getPos(tChar)
                        > tStart = tStart + 1
                        > tChar = aString.char[tStart]
                        > end repeat
                        > end repeat
                        > return lPositions
                        > end


                        • 9. Re: Chunk expressions: Need the first and last char position from member.word[x]?
                          Level 7
                          range? Is that an MX 2004 property? It's certainly not an MX property. If
                          it is new to 2004 then I'm assuming it returns a list of the first and last
                          char position of the chunk expression requested from within a string. If
                          so, COOL. You got me, I'm just an old timer who learned from John Thompson
                          back in the early 90's at NYU when Lingo had about 1/2 or less of the
                          properties, functions and keywords that it has now.


                          "McFazstp" <webforumsuser@macromedia.com> wrote in message
                          news:fqbe6b$lgl$1@forums.macromedia.com...
                          > If the string is in a member you can get the range
                          >
                          > myList = []
                          > myList.add( member( "Some text member" ).word[1].range )
                          > myList.add( member( "Some text member" ).word[2].range )
                          > myList.add( member( "Some text member" ).word[3].range )
                          >
                          >


                          • 10. Re: Chunk expressions: Need the first and last char position from member.word[x]?
                            Level 7
                            hmmmm...I opened MX 2004 (for the first time probably in 2 years) and
                            couldn't find the range property in the dictionary. I thought maybe it was
                            undocumented but I tried using it in the message window and it errored.


                            "McFazstp" <webforumsuser@macromedia.com> wrote in message
                            news:fqbe6b$lgl$1@forums.macromedia.com...
                            > If the string is in a member you can get the range
                            >
                            > myList = []
                            > myList.add( member( "Some text member" ).word[1].range )
                            > myList.add( member( "Some text member" ).word[2].range )
                            > myList.add( member( "Some text member" ).word[3].range )
                            >
                            >


                            • 11. Re: Chunk expressions: Need the first and last char position from member.word[x]?
                              Level 7
                              Well, I actually paid attention to the detail of your post and damn if it
                              didn't work when referencing a text member. Nice one. Of course, in MM's
                              and Adobe's infinite wisdom, we have just one more property that appears to
                              only work for text members and not field members.

                              Now I can get on with my day.

                              Craig

                              "McFazstp" <webforumsuser@macromedia.com> wrote in message
                              news:fqbe6b$lgl$1@forums.macromedia.com...
                              > If the string is in a member you can get the range
                              >
                              > myList = []
                              > myList.add( member( "Some text member" ).word[1].range )
                              > myList.add( member( "Some text member" ).word[2].range )
                              > myList.add( member( "Some text member" ).word[3].range )
                              >
                              >


                              • 12. Re: Chunk expressions: Need the first and last char position from member.word[x]?
                                Level 7
                                So, if you have a string that's not in a text member you can create a text
                                member on the fly, put your string in it, parse your data and get back the
                                list you need, then erase the cast member. Of course if you do this a lot
                                within a Director session, I've heard you can run into memory leaks although
                                this might tend to happen more with larger bitmaps.

                                Craig

                                "Applied CD" <webforumsuser@macromedia.com> wrote in message
                                news:fqa4dq$7eg$1@forums.macromedia.com...
                                >I need to build a linear list of the string positions of the first and last
                                > character of each word in a string. For example if I have the string myPet
                                > =
                                > ?DOG CAT FISH? then myList = [[1,3],[5,7],[9,12]]
                                >
                                > myPet.word[x] will let me access the individual words but I?m not sure how
                                > to
                                > get the char position of the beginning and end of each. Please help, it?s
                                > Friday and my brain has left for the weekend.
                                >


                                • 13. Re: Chunk expressions: Need the first and last char position from member.word[x]?
                                  Applied CD Level 1
                                  Thanks for everyone’s help. member.word[x].range turned out to be exactly what I was looking for but I appreciate everyone’s input.