Trouble with Looping Through Array in LiveCode

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
elenagilbert
Posts: 1
Joined: Wed Aug 21, 2024 4:29 am
Contact:

Trouble with Looping Through Array in LiveCode

Post by elenagilbert » Wed Aug 21, 2024 4:33 am

Hi everyone,
I’m having trouble with a script where I need to loop through an array and display its contents. My goal is to create a list from the array values and set it in a field. However, the field is only displaying the first value of the array, not iterating through the rest.

Code: Select all

-- Define and populate the array
put "Apple" into tArray["fruit1"]
put "Banana" into tArray["fruit2"]
put "Cherry" into tArray["fruit3"]

-- Initialize the field
put "" into fld "fruitList"

-- Loop through the array and display values
repeat for each key tKey in tArray
   put tArray[tKey] & cr after fld "fruitList"
end repeat
When I run the script, only "Apple" is displayed in the field "fruitList". The other values are not being added to the field.
Is there something wrong with the way I’m using the `repeat for each` loop to iterate through the array?
How can I ensure all values in the array are displayed in the field?

stam
Posts: 3060
Joined: Sun Jun 04, 2006 9:39 pm

Re: Trouble with Looping Through Array in LiveCode

Post by stam » Wed Aug 21, 2024 8:07 am

elenagilbert wrote:
Wed Aug 21, 2024 4:33 am
Hi everyone,
I’m having trouble with a script where I need to loop through an array and display its contents. My goal is to create a list from the array values and set it in a field. However, the field is only displaying the first value of the array, not iterating through the rest.

Code: Select all

-- Define and populate the array
put "Apple" into tArray["fruit1"]
put "Banana" into tArray["fruit2"]
put "Cherry" into tArray["fruit3"]

-- Initialize the field
put "" into fld "fruitList"

-- Loop through the array and display values
repeat for each key tKey in tArray
   put tArray[tKey] & cr after fld "fruitList"
end repeat
When I run the script, only "Apple" is displayed in the field "fruitList". The other values are not being added to the field.
Is there something wrong with the way I’m using the `repeat for each` loop to iterate through the array?
How can I ensure all values in the array are displayed in the field?
The way your code has been commented suggests this is AI-generated.
Nevertheless, the code is correct and works as expected: All 3 values are displayed, so can't explain your issue - look at the rest of your code if you are genuinely having this problem.

For such a simple array structure, instead of looping consider using combine instead:

Code: Select all

combine tArray using return
This 1 line replaces your repeat for loop (note however that it consumes the array, in other words, the array is converted into a return-delimited list, so if you want to preserve the original array put it into a temporary variable and combine that instead.

andresdt
Posts: 156
Joined: Fri Aug 16, 2019 7:51 pm
Contact:

Re: Trouble with Looping Through Array in LiveCode

Post by andresdt » Wed Aug 21, 2024 12:01 pm

I don't see anything wrong with your code and it works for me. So just out of curiosity. Is the height of the field high enough to display all the lines?

Code: Select all

set the height of field "fruitList" to the formattedHeight of field "fruitList"
Attachments
Screenshot_11.png
Be kind, we all have our own wars.
https://torocruzand.com/

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

Re: Trouble with Looping Through Array in LiveCode

Post by dunbarx » Wed Aug 21, 2024 2:26 pm

Same here. Code works as advertised.

What andresdt mentioned is cogent, but sort of not possible, since, as written, "apple" appears third in your field. But I bet something like that might be going on here...

Craig

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

Re: Trouble with Looping Through Array in LiveCode

Post by dunbarx » Wed Aug 21, 2024 2:43 pm

Stam's suggestion, using the "combine" command, makes "Apple" appears first in the field, as the OP set it up. But the OP's original loop makes it appear last.

I know that array variables do not have a fixed order internally, even though if one looks at the array variable 'tArray" in the SE after setting a breakpoint at the "put" command in the loop, the order as it appears in the SE is always as the OP created them. Is there any rule that determines how the final result will pan out as compared with the "apparent" order of the array in the SE?

Craig

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

Re: Trouble with Looping Through Array in LiveCode

Post by dunbarx » Wed Aug 21, 2024 2:47 pm

Aha.

Apparently not, since when I added a few more items to the list, the order is scrambled as I thought it always would be. So it is just a little odd that with a short list there seems to be some repeatable, though reversed, fixed order in the output.

Craig

Post Reply