How to resolve the algorithm Associative array/Iteration step by step in the LiveCode programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Associative array/Iteration step by step in the LiveCode programming language

Table of Contents

Problem Statement

Also show how to iterate just over the keys, or the values, if there is a separate way to do that in your language.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Associative array/Iteration step by step in the LiveCode programming language

Source code in the livecode programming language

put 3 into fruit["apples"]
put 5 into fruit["pears"]
put 6 into fruit["oranges"]
put "none" into fruit["bananas"]

put "Keys:" & cr & the keys of fruit & cr into tTmp
put "Values 1:" & tab after tTmp
repeat for each line tKey in the keys of fruit
    put fruit[tkey] & comma after tTmp
end repeat

-- need to copy array as combine will change variable
put fruit into fruit2
combine fruit2 using comma
put cr & "Values2:" & tab after tTmp
repeat for each item f2val in fruit2
    put f2val & comma after tTmp
end repeat

combine fruit using return and ":"
put cr & "Key:Values" & cr & fruit after tTmp
-- alternatively, use same loop as for values 1 with tkey && fruit[tKey]

put tTmp

Keys:
apples
pears
oranges
bananas
Values 1:	3,5,6,none,
Values2:	3,none,6,5,
Key:Values
apples:3
bananas:none
oranges:6
pears:5

  

You may also check:How to resolve the algorithm File size step by step in the REXX programming language
You may also check:How to resolve the algorithm Ulam spiral (for primes) step by step in the Racket programming language
You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the PHP programming language
You may also check:How to resolve the algorithm Leap year step by step in the C programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the Necromantus programming language