Jump to content

Simple question: Looping through non incrementing vars


Recommended Posts

I know there are a ton of ways to tackle this, but I know I always end up doing things the hard way.  So how would YOU do this:

$this = "whatever.pdf"
$that = "sure.xls"

For [each of the above]
   ...
Next

How I would probably end up doing it - because I don't know any better - is something like this

$this = "whatever.pdf"
$that = "sure.xls"

Dim $a[2]=[$this, $that]

For $i = 0 to Ubound( $a ) -1
   ...
Next

But that seems... clunky.

Probably (as always) there's some basic thing I'm missing :)

Thanks.

Link to comment
Share on other sites

  • Moderators

I'm really unsure what you're trying to accomplish.

Global $aFiles[] = ["whatever.pdf","sure.xls"]
For $i = 0 To UBound($aFiles) - 1
    ;...
Next

 

Is just as efficient.

But if you're looping through them, without using a base variable (where you could use Assign and Eval), not sure how you'd expect to accomplish it without an array.

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

Well, I specifically didn't want to create an array of the values... the variables already exist from earlier code.  I realize your way is how to do it if I'm starting with the values.

But that's cool.  I figured - as is the case so many times I find myself doing it "my way" - that there was probably some method I wasn't aware of.

In psudo-code, something like:

$this = "whatever.pdf"
$that = "sure.xls"

For Each $var In ($this, $that)
  ...
Next

Of course, it almost looks silly with this small amount of data :)  I have a reason when I look at my actual code  LOL

No biggie...  I can manage from here.  Thanks.

Link to comment
Share on other sites

  • Moderators

If it were a large amount of data, I'd have said to write a script to parse through them and change to array vars ;)...

Edit:

Or create an array and just use _arrayadd or your own pseudo code to add a bunch of values... maybe something like:

$this = "whatever.pdf"
$that = "sure.xls"

$aLoop = _mySillyArrayFunc($this, $that)
For $i = 0 To @extended - 1
  ConsoleWrite($aLoop[$i] & @CRLF)
Next


Func _mySillyArrayFunc($v1,$v2=0,$v3=0,$v4=0,$v5=0,$v6=0,$v7=0,$v8=0, _
    $v9=0,$v10=0,$v11=0,$v12=0,$v13=0,$v14=0,$v15=0,$v16=0,$v17=0,$v18=0, _
    $v19=0,$v20=0,$v21=0,$v22=0,$v23=0,$v24=0,$v25=0,$v26=0,$v27=0,$v28=0, _
    $v29=0,$v30=0,$v31=0,$v32=0,$v33=0,$v34=0,$v35=0,$v36=0,$v37=0,$v38=0, _
    $v39=0,$v40=0,$v41=0,$v42=0,$v43=0,$v44=0,$v45=0,$v46=0,$v47=0,$v48=0, _
    $v49=0,$v50=0)

    Local $aRet[@NumParams]
    For $i = 0 To @NumParams - 1
        $aRet[$i] = Eval("v" & $i + 1)
    Next

    Return SetError(0, @NumParams, $aRet)
EndFunc
Edited by SmOke_N

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

  • Moderators

So, don't ask me what made me come back to this, I seriously couldn't tell you...

But... you could really get silly with it other than the above:

$this = "whatever.pdf"
$that = "sure.xls"

Global $gaLoop
For $i = 0 To _to($gaLoop, $this, $that)
  ConsoleWrite($gaLoop[$i] & @CRLF)
Next

Func _to(ByRef $aArr, $v1,$v2=0,$v3=0,$v4=0,$v5=0,$v6=0,$v7=0,$v8=0, _
    $v9=0,$v10=0,$v11=0,$v12=0,$v13=0,$v14=0,$v15=0,$v16=0,$v17=0,$v18=0, _
    $v19=0,$v20=0,$v21=0,$v22=0,$v23=0,$v24=0,$v25=0,$v26=0,$v27=0,$v28=0, _
    $v29=0,$v30=0,$v31=0,$v32=0,$v33=0,$v34=0,$v35=0,$v36=0,$v37=0,$v38=0, _
    $v39=0,$v40=0,$v41=0,$v42=0,$v43=0,$v44=0,$v45=0,$v46=0,$v47=0,$v48=0, _
    $v49=0,$v50=0)

    Dim $aArr[@NumParams - 1]

    For $i = 0 To @NumParams - 2
        $aArr[$i] = Eval("v" & $i + 1)
    Next

    Return @NumParams - 2
EndFunc

And yes, even more fatigue:

#include <Array.au3>
; of course this only works if the variables are global

