4 Replies Latest reply on Feb 4, 2011 9:12 AM by schneb

    Find Line Number in Text Member

    schneb

      I would like to drop in a line of text after a specific phrase in a text member. To find the right place to drop the text, I must first determine the line number the specific phrase text is located. Now, I can use a REPEAT to walk through each line until it is found, but there must be a more elegant way. The code "Offset" is great for finding the first character number of the specific phrase, but I don't know how to glean the line number from that.

       

      Currently this is how I do it...

       

      repeat with xLineCount = 1 to 100
          if textDoc.line[xLineCount] contains "<specific text phrase>" then
            put RETURN & RETURN & "<new text pasted>" after
      textDoc.line[xLineCount]
            exit repeat
          end if
        end repeat

       

      Can anyone give me a more elegant approach this?

        • 1. Re: Find Line Number in Text Member
          Sean_Wilson Adobe Community Professional

          A more elegant approach might be to use a regular expression, but I'm not sure I understand your requirements well enough to make a sensible recommendation.

           

          You could try the following JavaScript handler and invoke it like:

          tMember.text = jsReplace(tMember.text, "<specific text phrase>", "<specific text phrase>" & RETURN & RETURN & "<new text pasted>")
          

           

          function jsReplace(inStr, strFind, strReplace){
            // error check input
            if(typeof(inStr)      != "string") { return inStr; }
            if(typeof(strFind)    != "string") { return inStr; }
            if(typeof(strReplace) != "string") { return inStr; }
            // search-and-replace
            re = new RegExp(strFind, "g");
            return inStr.replace(re, strReplace);
          }
          

           

          However, this will place the pasted text immediately after the search phrase and not at the end of the line. It is possible to do this with a regular expression, but I'd have to dust off my copy of Friedl's "Mastering Regular Expressions" to craft it for you and I don't have either the time or inclination at present.

          • 2. Re: Find Line Number in Text Member
            schneb Level 1

            Thanks Sean, but not quite what I am looking for unfortunately. Have never explored regular expression code, so I might have to do a little research later.

             

            What I like about lists is that rather than doing a REPEAT to located a specific item, you can us findPos. I wish they had a call "findLine" where findLine("to be or not to be") will give you the number of the line that phrase appears.

             

            If there was a way I could determine what line a particular character count occurs, I could use OFFSET.

            • 3. Re: Find Line Number in Text Member
              Sean_Wilson Adobe Community Professional
              Thanks Sean, but not quite what I am looking for unfortunately.

              I agreed with this before you even said so ;-)

               

              Like I said already - what you ask is possible with a regular expression and would be more "elegant" than your current solution (which in general terms is the only way to achieve what you're trying to do using Lingo alone).

              • 4. Re: Find Line Number in Text Member
                schneb Level 1

                Thanks Sean. I wish I knew my JavaScript coding better.