Using a variable to refer to a field

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
trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Using a variable to refer to a field

Post by trags3 » Fri Feb 26, 2021 3:36 pm

I have a series of fields named pHlp1,pHlp2 ...
Trying to find the first empty field (if any) and store that in another field... iFA (First Available)

Code: Select all

repeat with N=1 to 15
     put "pHlp" & N into emptyFLD
     if fld emptyFLD of cd"Page 2" of stack"MJADMIN" is empty then
         put N into field "iFA"
         exit repeat
     end if
 end repeat
I've tried quite a few different things on the third line to no avail.
I know this is basic but can't seem to find the answer.

Tom

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using a variable to refer to a field

Post by Klaus » Fri Feb 26, 2021 4:10 pm

Hi Tom,

syntax seems correct!
What does not work?


Best

Klaus

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Using a variable to refer to a field

Post by bogs » Fri Feb 26, 2021 4:16 pm

I dunno, Tom, while I wouldn't have gone this way, it works here with this setup -

Image

...so at least one question comes to mind.
When you named the fields, for instance, is there a space between "pHlp" and the number? That would sure throw off the code.
Image

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Using a variable to refer to a field

Post by trags3 » Fri Feb 26, 2021 4:59 pm

Sorry Bogs :oops:
Where I have the problem is in the code that uses the result. I didn't have any coffee when I wrote the post.What I posted works swimmingly!

I'll work that for a few minutes and get back.
Tom

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Using a variable to refer to a field

Post by trags3 » Fri Feb 26, 2021 5:27 pm

Hi Klaus and Bogs!
Here is where I have the problem.

Code: Select all

 repeat with N=1 to 15
      put "pHlp" & N into emptyFLD
      if fld emptyFLD of cd"Page 2" of stack"MJADMIN" is empty then
         put "pHlp" & N into field "ifa"
         exit repeat
      end if
   end repeat
   
   if the hilite of btn "ic1" then
      put fld"ifa" into tfirst
      put the label of btn"ic1" into fld"&tfirst" of cd"page 2" of stack "MJADMIN"
   end if
Thanks

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using a variable to refer to a field

Post by Klaus » Fri Feb 26, 2021 5:43 pm

The only problem I see is:

Code: Select all

...
## put the label of btn "ic1" into fld "& tfirst" of cd"page 2" of stack "MJADMIN"
## results in -> field "&tFirst"
## Since this is a STRING!
## You should have seen the error -> no such object
put the label of btn "ic1" into fld tfirst of cd "page 2" of stack "MJADMIN"
...

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Using a variable to refer to a field

Post by trags3 » Fri Feb 26, 2021 5:53 pm

Klaus,
Thank you so much!
Tom

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using a variable to refer to a field

Post by Klaus » Fri Feb 26, 2021 5:56 pm

Please use some more SPACES to make you code better readable.

And you can directly use concatenated object names if you use parens:

Code: Select all

...
repeat with N = 1 to 15
      if fld ("pHlp" & N) of cd "Page 2" of stack "MJADMIN" is empty then
...

liveme
Posts: 240
Joined: Thu Aug 27, 2015 5:22 pm

Re: Using a variable to refer to a field

Post by liveme » Fri Feb 26, 2021 10:06 pm

Trying to find the first empty field (if any) and store that in another field.
That problem, seems a bit funny depending how you read it:... :) ...and store empty field content (?)...into something else ? :lol:
(just kidding trags3 ) :wink:

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Using a variable to refer to a field

Post by trags3 » Sat Feb 27, 2021 3:13 am

Hi Liveme
Here is an explanation of what I'm doing in this part of the App.
I have a set of fields that hold a list of selections by the user. I this case the number is 15.
The user may delete a field for any of a number of reasons.
I want to keep the list (is the word congruous?) with no empty lines in the middle of the list for cosmetic reasons. Also I need to find an empty field if another item is added to the list.
That is the whole purpose of finding the first empty field in a list and putting the number or other reference to that field in another field. ie fld"iFA" (First Available.)
I appreciate all the help I've been given off and on for the past 6 years :D

Tom

Post Reply