referring to fields in a repeat loop

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
nigelBUSrqT
Posts: 14
Joined: Fri May 11, 2012 6:24 am

referring to fields in a repeat loop

Post by nigelBUSrqT » Wed Jun 06, 2012 10:23 am

I have been reluctant to submit this most basic of questions but having spent the last few hours scouring all sources of information (and there seem to be quite a few), I have yet to identify a solution. I have referred to the User Guide, Dictionary, lessons, tutorials, rev online, conferences .... and am clearly missing something obvious.

I am trying to run a similar collection of operations on items of an array and have each one reported into its own field in a UI and have each updated a continual basis through a "constrainLinearCallBack" function.

The requirement seems simple: put array[1] into field "fieldPercentage1" but given that the field term is described in quotes, it is not possible to specify it in the same terms as the array.

I saw one approach that looked something like this: put array[mykey] into fld id mykey
but I have not managed to:

- link a field with the key (I can get it to drop a number into fld id 1034 for example but this has no structure when there are multiple numbers of fields)
- declare an array with two levels of elements

Is there anywhere a straightforward description of doing either or both of these things?

cheers, Nigel

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: referring to fields in a repeat loop

Post by shaosean » Wed Jun 06, 2012 10:44 am

You can do something like this

Code: Select all

local tKey
local tFieldName
repeat for each key tKey in array
  put "fieldPercentage" & tKey into tFieldName
  put array[tKey] into field tFieldName
end repeat

nigelBUSrqT
Posts: 14
Joined: Fri May 11, 2012 6:24 am

Re: referring to fields in a repeat loop

Post by nigelBUSrqT » Wed Jun 06, 2012 8:07 pm

Aha - wonderful - thank you Shaosean! I am still not clear on the simple task of initially defining the keys of the array - will try to work that through in conjunction with the Dictionary and what you have suggested and let you know how I go

very best

Nigel

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: referring to fields in a repeat loop

Post by mwieder » Wed Jun 06, 2012 8:20 pm

...and for the second part
- declare an array with two levels of elements

Code: Select all

put "1600 Pennsylvania Avenue" into employeeArray[employeeID]["address"]
put game[1]["winner"]["emailAddress"] into field "fldRecipient"

put oneArray into anotherArray[nestLevel]

nigelBUSrqT
Posts: 14
Joined: Fri May 11, 2012 6:24 am

Re: referring to fields in a repeat loop

Post by nigelBUSrqT » Wed Jun 06, 2012 8:51 pm

thankyou - I can follow the logic of each of the items that you have listed up here - I am afraid my main challenge is even more basic:

let's say that there are to be seven elements in the array, ticon1 .... ticon7

how do I declare the array in such a way that a repeat loop responds to the presence of all seven elements?'

I can see that using the "repeat with i=1 to 7" approach does this but it limits me to using a number

and I can see that "repeat for each key tKey in array" works once I have defined the array as having seven elements and what the keys of these elements are - but how do I make explicit that there are 7 elements (and runs through the loop) to be dealt with and declare their names?

:oops: (sorry - am keen to have a more productive day than yesterday and am still failing to find this in the user guide)

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: referring to fields in a repeat loop

Post by mwieder » Wed Jun 06, 2012 9:14 pm

and am still failing to find this in the user guide
information on using arrays is sadly lacking from the User Guide. You'll have better luck searching for "array" at http://lessons.runrev.com

Arrays in LiveCode are typically called "hashes" in other programming environments, so some googling for that might also turn up useful bits.

But to get to the point... there's no need to declare the size of an array. If you want seven elements, then set your loop counter to seven.

Code: Select all

put 7 into tHowManyElements

repeat with x=1 to tHowManyElements
  put "Monty Python" & x into tElement
  put "something" into demoArray[tElement]
end repeat
If you need the element keys to have explicit names then you'll need to do something like

Code: Select all

put 7 into tHowManyElements

-- set up array with key names
put "red,orange,yellow,green,blue,indigo,violet" into tKeys

set the itemDelimiter to comma
repeat with x=1 to tHowManyElements
  put item x of tKeys into tElement
  put "something" into demoArray[tElement]
end repeat

nigelBUSrqT
Posts: 14
Joined: Fri May 11, 2012 6:24 am

Re: referring to fields in a repeat loop

Post by nigelBUSrqT » Wed Jun 06, 2012 10:30 pm

ah - Monty Python - now I understand!

thankyou so much, M - this will make a huge difference to my next 24 hours (if yesterday's is anything to go by)

:-)

N

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: referring to fields in a repeat loop

Post by shaosean » Thu Jun 07, 2012 1:39 am

You can compact the code just a little more and just declare your key inline without having to use another variable.. Of course, if your key creations are more complex, you may want to stick to using a separate line for it..

Code: Select all

repeat with x=1 to tHowManyElements
    put "something" into demoArray["Monty Python" & x]
  end repeat

Code: Select all

set the itemDelimiter to comma
  repeat with x=1 to tHowManyElements
    put "something" into demoArray[item x of tKeys]
  end repeat

nigelBUSrqT
Posts: 14
Joined: Fri May 11, 2012 6:24 am

Re: referring to fields in a repeat loop

Post by nigelBUSrqT » Thu Jun 07, 2012 10:47 pm

Hi Both - a quick update:

the invaluable guidance you provided led yesterday to the compression of my stack from several hundred lines down to a hundred or so - a great step forwards. At this point, I have not yet ironed out all of the bugs - I expect that will be the focus of most of today - once I have it all working again, I'll drop a note back in here and get onto replicating the approach to several clones at a level down in the functional hierarchy. Whilst there's still too much randomly banging around into things, the debugging I was getting into last night was starting to feel like an orderly pursuit of recognized goals - my journey from scientist / creative to scientist / creative / programmer is underway

many thanks for your help

:-)

N

nigelBUSrqT
Posts: 14
Joined: Fri May 11, 2012 6:24 am

further update

Post by nigelBUSrqT » Tue Jun 19, 2012 9:34 am

Many thanks for the earlier help - the stack now comprises a number of cards, each servicing a page in the app - tracking well.

Ahead, there is a significant challenge in terms of generating a page that draws input from all of the existing pages and integrates this with additional input scores to produce an overall integrated number. This will require:

- stashing of data into a couple of 7 x 7 arrays and performing some calculations on these to produce the final magic number
- careful opening and closing of sections of input screen to avoid gross screen clutter

I am not sure how long it will take me to plough through this (or whether I will see sense and focus on my day job and sub-contract this task) but am hoping at the moment that this rather addictive activity can yield to my persistence a little more quickly this time around. Either way, it is great to have experienced the first class support of this forum in addressing the challenges encountered so far

thankyou

Nigel

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”