Recommended Posts

Hi all,

Is hard to explain the situation but i'll try:

I have a 2D array filled with a series of number and string. This array have a sequence like:

1-A

2-B

3-C

-D

The user is limited to that, so can can be only 123ABCD. I need to know if is write in the correct order.

I'm increasing the variable \$iNumber ( and \$sFinal ) everytime the sequence is in the correct order, like:

123 ( \$iNumber = 3, \$sFinal = 123 )

ABCD ( \$iNumber = 4, \$sFinal = ABCD )

Until that work, the problem is when the string doesn't match...

Can be two possible scenario, clearly can be other combination:

12D ( in this case D is wrong, so \$iNumber = 0 and \$sFinal = "" )

12ABCD ( in this case A is wrong ( after 2 for complete the sequence is 3 the next ) but is correct for the other column, so pratically the \$iNumber will be reset after 12 and give me \$iNumber = 4, \$sFinal = ABCD )

The script:

#include <Array.au3>

Global \$aArray2D = [[1, "A"],[2, "B"],[3, "C"],["", "D"]]
Global \$sFinal, \$iNumber = 0

_ArrayDisplay(\$aArray2D)

_Sequence("A", \$aArray2D)
_Sequence("B", \$aArray2D)
_Sequence("C", \$aArray2D)
_Sequence("D", \$aArray2D)
MsgBox(0, 0, \$sFinal & " - N: " & \$iNumber)

; reset
\$iNumber = 0
\$sFinal = ""
; reset

_Sequence("1", \$aArray2D)
_Sequence("2", \$aArray2D)
_Sequence("3", \$aArray2D)
MsgBox(0, 0, \$sFinal & " - N: " & \$iNumber)

; reset
\$iNumber = 0
\$sFinal = ""
; reset

_Sequence("1", \$aArray2D)
_Sequence("2", \$aArray2D)
_Sequence("D", \$aArray2D)
MsgBox(0, 0, \$sFinal & " - N: " & \$iNumber) ; <<<<<<<<< wrong result

; reset
\$iNumber = 0
\$sFinal = ""
; reset

_Sequence("1", \$aArray2D)
_Sequence("2", \$aArray2D)
_Sequence("A", \$aArray2D)
_Sequence("B", \$aArray2D)
_Sequence("C", \$aArray2D)
_Sequence("D", \$aArray2D)
MsgBox(0, 0, \$sFinal & " - N: " & \$iNumber) ; <<<<<<<<<<< wrong result

Func _Sequence(\$sString, ByRef \$aArray)
For \$i = 0 To UBound(\$aArray, 2) - 1
If \$sString = \$aArray[\$iNumber][\$i] Then
\$iNumber += 1
\$sFinal &= \$sString
ExitLoop
EndIf
Next
EndFunc   ;==>_Sequence

You are free to change whatever you want but i need to have the same "scheme". If something is unclear, i'll try to repeat. Thanks to all

P.S. The only variable i need is \$sFinal, i'm using \$iNumber just for reference and increase the array index every match.

Edited by johnmcloud

Share on other sites

@johnmcloud of the clan mcloud

Here's another approach that appears to work.

#include <Array.au3>

Global \$aArray2D = [[1, "A"],[2, "B"],[3, "C"],["", "D"]]
Global \$sFinal

_ArrayDisplay(\$aArray2D)

\$sFinal = ""
_Sequence("A", \$aArray2D)
_Sequence("B", \$aArray2D)
_Sequence("C", \$aArray2D)
_Sequence("D", \$aArray2D)
MsgBox(0, 0, \$sFinal & " - N: " & StringLen(\$sFinal)) ; <<<<<<<<< ABCD - N: 4

\$sFinal = "" ; reset

_Sequence("1", \$aArray2D)
_Sequence("2", \$aArray2D)
_Sequence("3", \$aArray2D)
MsgBox(0, 0, \$sFinal & " - N: " & StringLen(\$sFinal)) ; <<<<<<<<< 123 - N: 3

\$sFinal = "" ; reset

