Jump to content

Array question.


Recommended Posts

Hello AutoIt people,

I'm trying to create a function that will tell whether the two arrays $x and $y with the same index limit $no are equal---equal in the sense that an element of $x has a corresponding equal element on $y. The function will return "True" if that's the case and "False" if otherwise.

#include-once
; . . . . .
Func _IsArrayEquivalent(ByRef $x, ByRef $y)

If Ubound($x) = Ubound($y) Then
    $no = Ubound($x)
    $count1 = 0
    For $countx = 0 to $no - 1 Step +1
        For $county = 0 to $no - 1 Step +1
            If $x[$countx] = $y[$county] Then
                $county = $no - 1
                $count1 += 1
            EndIf
        Next
    Next

    $count2 = 0
    For $county = 0 to $no - 1 Step +1
        For $countx = 0 to $no - 1 Step +1
            If $y[$county] = $x[$countx] Then
                $countx = $no - 1
                $count2 += 1
            EndIf
        Next
    Next

    If $count1 = $no And $count2 = $no Then
        Return "True"
    Else
        Return "False"
    EndIf

Else
    Return "False"
EndIf
EndFunc


; T R I A L
Dim $a[4] = ["Ba", "Ba", "Bi", "Bi"], $b[4] = ["Ba", "Bi", "Bi", "Bi"]

msgbox(0,"", _IsArrayEquivalent($a, $b))

The problem is that it is limited to those arrays whose every element occurs only once. So, the trial I did above will return "True" even though it should be false.

Help please? Thanks!

L. Go

Edited by Lilbert
Link to comment
Share on other sites

See Ubound() in the help file.

MsgBox(0, "Result", Ubound($x) = Ubound($y))

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Something like this ?

Global $a[4] = ["Ba", "Ba", "Bi", "Bi"], $b[4] = ["Ba", "Bi", "Bi", "Bi"]
ConsoleWrite ( " : " & _IsArrayEquivalent ( $a, $b ) & @Crlf )

Global $a[4] = ["Bo", "Bi", "Ba", "Bu"], $b[4] = ["Bo", "Bi", "Ba", "Bu"]
ConsoleWrite ( " : " & _IsArrayEquivalent ( $a, $b ) & @Crlf )

Func _IsArrayEquivalent ( $_Array1, $_Array2 )
    Local $_U1 = UBound ( $_Array1 ) -1, $_U2 = UBound ( $_Array2 ) -1
    If $_U2 <> $_U1 Then Return 0
    For $_I = 0 To $_U1
        ConsoleWrite ( "-->-- $_Array1[" & $_I & "] : " & $_Array1[$_I] & " -- $_Array2[" & $_I & "] : " & $_Array2[$_I] & @Crlf )
        If $_Array1[$_I] <> $_Array2[$_I] Then Return 0
    Next
    Return 1
EndFunc

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

A small modification to wakillon's script:

#include <Array.au3>

Global $a[4] = ["Ba", "Ba", "Bi", "Bi"], $b[4] = ["Ba", "Bi", "Bi", "Bi"]
ConsoleWrite ( " : " & _IsArrayEquivalent ( $a, $b ) & @Crlf )

Global $a[4] = ["Bo", "Bi", "Ba", "Bu"], $b[4] = ["Ba", "Bi", "Bo", "Bu"]
ConsoleWrite ( " : " & _IsArrayEquivalent ( $a, $b ) & @Crlf )

Func _IsArrayEquivalent($_Array1, $_Array2)
    Local $_U1 = UBound($_Array1), $_U2 =UBound( $_Array2)
    If $_U2 <> $_U1 Then Return 0
    Local $aT1 = $_Array1, $aT2 = $_Array2
    _ArraySort($aT1)
    _ArraySort($aT2)
    For $_I = 0 To $_U1 - 1
        ConsoleWrite ( "-->-- $aT1[" & $_I & "] : " & $aT1[$_I] & " -- $aT2[" & $_I & "] : " & $aT2[$_I] & @Crlf )
        If $aT1[$_I] <> $aT2[$_I] Then Return 0
    Next
    Return 1
EndFunc

If order of the elements doesn't matter!

Au revoir,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

@Lilbert,

Depending on your actual needs, you may have to refine a bit more good UEZ code.

Do you consider equality case sensitive or not?

Do you consider elements types (i.e. does "123" equal 123)?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

A small modification to UEZ's script Posted Image

#include <Array.au3>

Global $a[4] = ["Ba", "Ba", "Bi", "Bi"], $b[4] = ["Ba", "Bi", "Bi", "Bi"]
ConsoleWrite ( "!->-- _IsArrayEquivalent : " & _IsArrayEquivalent ( $a, $b ) & @Crlf )

Global $a[4] = ["Bo", "Bi", "Ba", "Bu"], $b[4] = ["Bo", "Bi", "Ba", "BU"]
ConsoleWrite ( "!->-- _IsArrayEquivalent : " & _IsArrayEquivalent ( $a, $b ) & @Crlf )

Global $a[4] = ["Bo", "Ba", "Bu", "Bi"], $b[4] = ["Bo", "Bi", "Ba", "Bu"]
ConsoleWrite ( "!->-- _IsArrayEquivalent : " & _IsArrayEquivalent ( $a, $b ) & @Crlf )

Global $a[4] = ["Bo", "Ba", "Bu", "Bi"], $b[4] = ["Bo", "Bi", "Ba", "Bu"]
ConsoleWrite ( "!->-- _IsArrayEquivalent : " & _IsArrayEquivalent ( $a, $b, 0 ) & @Crlf )

Func _IsArrayEquivalent ( $_Array1, $_Array2, $_Order=1, $_CaseSensitive=1 )
    Local $_U1 = UBound ( $_Array1 ) -1, $_U2 = UBound ( $_Array2 ) -1
    If $_U2 <> $_U1 Then Return 0
    Local $aT1 = $_Array1, $aT2 = $_Array2
    If $_Order Then
        _ArraySort ( $aT1 )
        _ArraySort ( $aT2 )
    EndIf
    For $_I = 0 To $_U1
        ConsoleWrite ( "-->-- $_Array1[" & $_I & "] : " & $aT1[$_I] & " -- $_Array2[" & $_I & "] : " & $aT2[$_I] & @Crlf )
        If $aT1[$_I] <> $aT2[$_I] Then Return 0
        If $_CaseSensitive And Not ( $aT1[$_I] == $aT2[$_I] ) Then Return 0
    Next
    Return 1
EndFunc ;==> _IsArrayEquivalent ( )

Edit : For strings only.

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

Thanks guys! I'll try the script and try to digest how it works. Takes some time for me to do so.

(And LOL. I should've not posted my script. Way too embarrassing for you guys to see it!)

Link to comment
Share on other sites

Thanks guys! I'll try the script and try to digest how it works. Takes some time for me to do so.

(And LOL. I should've not posted my script. Way too embarrassing for you guys to see it!)

You should have posted it, and definitely it is nothing to be embarrassed about.

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.
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...