Results 1 to 24 of 24

Thread: Compare Array Without Loop

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Compare Array Without Loop

    I have done some searching and from what I have read I don't think this is possible, but I thought I would ask anyway.

    Is there a way to do this:
    VB Code:
    1. Dim a(2) As Boolean
    2. Dim b(2) As Boolean
    3.  
    4. a(0) = False
    5. a(1) = True
    6. a(2) = True
    7.  
    8. b(0) = False
    9. b(1) = True
    10. b(2) = True
    11.  
    12. If a = b Then MsgBox("yay")
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Compare Array Without Loop

    not in VB6..... in .NET it can be done... but even that takes some code writing.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Compare Array Without Loop

    Who says you can't in VB6? Using a UDT should help

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type tTest
    4.     Matrix(2) As Long
    5. End Type
    6.  
    7. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As String, pSrc As Any, ByVal ByteLen As Long)
    8.  
    9. Private Sub Form_Activate()
    10.  
    11.     Dim A As tTest, B As tTest
    12.    
    13.     A.Matrix(0) = 5
    14.     A.Matrix(1) = 10
    15.     A.Matrix(2) = 15
    16.    
    17.     MsgBox TypeEquals(A, B) ' False
    18.    
    19.     B.Matrix(0) = 5
    20.     B.Matrix(1) = 10
    21.     B.Matrix(2) = 15
    22.    
    23.     MsgBox TypeEquals(A, B) ' True
    24.  
    25. End Sub
    26.  
    27. Private Function TypeEquals(A As tTest, B As tTest) As Boolean
    28.  
    29.     Dim StrA As String, StrB As String
    30.    
    31.     StrA = String(Len(A), 0)
    32.     CopyMemory StrA, A, Len(A)
    33.    
    34.     StrB = String(Len(A), 0)
    35.     CopyMemory StrB, B, Len(A)
    36.    
    37.     TypeEquals = StrA = StrB
    38.    
    39. End Function

  4. #4
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Compare Array Without Loop

    Even Better. Don't need UDT's after all!!!

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As String, pSrc As Any, ByVal ByteLen As Long)
    4.  
    5. Private Sub Form_Activate()
    6.  
    7.     Dim A(2) As Long, B(2) As Long
    8.    
    9.     A(0) = 5
    10.     A(1) = 10
    11.     A(2) = 15
    12.    
    13.     MsgBox ArrayEquals(A, B) ' False
    14.    
    15.     B(0) = 5
    16.     B(1) = 10
    17.     B(2) = 15
    18.    
    19.     MsgBox ArrayEquals(A, B) ' True
    20.  
    21. End Sub
    22.  
    23. Private Function ArrayEquals(A() As Long, B() As Long) As Boolean
    24.  
    25.     Dim StrA As String, StrB As String
    26.    
    27.     StrA = String(Len(A(0)) * UBound(A), 0)
    28.     CopyMemory StrA, A(0), Len(A(0)) * UBound(A)
    29.    
    30.     StrB = String(Len(A(0)) * UBound(A), 0)
    31.     CopyMemory StrB, B(0), Len(A(0)) * UBound(A)
    32.    
    33.     ArrayEquals = StrA = StrB
    34.    
    35. End Function

  5. #5
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Re: Compare Array Without Loop

    Don't forget ...

    VB Code:
    1. If VB cant do it Then
    2.  
    3.         Do it Yourself
    4.  
    5. End If

    Here's a quikly put together module that you could reuse in any application and it accepts 2 arrays of any type.

    VB Code:
    1. Public Function CompareArray(ParamArray Arrays()) As Boolean
    2.  
    3.     Dim A1() As Variant
    4.     Dim A2() As Variant
    5.     Dim i As Long
    6.    
    7.     ReDim A1(UBound(Arrays(0)))
    8.     ReDim A2(UBound(Arrays(1)))
    9.        
    10.     For i = 0 To UBound(Arrays(0))
    11.    
    12.         A1(i) = Arrays(0)(i)
    13.    
    14.     Next
    15.    
    16.     For i = 0 To UBound(Arrays(1))
    17.    
    18.         A2(i) = Arrays(1)(i)
    19.    
    20.     Next
    21.        
    22.     Dim A1Size As Long
    23.     Dim A2Size As Long
    24.    
    25.     A1Size = UBound(A1)
    26.     A2Size = UBound(A2)
    27.    
    28.     If A1Size = A2Size Then
    29.                        
    30.         For i = 0 To A1Size
    31.        
    32.             If A1(i) = A2(i) Then
    33.            
    34.                 CompareArray = True
    35.            
    36.             Else
    37.            
    38.                 CompareArray = False
    39.              
    40.                 Exit For
    41.            
    42.             End If
    43.                        
    44.         Next
    45.    
    46.     Else
    47.    
    48.         CompareArray = False
    49.    
    50.     End If
    51.  
    52. End Function

    Then just call it as so...
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim A(2) As Boolean
    4.     Dim B(2) As Boolean
    5.    
    6.     A(0) = True
    7.     A(1) = False
    8.     A(2) = True
    9.    
    10.     B(0) = True
    11.     B(1) = False
    12.     B(2) = True
    13.    
    14.     [B]Label1.Caption = CompareArray(A, B)[/B]
    15.        
    16. End Sub

    Cheers Hojo
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  6. #6
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Compare Array Without Loop

    My way is faster. No For loop needed and it compares it instantaniously. And he's looking for speed.

    Imagine if there are 100000 elements. How long do you think your function will run compared to mine?

  7. #7
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Re: Compare Array Without Loop

    I get from Post 1 that he is after ease of coding therefore not having to code a loop every time he wants to compare arrays.

    Your way is limited in that you have to recode each time you have a different array type, mine will accept any. Plus if he has to code that much to avoid coding a loop he might as well code a loop.

    If speed is an issue then perhaps a migration of our two method would be the optimum solution.

    Hojo
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  8. #8
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Re: Compare Array Without Loop

    I just ran your little test and As soon as i press the compare button the label updates with the answer immediatly. (with 100000 samples in each array)
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Compare Array Without Loop

    Quote Originally Posted by Hojo
    ........updates with the answer immediatly.
    Yeh, but Hojo, it does help that your using a "CRAY" computer

  10. #10
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Re: Compare Array Without Loop

    There are super computers and there are super programmers.
    lol just kidding

    Just a plain few year old desktop.
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  11. #11
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Re: Compare Array Without Loop

    Not wanting to harp on this topic but i just relooked at my code and trimmed out the unneeded parts (this should also halve processing time)

    VB Code:
    1. Public Function CompareArray(ParamArray Arrays()) As Boolean
    2.  
    3.     Dim i As Long
    4.  
    5.     If UBound(Arrays(0)) = UBound(Arrays(1)) Then
    6.                        
    7.         For i = 0 To UBound(Arrays(0))
    8.        
    9.             If Arrays(0)(i) = Arrays(1)(i) Then
    10.            
    11.                 CompareArray = True
    12.            
    13.             Else
    14.            
    15.                 CompareArray = False
    16.              
    17.                 Exit For
    18.            
    19.             End If
    20.                        
    21.         Next
    22.    
    23.     Else
    24.    
    25.         CompareArray = False
    26.    
    27.     End If
    28.  
    29. End Function

    Cheers Hojo
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Compare Array Without Loop

    The fastest way to compare anything that can't be done directly in VB is to use RtlCompareMemory.

    VB Code:
    1. Declare Function RtlCompareMemory Lib "ntdll.dll" ( _
    2.     ByRef lpvSrc1 As Any, _
    3.     ByRef lpvSrc2 As Any, _
    4.     ByVal cbLen As Long _
    5. ) As Long
    6.  
    7. ' Usage:
    8. If (RtlCompareMemory(a(0), b(0), UBound(a) * LenB(a)) = UBound(a) * LenB(a)) Then
    9.     MsgBox "yay"
    10. End If

  13. #13
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Compare Array Without Loop

    But that's only supported for XP computers. I don't have that

    My code in post #4 also works instantaniously.

  14. #14

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Compare Array Without Loop

    Thanks for all the help guys.

    To clear things up: I am not looking for ease of coding (at the moment). I am only look for speed. I will run some speed test on the ideas you guys mentioned and see which works better in my situation. I figured copy memory had something to do with it, I just didn't know hot to use it.

    BTW - my array will only have 9 elements in it. (Can anyone guess what I am making? )
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  15. #15
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Compare Array Without Loop

    A suduku solver?

  16. #16

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Compare Array Without Loop

    Noooooooooo....


    Yeeeeeeeeees!!!!

    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  17. #17
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Compare Array Without Loop

    Good. Then post #4 is the fastest method. No performance test necessary. It's common sense.

  18. #18

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Compare Array Without Loop

    Why wouldn't penagate's be faster (post # 12)? Strings are very slow in VB. I want to avoid them at all costs.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  19. #19

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Compare Array Without Loop

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function RtlCompareMemory Lib "ntdll.dll" ( _
    4.     ByRef lpvSrc1 As Any, _
    5.     ByRef lpvSrc2 As Any, _
    6.     ByVal cbLen As Long _
    7. ) As Long
    8.  
    9.  
    10. Private Sub Form_Load()
    11.     Dim a(2)        As Boolean
    12.     Dim b(2)        As Boolean
    13.    
    14.     a(0) = False
    15.     a(1) = True
    16.     a(2) = True
    17.    
    18.     b(0) = False
    19.     b(1) = True
    20.     b(2) = True
    21.    
    22.     lLen = UBound(a) * LenB(a)
    23.    
    24.     If (RtlCompareMemory(a(0), b(0), lLen) = lLen) Then
    25.         MsgBox "yay"
    26.     End If
    27. End Sub

    I get this error: Compile error - Variable required - Can't assign to this expression

    ... and it highlights the the a withing LenB(a).

    Any ideas?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  20. #20
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Compare Array Without Loop

    RtlCompareMemory is not in all OS's. Just XP I believe. I can't use it cause I don't have the supported API in my ntdll.dll file.

    And my function only assigns values to a string twice. So there shouldnt be any impact on performance.

  21. #21

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Compare Array Without Loop

    Well, I hope whoever scores the sudoku entries has XP. . I think I will go mention that now in the sudoku thread.

    I think I am going to use penagate's, because it is easier and seems quicker.

    I fixed it also. I need to Dim lLen and change LenB(a) to LenB(a(0)) and it works now.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  22. #22
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Compare Array Without Loop

    Whoops, my bad.

    ntdll.dll, as one might expect, is part of all NT-based Windows', but will work also on Win 9x, if supplied.

  23. #23
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Compare Array Without Loop

    It's supplied, just not the RtlCompareMemory function in the dll file, unless downloaded from a dll site that contains hundreds of dll files to choose from that you can download for free. Everyone with earlier versions of Windows using your program would have to upgrade that file then, unless supplied by you.

  24. #24

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Compare Array Without Loop

    I'll grab a copy and throw it in whenever I zip up the project.

    Thanks guys.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width