Is there any way how i could compare arrays like pattern matching or should i write my own code?

Thanks.

Dani AI

Generated

Short answer: you can do better than a naive nested loop. is right that VB string functions (Like/Filter) and simple loops will work for single-element checks, but for the contiguous‑subarray problem that asked about (does array A occur as a block inside array B?) you should pick one of three practical approaches depending on size and constraints.

  1. Join-to-string + InStr — fast to implement. Convert each element to a string, pick a delimiter that cannot appear in your data, build a single string for B and a single string for A (surrounded by delimiters) and use InStr/IndexOf. This uses highly optimized string search and often beats hand-written nested loops for moderate sizes. Be careful to choose/escape the delimiter and to convert numbers consistently.

Example idea (pseudocode):

' make S = "|" & Join(MapToString(B), "|") & "|"
' make P = "|" & Join(MapToString(A), "|") & "|"
If InStr(S, P) > 0 Then  ' pattern found
  1. Knuth–Morris–Pratt (KMP) — deterministic O(n + m). Implement KMP treating each array element as a “character” and equality as element equality. It finds a contiguous match in linear time and uses only O(m) extra space. Good when arrays are large or you need worst‑case guarantees.

KMP sketch (pseudocode): compute prefix table for pattern A, then scan B advancing pattern index with the prefix table on mismatches; report match when pattern index reaches pattern length.

  1. Rabin–Karp (rolling hash) — often simplest to implement and fast on average. Compute a rolling hash over B and compare to hash(A); verify on hash match to avoid false positives.

Practical tips: flatten the 3D slice you want to search before applying any method; measure with a simple timer; avoid very large intermediary strings if memory is tight (prefer KMP or rolling hash in that case). For most VB/VB6 tasks the join+InStr trick is the quickest to try; switch to KMP/Rabin–Karp only if profiling shows it’s necessary.

Recommended Answers

All 7 Replies

Hi,

You can use Like Operator or StrComp Function for Pattern Matching in Array Variable or any other Variable. You can compare the Values like below Given:

1) Bring the Array Values in loop and check the Value like this

IF a(I) LIKE "*a*" THEN
-----
-----
END IF
You can refer more about the Like Operator in

http://msdn.microsoft.com/en-us/library/swf8kaxw(VS.71).aspx


2) You can use StrComp Function like this

IF StrComp(a(I), "a") THEN
-----
-----
END IF

Shailaja:)


Is there any way how i could compare arrays like pattern matching or should i write my own code?

Thanks.

And there is no way to find "pattern" of one array in other? something like a(0) = b, a(1) = c and b(0) = a, b(1) = b, b(2) = c, b(3) = d.

bool = a like *b*

???

hi,

You can do like below when using the variable instead of values.


IF a(0) like "*" & b & "*" THEN
-----------
-----------
END IF

Shailaja:)

And there is no way to find "pattern" of one array in other? something like a(0) = b, a(1) = c and b(0) = a, b(1) = b, b(2) = c, b(3) = d.

bool = a like *b*

???

Private Sub Command1_Click()
Dim A(2) As String
Dim B(4) As String

    A(0) = 3
    A(1) = 2
    A(2) = 67
    
    B(0) = 89
    B(1) = 3
    B(2) = 2
    B(3) = 67
    B(4) = 99
    
    If A(0) Like "*" & B & "*" Then
        MsgBox "ahoy"
    End If
    
End Sub

It looks like ill have to use loops, because these is not working. And i don't need one value i need to find is one entire dimension of array in other. I am afraid loops will last too long.

hi,

You can do like below when using the variable instead of values.


IF a(0) like "*" & b & "*" THEN
-----------
-----------
END IF

Shailaja:)

hi,

Array can be used only by loops and that could be very if u understand wel.
Try the below coding and if you have any doubts in below codings you can ask me i will explain u in detail.

In Below coding, i had used nested loop (i.e). Loop is nothing but Loop is to make a rotation until it gets the end value. If it is Nested Loop then 2 loops used (i.e)
For i = 0 to 10 'Outer Loop
For j = 0 to 5 'Inner Loop
---------
Next
Next

Outer Loop have the value and comes to the inner loop. Then the inner loop starts the rotation until it getting the end value after that move to the outer loop and goes on until the outer loop ends. Simply we can say one element of outer loop compare with all element in the inner loop.

If u understand the loop concept clearly then only u can do anything in any language.


Private Sub Command1_Click()
Dim A(2) As String
Dim B(4) As String
Dim intI as Integer       'intI Variable for A()
Dim intj as Integer       'intJ Variable for B()

    A(0) = 3
    A(1) = 2
    A(2) = 67
    
    B(0) = 89
    B(1) = 3
    B(2) = 2
    B(3) = 67
    B(4) = 99
    
 'Nested Loop
     For intI = 0 to Ubound(A)
           For intJ = 0 to Ubound(B)  'Loop works until the size of the B() Array
                If A(intI) Like "*" & B(intJ) & "*" Then  'A() compared with the all element in the B() 
                      MsgBox A(intI)
                End If
          Next
     Next  

    
End Sub

Shailaja:)

Private Sub Command1_Click()
Dim A(2) As String
Dim B(4) As String

    A(0) = 3
    A(1) = 2
    A(2) = 67
    
    B(0) = 89
    B(1) = 3
    B(2) = 2
    B(3) = 67
    B(4) = 99
    
    If A(0) Like "*" & B & "*" Then
        MsgBox "ahoy"
    End If
    
End Sub

It looks like ill have to use loops, because these is not working. And i don't need one value i need to find is one entire dimension of array in other. I am afraid loops will last too long.

Tanks but i wanted to avoid that because i am using 3d arrays with 100+ values per one i was hoping i could do it faster.

Hi,

Try this one. I think this may be useful to u.

Private Sub Command1_Click()
Dim A(2) As String
Dim B(4) As String
Dim C as String
    A(0) = 3
    A(1) = 2
    A(2) = 67
    
    B(0) = 89
    B(1) = 3
    B(2) = 2
    B(3) = 67
    B(4) = 99

    IF UBound(Filter(B, A(0), True, vbTextCompare)) >-1 THEN
          C=Filter(B, A(0), True, vbTextCompare)    
           MsgBox C(0)
    End If
    
End Sub

To Know more


Shailaja:)

Tanks but i wanted to avoid that because i am using 3d arrays with 100+ values per one i was hoping i could do it faster.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.