Reputation: 7335
I'm pretty happy with the idea that Livecode Arrays are actually associative arrays, but I'm wondering if you can access them by index, rather than key?
I want to put 5 strings in to an array, so all I know how to do so far is
put "fred" into myArray[1]
put "wilma" into myArray[2]
put "barney" into myArray[3]
put "betty" into myArray[4]
put "dino" into myArray[5]
This creates me my array, and I can iterate it thru it by declaring a variable and incrementing it in a loop, accessing it with myArray[index]
In this case though, it feels to me like this is still doing a keyed lookup, and not using the index to refer to a position within the array.
Where this really hurts me is when I do
delete variable myArray[3]
Sure enough, barney gets deleted, and my array now thinks there are 4 items in it. So, if I asked for the third member of the array again, coming as I do from a Java / C background, I'd expect to get "betty" - but I don't - I get nothing, as the is now no member of the array keyed by "3"
So, can I access the array by index, or do I always have to go for a keyed retrieval?
Upvotes: 2
Views: 2964
Reputation: 21
I know this is an old thread, but coming from other languages, I've also been struggling with the lack of a true linear array in LiveCode. So for anyone else who happens upon this thread:
Since LiveCode seems to have a pretty full compliment of text methods (chunk expressions), I've been using comma delimited strings in variables instead of arrays.
In the original example above, deleting item 3, "barney" from a string, would move "betty" to the item 3 position. It would also return a length of 4 instead of 5.
This is definitely a different way of thinking, but in the absence of a linear array this has been working for me.
Upvotes: 2
Reputation: 809
Dave. You always have to refer to an array element by the key name. You can write a small function to do what you want;
function arrayIndex @pArray, pIndex
return pArray[line pIndex of the keys of pArray]
end arrayIndex
After you delete element "3", You can then access element number 3 (which is key "4") with;
put arrayIndex(myArray, 3) into tValue
HTH :D
Upvotes: 1
Reputation: 2420
You need to implement the functionality on top of arrays. Here's a couple of commands that might help you:
Remove the first instance of a known element from an array
command ArrayRemove @pArray,pValue
local tRemoved = "false"
repeat with tIndex = min(replaceText(keys(pArray),cr,comma)) to max(replaceText(keys(pArray),cr,comma))
if tRemoved then
put pArray[tIndex] into pArray[tIndex-1]
else if pArray[tIndex] = pValue then
delete variable pArray[tIndex]
put true into tRemoved
end if
end repeat
if tRemoved then
delete variable pArray[tIndex]
end if
end ArrayRemove
Remove a known index from an array
command ArrayRemoveIndex @pArray,pIndex
repeat with tIndex = pIndex+1 to max(replaceText(keys(pArray),cr,comma))
put pArray[tIndex] into pArray[tIndex-1]
end repeat
delete variable pArray[tIndex]
end ArrayRemoveIndex
Note that these commands also work with other stuff like Queues so the indexes might not start with 1. You could remove the max etc and replace with the number of elements of pArray
if you know your arrays are 1..n
If you're not positive that pIndex is within the range of the indexes of your array then you would need to add some checks to ArrayRemoveIndex
Upvotes: 2
Reputation: 2435
You can do this, but you still need to use the index. Since you want to use a numeric index, you need to sort the keys. (LiveCode arrays are fast, because the keys are unsorted).
put the keys of myArray into myKeys
sort the lines of myKeys numeric
put myArray[line 3 of myKeys] into myName
You can make this a (fake) one-liner by using a function:
-- usage:
put myArray[sortedKey(3,myArray)] into myName
function sortedKey(theIndex,theArray)
put theKeys of theArray into myKeys
sort the lines of myKeys numeric
return line theIndex of myKeys
end sortedKey
Evidently, there are multiple solutions to this problem. Keep in mind that sorting slows down the handling of the array.
Upvotes: 1