Jump to content

A complex FOR loop? possible?


sfresher
 Share

Recommended Posts

I was trying to write a complex FOR loop, like this: For a 2D array, from 1st to last column, but skip for item array[1][n] if the value is "NA".

For ($n = 1 to $array[0][1]) & ($array[1][n] <> "NA")

I got a compling error: missing seperater character after keyword.

Note: array is a 2D array, so $array[0][1] returns its number of max columns.

Link to comment
Share on other sites

  • Moderators

I was trying to write a complex FOR loop, like this: For a 2D array, from 1st to last column, but skip for item array[1][n] if the value is "NA".

For ($n = 1 to $array[0][1]) & ($array[1][n] <> "NA")

I got a compling error: missing seperater character after keyword.

Note: array is a 2D array, so $array[0][1] returns its number of max columns.

Provide a real example. What you posted is no where near what the help file shows how to do a For/Next loop.

Parameters

variable The variable used for the count.

start The initial numeric value of the variable.

stop The final numeric value of the variable.

stepval [optional] The numeric value (possibly fractional) that the count is increased by each loop. Default is 1.

Note those parameters require an integer value.

Also note, that variables here are declared with $ (dollar signs), and please understand, it's unlike C anything where you nest the statement on one line.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I came with an easier way to describle my problem:

Let's say the array is $array = [true, true, false, true, true]

For $n = 1 to 5

If $array[$n] = true, continue the loop. If all values are true, skip the next step.

If $array[$n] = false, exist the loop immediately. Go to next step.

Edited by sfresher
Link to comment
Share on other sites

  • Developers

Something like this?

Dim $array[5] = [True, True, False, True, True]

For $n = 0 To 4
    If $array[$n] = False Then ExitLoop
Next

If $n = 5 Then 
; All are True
    Exit
Else
    MsgBox(262144,'test',"At least one is False")
EndIf

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

Something like this?

Dim $array[5] = [True, True, False, True, True]

For $n = 0 To 4
    If $array[$n] = False Then ExitLoop
Next

If $n = 5 Then 
; All are True
    Exit
Else
    MsgBox(262144,'test',"At least one is False")
EndIf
Well it works, but I'm sure my Grandmother told me never to use a loop variable outside of the loop.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Developers

Well it works, but I'm sure my Grandmother told me never to use a loop variable outside of the loop.

I never had that "Grandmother" training, but why would that be an issue? (I am not really a programmer in real life so that could be the reason too I have never heard it :) )

I thought it was quit common that the loop variable is set to the last used value in the loop and when the for-next loop went "all the way" its set to the max value +1.

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

I came with an easier way to describle my problem:

Let's say the array is $array = [true, true, false, true, true]

For $n = 1 to 5

If $array[$n] = true, continue the loop. If all values are true, skip the next step.

If $array[$n] = false, exist the loop immediately. Go to next step.

Another example based on Jos' code.

Dim $array[5] = [True, True, False, True, True], $cTrue

For $n = 0 To UBound($array, 1) - 1
    If $array[$n] == False Then
        ExitLoop
    ElseIf $array[$n] == True Then
        $cTrue = $cTrue + 1
        ; more steps go here
    EndIf
Next

MsgBox(262144, 'test', "Counted " & $cTrue & ' "True" before "False" was encountered.')
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...