Global $var1 = "whatevs.go"
Global $this = "whatever.pdf"
Global $old = "somefile.txt"
Global $man = "forsure.exe"
Global $told = "dunno.docx"
Global $me = "really.rar"
Global $that = "sure.xls"
Global $misc1 = "hmm.example"

Global $gaLoop
For $i = 0 To _findto($gaLoop, "$this", "$that")
  ConsoleWrite($gaLoop[$i] & @CRLF)
Next

Func _findto(ByRef $aArr, $v1, $v2)

    ; obviously won't work on compiled
    If @Compiled Then Return SetError(1, 0, -1)

    ; read your script file, doesn't include includes obviously
    ;  unless you stripped it
    Local $sFRead = FileRead(@ScriptFullPath)

    ; to really make this work, you'd need to replace quoted strings first
    ;  but this is just proof of concept
    Local $sPat = "(?i)\$(\w+)(?:\s*\=|\s*,|\s*(?:\z|\v))"
    Local $aReg = StringRegExp($sFRead, $sPat, 3)
    Local $aUniq = _ArrayUnique($aReg, 0, 0, 0, 0)

    ; I'm really tired, so I'm going to skip the brain exercise to do the regex
    ;  to pull the data and just sort through what I have
    Local $iCount = 0, $iEnum = 0
    Dim $aArr[UBound($aUniq)]
    $v1 = StringReplace($v1, "$", "", 1)
    $v2 = StringReplace($v2, "$", "", 1)
    While 1
        If $aUniq[$iEnum] = $v1 Then
            For $i = $iEnum To UBound($aUniq) - 1
                $aArr[$iCount] = Eval($aUniq[$i])
                $iCount += 1
                If $aUniq[$i] = $v2 Then ExitLoop 2
            Next
        EndIf
        $iEnum += 1
    WEnd

    If Not $iCount Then Return SetError(2, 0, -1)

    ReDim $aArr[$iCount]

    Return UBound($aArr) - 1
EndFunc

Ok, I'm done... poof

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

With a standard naming convention, something like this:

$this1 = "something"
$this2 = "else"
$i = 1
While Eval("this" & $i)
    ConsoleWrite(Eval("this" & $i) & @CRLF)
    $i+=1
WEnd
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

I'm not really sure I'd call that "standard".  I mean, if the data is all related, for example:

$Customer1 = "John"

$Customer2 = "Pete"

$Customer3 = "Mary"

Then sure, that makes sense but then I'd think you'd really be better off with

$Customer[3]=["John", "Pete", "Mary"]

If, as is the case with my data, the elements are NOT related, for example:

$customer = "john"

$day = "tuesday"

$color = "blue"

Then an array doesn't make sense and NEITHER would naming the variables:

$standard1 = "john"

$standard2 = "tuesday"

$standard3 = "blue"

That would totally kill readability!

 

Granted, there aren't many instances where you would be performing a loop on completely unrelated data.  But I had my reason :)  And I was just seeing if there was a better way to loop through them vs. creating a new array just to stuff it with existing variables so I can loop through it.

it doesn't exist.  I'm fine with that answer.   

Link to comment
Share on other sites

  • Moderators

 

With a standard naming convention, something like this:

$this1 = "something"
$this2 = "else"
$i = 1
While Eval("this" & $i)
    ConsoleWrite(Eval("this" & $i) & @CRLF)
    $i+=1
WEnd

That was the point I was making in post #2, and took it to the ridiculous in the others :)

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

Well, obviously use the array then...or a 2d array...I'm just throwing out some curve balls on how to deal with this kind of thing.

And come on, you have to think outside of the box!.  Like this:

$customer1 = "first"
$customer2 = "second"
$customerPhone1 = "5555555555"
$customerPhone2 = "5555555556"

$i = 1
While Eval("customer" & $i)
    ConsoleWrite(Eval("customer" & $i) & " " & Eval("customerPhone" & $i) & @CRLF)
    $i+=1
WEnd

output:

first 5555555555
second 5555555556

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Well, if it's an au3 file, you can read the script and grab ALL variables (a regexp), and then eval each one...but then you would also need to logically read in all the includes as well...you seem to have no rhyme or reason so this could work.

Make life easy, group things together in arrays.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • Moderators

Well, if it's an au3 file, you can read the script and grab ALL variables (a regexp), and then eval each one...but then you would also need to logically read in all the includes as well...you seem to have no rhyme or reason so this could work.

Make life easy, group things together in arrays.

again... 

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

you seem to have no rhyme or reason

I asked a SIMPLE question and already received a simple answer (that, no, there is nothing I missed in the language).  As I said repeatedly, we can let it go.  I have no rhyme, no reason... and no problem that needs solving.  It was just a question.

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