_Sequence("1", \$aArray2D)
_Sequence("2", \$aArray2D)
_Sequence("D", \$aArray2D)
MsgBox(0, 0, \$sFinal & " - N: " & StringLen(\$sFinal)) ; <<<<<<<<< - N: 0

\$sFinal = "" ; reset

_Sequence("1", \$aArray2D)
_Sequence("2", \$aArray2D)
_Sequence("A", \$aArray2D)
_Sequence("B", \$aArray2D)
_Sequence("C", \$aArray2D)
_Sequence("D", \$aArray2D)
MsgBox(0, 0, \$sFinal & " - N: " & StringLen(\$sFinal)) ; <<<<<<<<<<< ABCD - N: 4

Func _Sequence(\$sString, ByRef \$aArray)
Local Static \$Row, \$Col
If \$sFinal = "" Then ; Find the position of \$String within the \$aArray
For \$i = 0 To UBound(\$aArray) - 1
For \$j = 0 To UBound(\$aArray, 2) - 1
If \$sString == \$aArray[\$i][\$j] Then
\$Row = \$i
\$Col = \$j
\$sFinal = \$sString
Return
EndIf
Next
Next
EndIf
\$Row += 1
If \$sString = \$aArray[\$Row][\$Col] Then ; Check if the incremented \$Row index of the array contains the same data as the \$sString.
\$sFinal &= \$sString
Else
\$sFinal = "" ; Initialize
_Sequence(\$sString, \$aArray) ; Reset to new \$Row and \$Col indexes.
EndIf
If (\$sFinal == \$aArray[UBound(\$aArray) - 1]) Or (\$sFinal == \$aArray[UBound(\$aArray) - 1]) Then \$sFinal = ""
EndFunc   ;==>_Sequence

#cs ; Returns:-
ABCD - N: 4
123 - N: 3
- N: 0
ABCD - N: 4
#ce

Share on other sites

There is a clan of mcloud? Well @malkeymcloud there is only a little problem. If the sequence is correct but the user "write" another character there is a error of dimension exceeded for the line:

If \$sString = \$aArray[\$Row][\$Col] Then

i mean:

ABCDD or ABCD2 ( after the first D must be nothing, D or 2 are not a first character of a sequence so sFinal = "" )

ABCD1 ( after D must be nothing BUT 1 is the first of another sequence so sFinal = "1" )

123A ( an alternative of ABCD1, sFinal = "A" )

ABCDA ( an alternative of ABCD1, sFinal = "A" )

ABCD123 ( sFinal = "123" )

etc. many combination with a compatible scheme

Pratically is the same of the previus scenario but with a complete sequence before.

Thanks again EDIT: Maybe this change can work:

If UBound(\$aArray) <> \$Row And \$sString = \$aArray[\$Row][\$Col] Then

Did you confirm the correction?

Edited by johnmcloud

Share on other sites

Well bro, allowing for six possibilities appears to work.

Global \$aArray2D = [[1, "A"],[2, "B"],[3, "C"],["", "D"]]
Global \$sFinal

;#cs
\$sFinal = ""
_Sequence("A", \$aArray2D)
_Sequence("B", \$aArray2D)
_Sequence("C", \$aArray2D)
_Sequence("D", \$aArray2D)
_Sequence("D", \$aArray2D)
MsgBox(0, "", "Sequence:-" & @LF & 'ABCDD > ""' & @LF & @LF & "Result:-" & @LF & '"' & \$sFinal & '" - N: ' & StringLen(\$sFinal))

\$sFinal = "" ; reset
_Sequence("1", \$aArray2D)
_Sequence("2", \$aArray2D)
_Sequence("D", \$aArray2D)
MsgBox(0, "", "Sequence:-" & @LF & '12D > ""' & @LF & @LF & "Result:-" & @LF & '"' & \$sFinal & '" - N: ' & StringLen(\$sFinal))

