Jump to content

# Lesson in Logic

## Recommended Posts

Hello,

I had an odd idea in my head to try and compare arrays as a whole to each other to see what might be true. Can someone explain the following to me?

```Global \$a_Values[3] = [1,2,3]
Global \$a_Stuff[3] = [0,0,0]

If (\$a_Values == \$a_Stuff) Then
MsgBox(0,"Equal!","-----------" & @CRLF & \$a_Stuff & @CRLF & \$a_Values & @CRLF & "-----------")
Else
MsgBox(0,"Not Equal!","-----------" & @CRLF & \$a_Stuff & @CRLF & \$a_Values & @CRLF & "-----------")
EndIf```

This code is always true and the MsgBox always returns:

```-----------

-----------```

If I refer to an entire array, I figured that it would return the pointer to the array. Why does it return ""?

#### Share this post

##### Share on other sites

Hi,

it's required, to compare not the handle of arrays, but the elements of this.

Here my solution:

```Global \$a_Values[3] = [1,2,3]
Global \$a_Stuff[3] = [0,0,0]

Global \$ret = _GetIntersection(\$a_Values, \$a_Stuff, 1)
If IsArray(\$ret) Then
Local \$s1 = '', \$s2 = ''
For \$i = 0 To UBound(\$ret) -1
\$s1 &= \$ret[\$i][1]
\$s2 &= \$ret[\$i][2]
Next
If (\$s1 = '') And (\$s2 = '') Then
MsgBox(0, '', 'Arrays are equal')
Else
MsgBox(0, '', 'Arrays are not equal')
EndIf
;~    _ArrayDisplay(\$ret)
EndIf
;==================================================================================================
; Function Name:   _GetIntersection(\$Set1, \$Set2 [, \$GetAll=0 [, \$Delim=Default]])
; Description::    Detect from 2 sets
;                  - Intersection (elements are contains in both sets)
;                  - Difference 1 (elements are contains only in \$Set1)
;                  - Difference 2 (elements are contains only in \$Set2)
; Parameter(s):    \$Set1    set 1 (1D-array or delimited string)
;                  \$Set2  set 2 (1D-array or delimited string)
;      optional:   \$GetAll  0 - only one occurence of every different element are shown (Default)
;                           1 - all elements of differences are shown
;      optional:   \$Delim   Delimiter for strings (Default use the separator character set by Opt("GUIDataSeparatorChar") )
; Return Value(s): Succes   2D-array    [i][0]=Intersection
;                                       [i][1]=Difference 1
;                                       [i][2]=Difference 2
;                  Failure  -1    @error  set, that was given as array, is'nt 1D-array
; Note:            Comparison is case-sensitiv! - i.e. Number 9 is different to string '9'!
; Author(s):       BugFix (bugfix@autoit.de)
;==================================================================================================
Func _GetIntersection(ByRef \$Set1, ByRef \$Set2, \$GetAll=0, \$Delim=Default)
Local \$o1 = ObjCreate("System.Collections.ArrayList")
Local \$o2 = ObjCreate("System.Collections.ArrayList")
Local \$oUnion = ObjCreate("System.Collections.ArrayList")
Local \$oDiff1 = ObjCreate("System.Collections.ArrayList")
Local \$oDiff2 = ObjCreate("System.Collections.ArrayList")
Local \$tmp, \$i
If \$GetAll <> 1 Then \$GetAll = 0
If \$Delim = Default Then \$Delim = Opt("GUIDataSeparatorChar")
If Not IsArray(\$Set1) Then
If Not StringInStr(\$Set1, \$Delim) Then
\$o1.Add(\$Set1)
Else
\$tmp = StringSplit(\$Set1, \$Delim, 1)
For \$i = 1 To UBound(\$tmp) -1
\$o1.Add(\$tmp[\$i])
Next
EndIf
Else
If UBound(\$Set1, 0) > 1 Then Return SetError(1,0,-1)
For \$i = 0 To UBound(\$Set1) -1
\$o1.Add(\$Set1[\$i])
Next
EndIf
If Not IsArray(\$Set2) Then
If Not StringInStr(\$Set2, \$Delim) Then
\$o2.Add(\$Set2)
Else
\$tmp = StringSplit(\$Set2, \$Delim, 1)
For \$i = 1 To UBound(\$tmp) -1
\$o2.Add(\$tmp[\$i])
Next
EndIf
Else
If UBound(\$Set2, 0) > 1 Then Return SetError(1,0,-1)
For \$i = 0 To UBound(\$Set2) -1
\$o2.Add(\$Set2[\$i])
Next
EndIf
For \$tmp In \$o1
If \$o2.Contains(\$tmp) And Not \$oUnion.Contains(\$tmp) Then \$oUnion.Add(\$tmp)
Next
For \$tmp In \$o2
If \$o1.Contains(\$tmp) And Not \$oUnion.Contains(\$tmp) Then \$oUnion.Add(\$tmp)
Next
For \$tmp In \$o1
If \$GetAll Then
If Not \$oUnion.Contains(\$tmp) Then \$oDiff1.Add(\$tmp)
Else
If Not \$oUnion.Contains(\$tmp) And Not \$oDiff1.Contains(\$tmp) Then \$oDiff1.Add(\$tmp)
EndIf
Next
For \$tmp In \$o2
If \$GetAll Then
If Not \$oUnion.Contains(\$tmp) Then \$oDiff2.Add(\$tmp)
Else
If Not \$oUnion.Contains(\$tmp) And Not \$oDiff2.Contains(\$tmp) Then \$oDiff2.Add(\$tmp)
EndIf
Next
Local \$UBound[3] = [\$oDiff1.Count,\$oDiff2.Count,\$oUnion.Count], \$max = 1
For \$i = 0 To UBound(\$UBound) -1
If \$UBound[\$i] > \$max Then \$max = \$UBound[\$i]
Next
Local \$aOut[\$max][3]
If \$oUnion.Count > 0 Then
\$i = 0
For \$tmp In \$oUnion
\$aOut[\$i][0] = \$tmp
\$i += 1
Next
EndIf
If \$oDiff1.Count > 0 Then
\$i = 0
For \$tmp In \$oDiff1
\$aOut[\$i][1] = \$tmp
\$i += 1
Next
EndIf
If \$oDiff2.Count > 0 Then
\$i = 0
For \$tmp In \$oDiff2
\$aOut[\$i][2] = \$tmp
\$i += 1
Next
EndIf
Return \$aOut
EndFunc  ;==>_GetIntersection```
Edited by BugFix

