Page 1 of 1

Trouble with Looping Through Array in LiveCode

Posted: Wed Aug 21, 2024 4:33 am
by elenagilbert
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?

Re: Trouble with Looping Through Array in LiveCode

Posted: Wed Aug 21, 2024 8:07 am
by stam
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.

Re: Trouble with Looping Through Array in LiveCode

Posted: Wed Aug 21, 2024 12:01 pm
by andresdt
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"

Re: Trouble with Looping Through Array in LiveCode

Posted: Wed Aug 21, 2024 2:26 pm
by dunbarx
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

Re: Trouble with Looping Through Array in LiveCode

Posted: Wed Aug 21, 2024 2:43 pm
by dunbarx
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

Re: Trouble with Looping Through Array in LiveCode

Posted: Wed Aug 21, 2024 2:47 pm
by dunbarx
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