Page 1 of 1
Are CompareArray() CompareList() and CompareMap() usable?
Posted: Thu Aug 24, 2023 12:11 pm
by infratec
Hi,
I found that there are procedures available, also with syntax hints, but with no documentation.
CompareArray()
CompareList()
CompareMap()
Are they deprecated? for future version? or simply forgotten to add in the documentation?
@Fred
Can we use them

Re: Are CompareArray() CompareList() and CompareMap() usable?
Posted: Thu Aug 24, 2023 12:47 pm
by mk-soft
And new since v6.01 compare structure
@Fred
Thats valid functions ?
Code: Select all
Structure udtData
iVal.i
sVal.s
EndStructure
Global ds1.udtData
Global ds2.udtData
ds1\iVal = 100
ds1\sVal = "Hello World"
CopyStructure(ds1, ds2, udtData)
r1 = CompareStructure(ds1, ds2, udtData)
Debug r1
ds2\sVal = "hello world"
r1 = CompareStructure(ds1, ds2, udtData)
Debug r1
r1 = CompareStructure(ds1, ds2, udtData, #PB_String_NoCase)
Debug r1
Re: Are CompareArray() CompareList() and CompareMap() usable?
Posted: Thu Aug 24, 2023 1:24 pm
by Little John
How cool is that?!

Re: Are CompareArray() CompareList() and CompareMap() usable?
Posted: Thu Aug 24, 2023 3:21 pm
by Fred
It was meant for 6.10 but yeah it should be usable as I forgot to deactivate them, but consider them beta (ie : no bug fixes planned)

Re: Are CompareArray() CompareList() and CompareMap() usable?
Posted: Thu Aug 24, 2023 5:53 pm
by mk-soft
Then we do a few beta tests with structures and substructures.
Re: Are CompareArray() CompareList() and CompareMap() usable?
Posted: Thu Aug 24, 2023 5:58 pm
by freak
They are meant to replace the various hacks for structure comparison that can be found around the forum (using JSON serialization for example). The functions perform a recursive comparison of the structure and anything within, even nested Arrays, Lists or Maps or other structures.
Example:
Code: Select all
Structure TestStruct
value.l
List sublist.l()
EndStructure
Define a.TestStruct
AddElement(a\sublist()): a\sublist() = 1
AddElement(a\sublist()): a\sublist() = 2
; same
Define b.TestStruct
AddElement(b\sublist()): b\sublist() = 1
AddElement(b\sublist()): b\sublist() = 2
Debug CompareStructure(a, b, TestStruct)
; differnt
Define c.TestStruct
AddElement(c\sublist()): c\sublist() = 1
AddElement(c\sublist()): c\sublist() = 2
AddElement(c\sublist()): c\sublist() = 3
Debug CompareStructure(a, c, TestStruct)
You already discovered the #PB_String_NoCase flag to make string comparisons case insensitive. There is also a second possible flag to tell the command to follow pointers within the structure (if they are not 0) and compare the pointed value instead of the pointer itself.
Example:
Code: Select all
#PB_Memory_FollowPointers = (1 << 8)
Structure StructWithPtr
*Ptr.Long
EndStructure
Define v1.l = 123
Define v2.l = 123 ; same value as v1 but at a different address
Define a.StructWithPtr
a\Ptr = @v1
Define b.StructWithPtr
b\Ptr = @v2
; Not equal because the numeric pointer value "Ptr" is not the same
Debug CompareStructure(a, b, StructWithPtr)
; Equal because both pointers point to content that compares as equal
Debug CompareStructure(a, b, StructWithPtr, #PB_Memory_FollowPointers)
; A null pointer is not followed and considered equal only to another null pointer
Define c.StructWithPtr
c\Ptr = 0
Debug CompareStructure(a, c, StructWithPtr, #PB_Memory_FollowPointers)
; Pointers with invalid values are not allowed with #PB_Memory_FollowPointers for obvious reasons
; (same for recursive structures that point to themselves -> endless loop)
Define d.StructWithPtr
d\Ptr = 9999999999999
; This will crash!
; Debug CompareStructure(a, d, StructWithPtr, #PB_Memory_FollowPointers)
; This is safe however because without #PB_Memory_FollowPointers the pointer is just compared as a number
Debug CompareStructure(a, d, StructWithPtr)
CompareArray(), CompareList() and CompareMap() work the same way except on entire Arrays, Lists or Maps instead of just one structure pointer.