Referring to field by value stored in list

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Referring to field by value stored in list

Post by Simon Knight » Wed Nov 19, 2014 7:48 pm

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
best wishes
Skids

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Referring to field by value stored in list

Post by dunbarx » Wed Nov 19, 2014 8:03 pm

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

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Referring to field by value stored in list

Post by Simon Knight » Wed Nov 19, 2014 10:54 pm

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
best wishes
Skids

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Referring to field by value stored in list

Post by dunbarx » Thu Nov 20, 2014 12:32 am

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

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Referring to field by value stored in list

Post by Simon Knight » Thu Nov 20, 2014 11:30 am

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
best wishes
Skids

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Referring to field by value stored in list

Post by Simon Knight » Thu Nov 20, 2014 11:40 am

Hi,

I sit corrected! The engine does resolve

Code: Select all

Field ( field "MyFieldName")
correctly. It does the speed test in 271 ms so is slightly faster than extracting the characters from the text name.

Thanks

S
best wishes
Skids

Post Reply