Think of something that you wish Authorware could do but it doesn't?  Let the our good friends at Macromedia know via the wishlist.

Please let us know if you find any of the materials on this site inappropriate or offensive. Please include the url and why the material should be reviewed.

Comments and questions about the site are also welcome. Please no Authorware questions, use the AWARE list.

Back

B8001 - How do I find out on which line number a string of text is found?

by - Joseph Ganci


Hi. I am trying to retrieve a line number of a text string within a list of text. All I can find in the book is a way to retrieve text when you KNOW the line number. Can I say something like: Here is some text, find it in a list of text and return the line number? Thanks.  

Thanks to Kevin Warner for this clever solution:  

    LineNo:=LineCount(SubStr(EntireText,1,Find(TargetString,EntireText))) 

Remember that functions are executed from the inside out. Therefore, the first thing that will happen in the above line is that the Find function will search for the target string (contained in the user variable TargetString) within the text (contained in the user variable EntireText). The result of this will be the exact position number in the string at which the target string is found. This number is then passed as the third argument to the SubStr function, which will make a copy of the text from the first character to the character whose position has just been defined by the Find function. The copied text is then passed to the LineCount function which returns the number of lines in the copied text. Since the target position is on the last line of the copied text, this is equivalent to the line number in the original text in which the text was found. 

Let's look at an example. Take, for instance, the following paragraph: 

    Authorware is by far the best authoring system not only in the world, but in the universe. No authoring system can compare to the features found in this most excellent application. 
We can place it in the variable EntireText by any number of means, including reading it in from an external file using the ReadExtFile function, or by assigning it in a Calculation icon, like so: 
    EntireText := "Authorware is by far the best authoring system" ^ Return 
    EntireText := EntireText ^ "not only in the world, but in the universe. No" ^ Return 
    EntireText := EntireText ^ "authoring system can compare to the features found" ^ Return EntireText := EntireText ^ "in this most excellent application." ^ Return 
Know that the "\r" is an Authorware shortcut for the Return character. Note that though the text assignment below is separated here for readability, in reality it appears on one line in an Authorware calculation. Note that Authorware does limit the number of characters you can place on one line in Calculation icon, so you would have to provide a break as above at certain points in long passages. 

    EntireText := "Authorware is by far the best authoring system\rnot only in the world, but in the universe. No\rauthoring system can compare to the features found\rin this most excellent application\r." 

In the above text, if we were to search for the word "features" using Kevin Warner's method: 

    LineNo:=LineCount(SubStr(EntireText,1,Find(TargetString,EntireText))) 

the Find function will return 131, because it is found in the 131st position of the string (notice the Return characters count as well). The substring that is returned by the SubStr function returns the following: 

    Authorware is by far the best authoring system not only in the world, but in the universe.  No authoring system can compare to the features found in this most excellent applicaiton. 

The LineCount function then returns 3 which indicates that there are three lines in the resulting string, which is the same as saying that the target was found on the third line of the original string. 

If the string is not found, the Find function returns 0, which means the SubStr function returns an empty string, which means the LineCount function returns 0 as well. 

One small drawback to the above method is that the target string is assumed unique in the string being searched. What if you wanted to find every line number that contained the target string? You could modify the above method, where you look on the remainder of the string each time you find the target for the next search, but that can get a little unwieldy. A more direct approach uses the Repeat construct. First let's look at how we would copy the functionality of the previous method: 

    LineNum := 0 
    TargetString := "features" 
    count := LineCount(EntireText) 
    repeat with i := 1 to count line := GetLine(EntireText, i) 

      if Find(TargetString, EntireText) then LineNum := i exit repeat end if 
    end repeat 

This too will find only one target in the string. It will loop through the contents of the EntireText variable one line at a time using the GetLine function. Once it finds the target, it sets the LineNum variable to the first line on which the target is found, then it exits the loop. 

If we wish to make a list of all line numbers that contain the target string, we would amend the above as follows: 

    LineNum := "" 
    TargetString := "features" 
    count := LineCount(EntireText) 
    repeat with i := 1 to count 

      line := GetLine(EntireText, i) 
      if Find(TargetString, EntireText) then LineNum := LineNum ^ "," ^ i 
    end repeat 

This creates a text list in the variable LineNum, each line number divided by a comma. 

There are 0 reviews
Add your review
Back