Page 1 of 1

Finding in field anomaly?

Posted: Tue Nov 04, 2014 4:22 pm
by dunbarx
This was an issue in another thread on the beginner's forum.

Given a field with text on several lines, and for the purpose assume that all entries start with the same char, say "a". In the field script:

Code: Select all

on keyDown var
find var in me
end keyDown
With that field in focus, pressing "a" on the keyboard will find each instance of the text. But when the last line is found, a new key press does nothing. Yet another key press will find the first line again, and so on. The "result", after that last "find" is, not surprisingly ,"Not Found". I made a kluge to fix this:

Code: Select all

on keydown var
   find var in me
   if the result <> "" then
      select char 0 of me
      find var in me
      end if
end keydown
Works fine. My question is this, what is happening? When finding from another source, say the msg box, the "find" cycles what I would consider "normally", that is, round and around without a break, as many times as you care to press that key. I am used to the "find" command searching among cards without issue, the internal index continuing to "find" the next instance, and that index knows when the last card has been scoured, and dutifully starts over. There must to be a similar index watching the field, but it does not cycle as well at all.

Craig Newman

Re: Finding in field anomaly?

Posted: Wed Nov 05, 2014 8:35 pm
by jacque
The behavior was implemented intentionally in the first release of MetaCard 1.0 in order to provide an easy solution to a common problem in HyperCard. Consider the case where you want to loop through all the cards and find all instances of a string:

Code: Select all

repeat 
 find "myString"
 if the result is not empty then exit repeat
 put the foundchunk & cr after tList
end repeat

In HC this will loop forever except for instances where there is no match anywhere in the stack. To solve this, HC users had to store the first foundchunk and then compare each instance of subsequent finds to see if it was the same one, which would tell the script that all instances had been found and the loop should exit. To fix that, the MC/LC engine tracks the progress for you. If the result is empty, either there were no matches at all or the entire loop has completed already. Issuing the find commad again after that will start the process over.

Specifically executing "find empty" resets the find flag and begins a new search. I haven't looked at the scripts, but I suspect the message box is resetting the tracking flag so that you get the results you'd expect.

Re: Finding in field anomaly?

Posted: Thu Nov 06, 2014 2:48 pm
by dunbarx
Jacque.

(HC) I know this. I still run stacks that include it. I am afraid it dates us both.

(LC) Well and good. So what I called a kluge is just proper management, in the same vein.

Craig