Jump to content

Replace multiple if statements with for statements


aeun01
 Share

Go to solution Solved by RTFC,

Recommended Posts

I am making a script to find out where a value in Array C is in Array A. It worked,

but nested if statements are too long.

Can this be represented simply by using for statement and putting a variable inside the array c[] with the following rules?

#include <Array.au3>
#include <MsgBoxConstants.au3>

Local $a[]=[33,5,3,4,4,'a4',2,22,66,234,'a4',234,31,34,55,'a4',22,44]
Local $c[]=['a4',22,44]


For $b=0 To 17

    If $c[0] == $a[$b] Then

        If $c[1] == $a[$b+1] Then

            If $c[2] == $a[$b+2] Then

                $k=$b

            EndIf

        EndIf

    EndIf

Next

MsgBox($MB_SYSTEMMODAL, "", $k &"th value" )

 

Edited by aeun01
Please don't copy&paste formating of translated text and post code in a codebox.
Link to comment
Share on other sites

  • aeun01 changed the title to Replace multiple if statements with for statements

Something like the following code?

#include <Array.au3>
#include <MsgBoxConstants.au3>

Local $a[]=[33,5,3,4,4,'a4',2,22,66,234,'a4',234,31,34,55,'a4',22,44]
Local $c[]=['a4',22,44]

For $ic=0 To Ubound($c) - 1
    For $ia=0 To UBound($a) - 1
        If $c[$ic]==$a[$ia] Then
            MsgBox($MB_SYSTEMMODAL, "", "value " & $c[$ic] & " found in $a at index " & $ia)
        EndIf
    Next
Next

 

Link to comment
Share on other sites

  • Solution
#include <MsgBoxConstants.au3>

Local $a[]=[33,5,3,4,4,'a4',2,22,66,234,'a4',234,31,34,55,'a4',22,44]
Local $c[]=['a4',22,44]

Local $lastrow=UBound($a)-1
Local $lastcol=UBound($c)-1

Local $d=$c[0] & "/"
For $cc=1 To $lastcol
    $d&= $c[$cc] & "/"  ; this ensures 1/23 and 12/3 are not confused
Next

Local $e
For $rc=0 To $lastrow-$lastcol  ; no need to search beyond what would fit
    $e=$a[$rc] & "/"
    For $cc=1 To $lastcol
        $e&=$a[$rc+$cc] & "/"
    Next

    ; NB performing a STRING comparison on values (is this intended?)
    If $d==$e Then MsgBox($MB_SYSTEMMODAL, "", $rc &"th value (base-0)" )
Next

A different way:

#include <Array.au3>
#include <MsgBoxConstants.au3>

Local $a[]=[33,5,3,4,4,'a4',2,22,66,234,'a4',234,31,34,55,'a4',22,44]
Local $c[]=['a4',22,44]

Local $aStr=_ArrayToString($a) & "|"
Local $cStr=_ArrayToString($c) & "|"
Local $found=StringInStr($aStr,$cStr)

if $found>0 Then
    Local $pos=0
    For $b=1 To $found-1
        $pos+=(StringMid($aStr,$b,1)="|")
    Next
    MsgBox($MB_SYSTEMMODAL, "", $pos &"th value (base-0)" )
EndIf

Incorporating Nine's suggestion in the next post, this is shorter still (but does not contain a For-loop as requested by OP):

#include <Array.au3>
#include <MsgBoxConstants.au3>

Local $a[]=[33,5,3,4,4,'a4',2,22,66,234,'a4',234,31,34,55,'a4',22,44]
Local $c[]=['a4',22,44]

Local $aStr=_ArrayToString($a) & "|"
Local $found=StringInStr($aStr,_ArrayToString($c) & "|")
If $found>0 Then StringReplace(StringLeft($aStr,$found-1),"|","|")
MsgBox($MB_SYSTEMMODAL, "", (($found>0)?(@extended & "th value (base-0)"):("not found")))

EDIT: There was an edge-case that wasn't properly handled in all my examples (final substring part was unterminated so could trigger a false-positive), now fixed.

Edited by RTFC
Link to comment
Share on other sites

With no loop at all :

#include <Array.au3>
#include <Constants.au3>

Local $a[] = [33, 5, 3, 4, 4, 'a4', 2, 22, 66, 234, 'a4', 234, 31, 34, 55, 'a4', 22, 44]
Local $c[] = ['a4', 22, 44]

Local $pos = _ArrayInArray($a, $c)
If Not @error Then MsgBox($MB_SYSTEMMODAL, "", $pos & "th value (base-0)")

Func _ArrayInArray(ByRef $a, ByRef $c)
  Local $aStr = _ArrayToString($a)
  Local $found = StringInStr($aStr, _ArrayToString($c))
  If Not $found Then Return SetError(1, 0, -1)
  StringReplace(StringLeft($aStr, $found - 1), "|", "|", 0, $STR_CASESENSE)
  Return @extended
EndFunc   ;==>_ArrayInArray

:)

Edit : increase speed with removing case sensitivity

Edited by Nine
Change function name
Link to comment
Share on other sites

My 2 cents   My 0.5 cents ... 😊

Local $a[] = ['a4', 22, 44, 33, 5, 3, 4, 4, 'a4', 22, 44, 66, 234, 'a4', 234, 31, 34, 55, 'a4', 22, 44]
Local $c[] = ['a4', 22, 44]
Local $bMatch

For $ia = 0 To UBound($a) - UBound($c)
    $bMatch = 0
    For $ic = 0 To UBound($c) - 1
        $bMatch += ($a[$ia + $ic] == $c[$ic])
    Next

    If $bMatch = UBound($c) Then ConsoleWrite("sequence found at index " & $ia & @CRLF)
Next

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

  • Developers
On 11/5/2022 at 5:26 AM, aeun01 said:

Please don't copy&paste formating of translated text and post code in a codebox.

Guess you missed this from your first post? 
Please use the default formatting of the forum when posting text!

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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...