Library for using associative arrays as indexed arrays
Posted: Mon Feb 13, 2017 4:33 pm
Hi
I have an array of line items where the user can delete line at a time and I have a parallel Array/customproperty that hold other information. The routines that I have, before adding this request assumed the lines were contiguous.
So searched in the dictionary to see if tere was a way of Squishing my array after the element was removed but I came up with nothing that I can see. So I quickly whipped up something that works perfectly for my situation but it doesn't seem elegant or in anyway "right" when we have associative arrays and nothing built in to "cleanup" when we delete elements.
Has anybody got a better/simpler way - there have to be at least 5 methods (it's Livecode isn't it?)
Regards Lagi
I have an array of line items where the user can delete line at a time and I have a parallel Array/customproperty that hold other information. The routines that I have, before adding this request assumed the lines were contiguous.
So searched in the dictionary to see if tere was a way of Squishing my array after the element was removed but I came up with nothing that I can see. So I quickly whipped up something that works perfectly for my situation but it doesn't seem elegant or in anyway "right" when we have associative arrays and nothing built in to "cleanup" when we delete elements.
Has anybody got a better/simpler way - there have to be at least 5 methods (it's Livecode isn't it?)
Regards Lagi
Code: Select all
function SquishArray pArray
local lcNewA, lnMaxKeys, lnX, lnNewX, lcKeys, lnOldIndex
get the keys of pArray
put the number of lines in it into lnMaxKeys
put it into lcKeys
sort lines of lcKeys Ascending Numeric
repeat with lnX = 1 to lnMaxKeys
add 1 to lnNewX
put line lnX of lcKeys into lnOldIndex
put pArray[lnOldIndex] into lcNewA[lnNewX]
end repeat
return lcNewA
end SquishArray
Code: Select all
on TestSquish
local lcA, lcNewA
put "ZZZ" into lcA[26]
put "AAA" into lcA[1]
put "GGG" into lcA[8]
put "BBB" into lca[2]
put "EEE" into lcA[5]
put SquishArray(lcA) into lcNewA
put PrintA(lcNewA,true)
end TestSquish
function PrintA pA, pWithKeys
local lcArray, lcKeys
put the keys of pA into lcKeys
put pA into lcArray
combine lcArray using return and ":"
return lcArray
end PrintA