Ending Focused $Order Loops
Now let's focus the end of the loop. Again, assume the variable sub holds the non-empty substring. Then the following statement: (quit:$extract(ln,1,$length(sub))'=sub) means: “Quit if the beginning of ln doesn't match sub”. It also handles the case where $Order reaches the end of the names, where ln = "". That's because, if ln is the empty string, $Extract of ln is the empty string, and since sub isn't empty, the Quit will happen and the loop ends. Some examples are below.
SAMPLES>set sub = "J"
SAMPLES>set ln = "Jones"
SAMPLES>if $extract(ln, 1, $length(sub)) '= sub { write "No Match" }
SAMPLES>set sub = "J"
SAMPLES>set ln = "Swoboda"
SAMPLES>if $extract(ln, 1, $length(sub)) '= sub { write "No Match" }
No Match
SAMPLES>set sub = "J"
SAMPLES>set ln = ""
SAMPLES>if $extract(ln, 1, $length(sub)) '= sub { write "No Match" }
No Match
SAMPLES>do ^loopend
Search for: J
Jones
SAMPLES>
The finished focused loop (loopend.mac) code:
loopend ; loop through last names
read "Search for: ",sub
set ln = $order( ^PersonI("Name", sub), -1)
for {
set ln = $order( ^PersonI("Name", ln) )
quit:( $extract(ln, 1, $length(sub)) '= sub )
write !, ln
}