Are CompareArray() CompareList() and CompareMap() usable?

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7579
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Are CompareArray() CompareList() and CompareMap() usable?

Post 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 :?:
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Are CompareArray() CompareList() and CompareMap() usable?

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Are CompareArray() CompareList() and CompareMap() usable?

Post by Little John »

How cool is that?! 8) :)
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Are CompareArray() CompareList() and CompareMap() usable?

Post 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) :)
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Are CompareArray() CompareList() and CompareMap() usable?

Post by mk-soft »

Then we do a few beta tests with structures and substructures.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Are CompareArray() CompareList() and CompareMap() usable?

Post 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.
quidquid Latine dictum sit altum videtur
Post Reply