PDA

View Full Version : How to count the occurences of all text found in a richtext object?


hliobasilema
04-02-2009, 10:35 PM
The following is my attempt that did not produce correct results.
Can somebody help me on this?
Thanks...




search_result = RichText.FindText("RichText1", "findthis", 1, -1, false, false);

-- Create a table containing all possible matches.
all_matches = {};

min = search_result.Start+1; -- The number to start at
max = RichText.GetTextLength("RichText1"); -- The number to stop at
step = 1; -- The number to count by

--try to find all matches
for count_var = min, max, step do --beginning of for loop
search_result=nil; --just to avoid confussion with previous references, just in case anyway
search_result = RichText.FindText("RichText1", "findthis", count_var, -1, false, false);


--check to see if it is a match
if (search_result) then
all_matches[(Table.Count(all_matches)+1)]=search_result;
end


end ---end of for loop

------
------
------
------ IF ALL CORRECT UNTILL NOW, THE FOLLOWING SHOULD DISPLAY CORRECT RESULTS

no_of_matches_found = Table.Count(all_matches);

---- BUT THE NUMBER DISPLAYED IS NOT AS EXPECTED! IT IS A REALLY HUGE ONE INSTEAD!

hliobasilema
04-03-2009, 09:01 AM
I made the following amendments: (just added a check for duplicate values)

--check to see if it is a match and check for duplication of values
if ((search_result~=nil)and (search_result~=previous_search_result)) then
all_matches[(Table.Count(all_matches)+1)]=search_result;
previous_search_result=search_result;
end


But still i get wrong results.

I believe that it must be something about how the "Richtext.FindText" built in function has been implemented.
I am really stuck with this one, i have no more ideas on top off my head.:huh

hliobasilema
04-03-2009, 10:22 AM
search_result~=previous_search_result


SOLVED

I just changed the above line with:

search_result.Start~=search_result.End



:yes now works fine. Thank you everybody who have read my case

holtgrewe
04-03-2009, 10:44 AM
You solved it before I hade a chance to reply - I dug this out of some old code I had and adapted it to you...

all_matches = {};
search_variable="findthis"
bump=1
beg=1
x=1
while x==1 do
search_result = RichText.FindText("RichText1", search_variable, beg, -1, false, false);
if (search_result) then
beg=search_result.End +1
all_matches[bump]=search_result
bump=bump+1
else
x=0;
end
end
Dialog.Message("matches:", ""..Table.Count(all_matches))

hliobasilema
04-03-2009, 11:19 AM
Never it is too late! Here you give me a chance to consider your code that is
more elegant.
So thank you!

I like a lot the "beg=search_result.End +1" part!!!

But isn't this code going to break the loop as soon
as we don't have a match???

Shouldn't the while loop be within another loop?

holtgrewe
04-03-2009, 11:50 AM
Try it. I haven't tested it completely, but it should check the entire RT for all occurances...and yeah, the code will break when you don't have a match...I thought that was the premise. It should check for each occurance and store them in the table (for additional reference if desired).

hliobasilema
04-03-2009, 12:17 PM
Try it. I haven't tested it completely, but it should check the entire RT for all occurances...and yeah, the code will break when you don't have a match...I thought that was the premise. It should check for each occurance and store them in the table (for additional reference if desired).

What happens is that this code will only capture the first match. As soon as it will start looking for the second will stop.

But it should not stop and continue looking untill the end of document trying to find yet another match.

holtgrewe
04-03-2009, 12:26 PM
That's strange...I just tested it and it works fine here.
Sorry it didn't work for you...just scrap my code and use your solution. :yes

holtgrewe
04-03-2009, 12:40 PM
hliobasilema
Here's a quick example of my test. Maybe we're not on the same page as to what you are wanting to do.