Quantcast
Channel: LiveCode Forums
Viewing all articles
Browse latest Browse all 672

Getting Started with LiveCode - Complete Beginners • Re: Trouble with Looping Through Array in LiveCode

$
0
0
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:

-- Define and populate the arrayput "Apple" into tArray["fruit1"]put "Banana" into tArray["fruit2"]put "Cherry" into tArray["fruit3"]-- Initialize the fieldput "" into fld "fruitList"-- Loop through the array and display valuesrepeat 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:

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.

Statistics: Posted by stam — Wed Aug 21, 2024 8:07 am



Viewing all articles
Browse latest Browse all 672

Trending Articles