\$sFinal = "" ; resetABCD2
_Sequence("A", \$aArray2D)
_Sequence("B", \$aArray2D)
_Sequence("C", \$aArray2D)
_Sequence("D", \$aArray2D)
_Sequence("2", \$aArray2D)
MsgBox(0, "", "Sequence:-" & @LF & 'ABCD2 > """' & @LF & @LF & "Result:-" & @LF & '"' & \$sFinal & '" - N: ' & StringLen(\$sFinal))

\$sFinal = "" ; reset
_Sequence("1", \$aArray2D)
_Sequence("2", \$aArray2D)
_Sequence("3", \$aArray2D)
MsgBox(0, "", "Sequence:-" & @LF & '123 > "123"' & @LF & @LF & "Result:-" & @LF & '"' & \$sFinal & '" - N: ' & StringLen(\$sFinal))

\$sFinal = "" ; reset
_Sequence("A", \$aArray2D)
_Sequence("B", \$aArray2D)
_Sequence("C", \$aArray2D)
_Sequence("D", \$aArray2D)
_Sequence("1", \$aArray2D)
_Sequence("2", \$aArray2D)
_Sequence("3", \$aArray2D)
MsgBox(0, "", "Sequence:-" & @LF & 'ABCD123 > "123"' & @LF & @LF & "Result:-" & @LF & '"' & \$sFinal & '" - N: ' & StringLen(\$sFinal))

\$sFinal = "" ; reset
_Sequence("1", \$aArray2D)
_Sequence("2", \$aArray2D)
_Sequence("3", \$aArray2D)
_Sequence("A", \$aArray2D)
_Sequence("B", \$aArray2D)
_Sequence("C", \$aArray2D)
_Sequence("D", \$aArray2D)
MsgBox(0, "", "Sequence:-" & @LF & '123ABCD > "ABDC"' & @LF & @LF & "Result:-" & @LF & '"' & \$sFinal & '" - N: ' & StringLen(\$sFinal))

\$sFinal = "" ; reset
_Sequence("1", \$aArray2D)
_Sequence("2", \$aArray2D)
_Sequence("3", \$aArray2D)
_Sequence("A", \$aArray2D)
MsgBox(0, "", "Sequence:-" & @LF & '123A > "A"' & @LF & @LF & "Result:-" & @LF & '"' & \$sFinal & '" - N: ' & StringLen(\$sFinal))

\$sFinal = "" ; reset
_Sequence("A", \$aArray2D)
_Sequence("B", \$aArray2D)
_Sequence("C", \$aArray2D)
_Sequence("D", \$aArray2D)
_Sequence("1", \$aArray2D)
MsgBox(0, "", "Sequence:-" & @LF & 'ABCD1 > "1"' & @LF & @LF & "Result:-" & @LF & '"' & \$sFinal & '" - N: ' & StringLen(\$sFinal))

Func _Sequence(\$sString, ByRef \$aArray)
Local Static \$Row, \$Col
If \$sFinal == "" And Not (\$sString == \$aArray Or \$sString == \$aArray) Then Return ;  \$sFinal must start with a "1" or a "A"
If \$sFinal == "" And (\$sString == \$aArray) Then ; If \$sFinal starts with a "1"
\$Row = 0
\$Col = 0
\$sFinal = \$sString
Return
EndIf
If \$sFinal == "" And (\$sString == \$aArray) Then ; If \$sFinal starts with an "A"
\$Row = 0
\$Col = 1
\$sFinal = \$sString
Return
EndIf
If \$Row < UBound(\$aArray) - 1 And \$sString == \$aArray[\$Row + 1][\$Col] Then ; If next element in array equals \$sString then add to \$sFinal.
\$Row += 1
\$sFinal &= \$sString
Return
Else ; Start \$sFinal again with this entered \$sString.
\$sFinal = ""
_Sequence(\$sString, \$aArray)
EndIf
If \$Row >= UBound(\$aArray) - 1 Then ; This \$sString entered passes the end of array sequence so start \$sFinal again.
\$sFinal = ""
_Sequence(\$sString, \$aArray)
EndIf
EndFunc   ;==>_Sequence

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