Page 1 of 1
Referring to field by value stored in list
Posted: Wed Nov 19, 2014 7:48 pm
by Simon Knight
Hi,
I am saving a number of values extracted from fields on a card prefixed by the name of the field. The list looks like this :
Code: Select all
field "TagA",FIN
field "TagB", TRN
field "TagC",BCN
I wish to loop through the list setting the values into the fields. Is it possible to use the first item directly in code to refer to a field control where the first item is 'field "TagA". I have tried the following but it does not work (it writes data back to the list)
Code: Select all
Repeat for each line tline in tPrefsData
put item 2 of tline into Item 1 of tLine
end Repeat
I have tried various combinations of brackets and the value command but with no success.
Any ideas ?
Simon
Re: Referring to field by value stored in list
Posted: Wed Nov 19, 2014 8:03 pm
by dunbarx
Hi.
This is a case for the "do" construction.
Code: Select all
on mouseUp
Repeat for each line tline in tPrefsData
put item 1 of tLine into tFld
put item 2 of tLine into tData
do "put" && tdata && "into" && tFld
end Repeat
end mouseUp
This is a bit wordy, but intentionally so for clarity. The issue is old; LC needs an extra level of evaluation to make sense of your original statement. That thinking was really OK, but just one level removed of what LC can parse and compile correctly. Please play with this. It is an invaluable part of your LC arsenal.
Craig Newman
Re: Referring to field by value stored in list
Posted: Wed Nov 19, 2014 10:54 pm
by Simon Knight
Hi Craig,
Thanks for the code, I guessed that it must be possible as livecode returns the name of a field as field "fieldname".
Simon
Re: Referring to field by value stored in list
Posted: Thu Nov 20, 2014 12:32 am
by dunbarx
Sometimes you can help the process the old fashioned way. Did you try this?
Code: Select all
on mouseUp
put fld 1 into tPrefsData
Repeat for each line tline in tPrefsData
put (item 2 of tline) into fld (Item 1 of tLine)
end repeat
end mouseUp
This gives enough of a push to allow the script to do what you want as well. maybe easier to understand?
Craig
Re: Referring to field by value stored in list
Posted: Thu Nov 20, 2014 11:30 am
by Simon Knight
Hi Craig,
I have not tried your second example but will give it a try. I am not sure that it will work: Item 1 of tLine is set to : Field "Tag1" so unless the engine can resolve fld(Field "Tag1") it is sure to throw an error.
I have written a second handler that strips the text after the word field so it gives Tag1 in the example above. I have compared the two methods of writing the data to the fields calling the routines a 1000 times. Using the Do command took 435ms whereas using the loop that extracts the name took 287ms. Also using the Do command requires protective code against blanks and text that it thinks is a command. So the Do is useful but needs care.
best wishes
Simon
Re: Referring to field by value stored in list
Posted: Thu Nov 20, 2014 11:40 am
by Simon Knight
Hi,
I sit corrected! The engine does resolve
correctly. It does the speed test in 271 ms so is slightly faster than extracting the characters from the text name.
Thanks
S