control scructure repeat and next line
Sarah Reichelt
sarahr at genesearch.com.au
Tue Feb 17 20:55:53 EST 2004
>>> This is the recipe I'm using. the field is "list behavior" and lock
>>> text is unchecked.
>>> the problem is that it stays on the same line it doesn't move on to
>>> the next line and so on.
>>> global myDbid,myLine,thisLine,mySql
>>> on mouseUp
>>> put
>>> revOpenDatabase("PostgreSQL","127.0.0.1","test",postgres,myPassword)i
>>> nto myDbid
>>> repeat for each line myL in field "field 1"
>>> put the selectedText of field "field 1" into mySql
>>> put revdb_execute(myDbid,mySql) after field "field 2"-- to see
>>> what is going on
>>> put the selectedLine of field "field 1" into field "field 3"--
>>> to see what is going on
>>> wait for 1 second
>>> if the mouse is down then exit repeat
>>> end repeat
>>> end mouseUp
>>
>> Each time through the loop, myL contains the text of each line of the
>> field. In the script above, you do not use this variable anywhere in
>> the loop. There is no code to change the selectedText so in effect,
>> you are using the number of lines as a counter, not to get any data.
> Didn't get this last sentence "There is no code to change the
> selectedText so in effect, you are using the number of lines as a
> counter, not to get any data".
OK, I'll try and make myself clearer :-)
There are 2 ways of changing the selectedText in a list field:
1. click on a different line
2. by scripting e.g. set the hilitedLine of fld "MyList" to 3
This is a visual indicator to your user of the currently selected line
and as the programmer, you can use the "selectedText" property to find
out what line your user has clicked on.
In your loop above, you are using the selectedText to decide which SQL
command to execute, but this is inside a loop and there is nothing in
the loop that is going to change which line is selected. The user can't
use method 1, because the program is running a loop and they won't have
control, and the script is not using method 2.
Therefore, you are repeating the loop, using the same data each time
through the loop - whatever line was selected when the loop started.
"repeat for each" does not use the selectedText. It takes the data and
each go through the loop, it fills the assigned loop variable with the
contents of the next chunk, in your case, the next line of the field.
You were using a "repeat for each" loop, but not actually referring to
the data that the loop had got for you, the myL variable. The loop
variable was never used but as the loop was going through every line,
this dictated how many times the loop ran, so the number of lines in
your field was acting as a counter and nothing else.
Hopefully this has clarified how the "repeat for each" works. One
further piece of advice: it is always faster to operate on variables
than fields. If you are only doing a few operations, you won't notice
much difference, but it is a good habit to get into e.g.
put fld "MyList" into myVar
repeat for each line L in myVar
...
> Other then that , it works great . You made in 5 minutes what I didn't
> do in 2 days. ( I said your a genius ) Thanks again.
Thank you.
> By the way the " put
> revOpenDatabase("PostgreSQL","127.0.0.1","test",postgres,myPassword)int
> o myDbid" I put before the repeat because it over loaded the db
> connections.
Yes, you only need to do this once.
Cheers,
Sarah
More information about the use-livecode
mailing list