Best Regards BugFix

#### Share this post

##### Share on other sites

Hello,

I had an odd idea in my head to try and compare arrays as a whole to each other to see what might be true. Can someone explain the following to me?

```Global \$a_Values[3] = [1,2,3]
Global \$a_Stuff[3] = [0,0,0]

If (\$a_Values == \$a_Stuff) Then
MsgBox(0,"Equal!","-----------" & @CRLF & \$a_Stuff & @CRLF & \$a_Values & @CRLF & "-----------")
Else
MsgBox(0,"Not Equal!","-----------" & @CRLF & \$a_Stuff & @CRLF & \$a_Values & @CRLF & "-----------")
EndIf```

This code is always true and the MsgBox always returns:

```-----------

-----------```

If I refer to an entire array, I figured that it would return the pointer to the array. Why does it return ""?

It is reasonable to expect that the variable name of an array might return a pointer, and no doubt in the bowels of AutoIt that is what it represents. But pointers are not exposed in AutoIt except when dealing with DllStructs.

The variable doesn't return "" either as far as I know because if you test the variable like this

`If IsString(\$a_Stuff) then consolewrite("string" & @CR)`

nothing will be printed and the same for all the other datya types you can test for.

Trying to get the pointer to a variable in AutoIt is something I have tried to do before but with no success. I wrote a dll which I passed a pointer to an integer like this

```\$r = 175;some value
\$result = DllCall("getthepoint.dll","int","pointer","int*",\$r);tried the return type of "ptr" and "int*" as well as "int"
\$t = DllSTructCreate("int",\$result[0])
ConsoleWrite("value of \$r = " & DllStructGetData(\$t,1))```

but although the dll gets the correct value of \$r using the pointer it is passed, and returns that pointer to the script, the dllstruct doesn't contain the value of \$r. If anyone can see that I'm doing something wrong I'd be interested. Maybe DllCall makes a temporary copy of the variables rather than use pointers to the normal location in memory.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

#### Share this post

##### Share on other sites

I don't see a problem with your code, but I'm still a novice in C++ at best.

So, I take it that the parser for this language is able to pass pointers properly. For example:

```Global \$a_Array[3] = [1,2,3]
Global \$a_NewArray[3] = [0,0,0]

\$a_Array = \$a_NewArray```

Even though we the developers cannot see/alter them. Just thought it was odd. I was expecting a handle/pointer.

I did come up with my own UDF to compare arrays properly, but it is rather slow when comparing 3+ dimensional arrays (obviously).

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

×

• Wiki

• Back

• Git

## Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.