# Find matching positions in two arrays

Go to solution Solved by Nine,

## Recommended Posts

hello! Given the following two-dimensional arrays A and B, I want to find out where B is located in A.

It's a very difficult challenge for me, so I'm asking for help. I don't know which part to improve. Please advise if you have any suggestions for improvement or other methods.

That is, I want the result to be 6 rows and 4 columns. Thank you!

##### Share on other sites

My try with StringInStr:

```#include <Array.au3>

Global \$A = [[1, 2, 22,'a4', 20, 22, 66, 23],   _
[ 2, 5, 23,'a4', 34, 31, 34, 55],   _
[ 2, 5, 23,'a4', 34, 31, 34, 55],   _
[ 1, 2, 22,'a4', 20, 22, 66, 23],   _
[ 2, 5, 23,'a4', 34, 31, 34, 55],   _
[ 2, 5, 23,'a4', 34, 31, 34, 55],   _; 6,4
[ 5, 1, 20,'a4', 22, 44, 55, 66],   _;
[ 6, 7, 10,'a4', 55, 66, 33, 44],   _;
[ 5, 1, 20,'a4', 55, 66, 33, 44],   _
[ 2, 5, 23,'a4', 22, 44, 55, 66],   _
[ 1, 2, 86,'a4', 55, 66, 33, 44]]

Global \$B = [['a4', 34, 31, 34, 55],    _
[ 'a4', 22, 44, 55, 66],    _
[ 'a4', 55, 66, 33, 44]]
;MsgBox(\$MB_SYSTEMMODAL,'',UBound(\$A,2)&' '&UBound(\$b,2) ) ;3임 3,7
Global \$sA= _ArrayToString(\$A)
\$sA = StringSplit(\$sA, @CRLF, 3)
;_ArrayDisplay(\$sA)
Global \$sB= _ArrayToString(\$B)
\$sB = StringSplit(\$sB, @CRLF, 3)
;_ArrayDisplay(\$sB)

For \$i = 0 To UBound(\$sB) -1
For \$j = 0 To UBound(\$sA) -1
\$iPos = StringInStr(\$sA[\$j], \$sB[\$i])
If \$iPos  Then ConsoleWrite('Match [' & \$j & '|' & \$i & ']: ' & \$sB[\$i] & ' is in ' & \$sA[\$j] & @CRLF)
Next
Next```

##### Share on other sites

• Solution

This should give right result :

```#include <Array.au3>

Global \$A = [[1, 2, 22,'a4', 20, 22, 66, 23],   _
[ 2, 5, 23,'a4', 34, 31, 34, 55],   _
[ 2, 5, 23,'a4', 34, 31, 34, 55],   _
[ 1, 2, 22,'a4', 20, 22, 66, 23],   _
[ 2, 5, 23,'a4', 34, 31, 34, 55],   _
[ 2, 5, 23,'a4', 34, 31, 34, 55],   _; 6,4
[ 5, 1, 20,'a4', 22, 44, 55, 66],   _;
[ 6, 7, 10,'a4', 55, 66, 33, 44],   _;
[ 5, 1, 20,'a4', 55, 66, 33, 44],   _
[ 2, 5, 23,'a4', 22, 44, 55, 66],   _
[ 1, 2, 86,'a4', 55, 66, 33, 44]]

Global \$B = [['a4', 34, 31, 34, 55],    _
[ 'a4', 22, 44, 55, 66],    _
[ 'a4', 55, 66, 33, 44]]

Local \$a1 = StringSplit(_ArrayToString(\$A), @CRLF, 3)
Local \$b1 = StringSplit(_ArrayToString(\$B), @CRLF, 3)

Local \$iPos, \$iTmp

For \$i = 0 to UBound(\$a1) - UBound(\$b1)
\$iPos = StringInStr(\$a1[\$i], \$b1[0], \$STR_CASESENSE)
If Not \$iPos Then ContinueLoop
For \$j = 1 To UBound(\$b1) - 1
\$iTmp = StringInStr(\$a1[\$i + \$j], \$b1[\$j], \$STR_CASESENSE)
If \$iTmp <> \$iPos Then ContinueLoop 2
Next
StringReplace(StringLeft(\$a1[\$i], \$iPos), "|", "|", 0, \$STR_CASESENSE)
Exit MsgBox(\$MB_SYSTEMMODAL, "Found", "At " & \$i + 1 & "/" & @extended + 1)
Next

There is 2 very important assumptions I made :

1. There is only 1 possible match of \$b1[x] within any line of \$a1
2. Cells in \$A have same length for each columns

If those assumptions are not respected, you will need to adapt my code

Edited by Nine

“They did not know it was impossible, so they did it” ― Mark Twain

Spoiler

##### Share on other sites

I tested the code you posted. Running the code posted by AutoBert didn't give me the desired result, but thanks for the reply! The code 9 wrote was what I wanted. It runs fine. Thank you!

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...