Nested array comparisons
Richard Gaskin
ambassador at fourthworld.com
Mon Mar 25 09:47:12 EDT 2013
Alex Tweedly wrote:
> try it yourself if you want to see the details - but the bottom line is
> that the arrays have the same keys and contents, comparing the arrays
> gets that right, and comparing the arrayencode() of the arrays says
> (wrongly) that they differ.
>
> QED - you can't compare the arrayencoding, even in some simple cases.
>
> -- Alex.
>
> On 24/03/2013 23:53, Alex Tweedly wrote:
>> OK, let's just test it - focusing on the likelihood that key order
>> matters
>>
>> Code:
>>> on mouseup
>>> local T1, T2
>>> local tK, j
>>>
>>> constant K = 10
>>> repeat with i = 1 to K
>>> put random(i) into t1[i]
>>> end repeat
>>>
>>> put the keys of T1 into tK
>>> repeat with i = K down to 1
>>> put line i of tK into j
>>> put T1[j] into T2[j]
>>> end repeat
>>>
>>> put "array" && (T1 = T2) & CR & "code" && (arrayencode(t1) is
>>> arrayencode(t2)) \
>>> &CR & arrayencode(t1) &CR&CR & arrayencode(t2)
>>> end mouseup
>> Result:
>>> array true
>>> code false
Interestingly, if you alter the code to use alphabetic keys rather than
numeric, the two encoded arrays always match.
This fits with what I saw when I started working with Mark Waddingham's
arrayEncode parser he sent for my upcoming LiveCode Journal article, in
which most keys were ordered alphabetically.
Since numbers are stored in binary form in encoded arrays, it may be
that no sorting is done for the output as appears to be the case with
string keys.
Either way, certainly much faster to just use "=" on the arrays
directly. As we see with getting/setting nested arrays in custom props,
arrayEncode's need to iterate through every element makes it
computationally expensive for some tasks.
PS: Alex, I appreciate the code you post on this list, but why do you
always post code with each line preceded by ">"? Makes it more
difficult to strip out the ">" to run the code.
--
Richard Gaskin
Fourth World
LiveCode training and consulting: http://www.fourthworld.com
Webzine for LiveCode developers: http://www.LiveCodeJournal.com
Follow me on Twitter: http://twitter.com/FourthWorldSys
More information about the use-livecode
mailing list