Jump to content

Looping Advice


Recommended Posts

So I've had this durdge of probable unecessary extra lines in one of my scripts for a while, mainly due to my inability to wrap my brain around a way to loop a section of code so it cycles variables the way I need, so it doesn't break the other 3000 lines. Let me preface the showing of this code by saying this was one of the first scripts I wrote in AutoIt, so please be kind... I don't make these kinds of mistakes... often. :o If I did a total rewrite of the script, it'd look quite different now.

I do the following, 30 times, with certain variables changing names in each section... When you look at the code, note that the only variables that change have "gaat" in their names. They change as a location code for each site, such as "gane" or "gase", again there's 30 different versions of those variables. I know there must be an easy way to do a loop so I only have to do it once, anyways, here's the code:

; Define variables
Dim $gaat
Dim $filename
Dim $agaatLine

; Read UND Data From GAAT
; Load GAAT data
$filename = FileOpen("C:\undsearch\gaat-und.csv", 0)

; Check if file opened for reading OK
If $filename = -1 Then
    MsgBox(0, "Error", "Unable to open GAAT file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
DIM $acounti[1000]
$counti = 0
DIM $resultcount
$resultcount = -1
While 1
    $counti = $counti + 1
    $gaatline = FileReadLine($filename)
    If @error = -1 Then ExitLoop
    $stringfound = stringInstr($gaatline, $racfid)
    If $stringfound > 0 THEN
        $resultcount = $resultcount+1
        _ArrayInsert( $acounti, $resultcount, $counti)
    EndIf
    $stringfound = 0
Wend
IF IsArray($acounti) THEN 
    IF $acounti[0]>0 THEN
        $gaatline = FileReadLine($filename,$acounti[0])
        $agaatLine1 = StringSplit($gaatline,"|")
    EndIf
    IF $acounti[1]>0 THEN
        $gaatline = FileReadLine($filename,$acounti[1])
        $agaatLine2 = StringSplit($gaatline,"|")
    EndIf
    IF $acounti[2]>0 THEN
        $gaatline = FileReadLine($filename,$acounti[2])
        $agaatLine3 = StringSplit($gaatline,"|")
    EndIf
    IF $acounti[3]>0 THEN
        $gaatline = FileReadLine($filename,$acounti[3])
        $agaatLine4 = StringSplit($gaatline,"|")
    EndIf
    IF $acounti[4]>0 THEN
        $gaatline = FileReadLine($filename,$acounti[4])
        $agaatLine5 = StringSplit($gaatline,"|")
    EndIf
    IF $acounti[5]>0 THEN
        $gaatline = FileReadLine($filename,$acounti[5])
        $agaatLine6 = StringSplit($gaatline,"|")
    EndIf
    IF $acounti[6]>0 THEN
        $gaatline = FileReadLine($filename,$acounti[6])
        $agaatLine7 = StringSplit($gaatline,"|")
    EndIf
    IF $acounti[7]>0 THEN
        $gaatline = FileReadLine($filename,$acounti[7])
        $agaatLine8 = StringSplit($gaatline,"|")
    EndIf
    IF $acounti[8]>0 THEN
        $gaatline = FileReadLine($filename,$acounti[8])
        $agaatLine9 = StringSplit($gaatline,"|")
    EndIf
    IF $acounti[9]>0 THEN
        $gaatline = FileReadLine($filename,$acounti[9])
        $agaatLine10 = StringSplit($gaatline,"|")
    EndIf
EndIf
FileClose($filename)

If anyone has a suggestion for a relatively painless method of looping the affected variables, I'm willing to listen. If I could insert variable names inside of an array for use later, this would be much easier... or can I? I've never seen an example of this, so I'm not sure how you'd go about it.

Thank you for your time!

-Crim

Link to comment
Share on other sites

IF $acounti[0]>0 THEN

$gaatline = FileReadLine($filename,$acounti[0])

$agaatLine1 = StringSplit($gaatline,"|")

EndIf

IF $acounti[1]>0 THEN

$gaatline = FileReadLine($filename,$acounti[1])

$agaatLine2 = StringSplit($gaatline,"|")

EndIf

IF $acounti[2]>0 THEN

$gaatline = FileReadLine($filename,$acounti[2])

$agaatLine3 = StringSplit($gaatline,"|")

EndIf

IF $acounti[3]>0 THEN

$gaatline = FileReadLine($filename,$acounti[3])

$agaatLine4 = StringSplit($gaatline,"|")

EndIf

IF $acounti[4]>0 THEN

$gaatline = FileReadLine($filename,$acounti[4])

$agaatLine5 = StringSplit($gaatline,"|")

EndIf

IF $acounti[5]>0 THEN

$gaatline = FileReadLine($filename,$acounti[5])

$agaatLine6 = StringSplit($gaatline,"|")

EndIf

IF $acounti[6]>0 THEN

$gaatline = FileReadLine($filename,$acounti[6])

$agaatLine7 = StringSplit($gaatline,"|")

EndIf

IF $acounti[7]>0 THEN

$gaatline = FileReadLine($filename,$acounti[7])

$agaatLine8 = StringSplit($gaatline,"|")

EndIf

IF $acounti[8]>0 THEN

$gaatline = FileReadLine($filename,$acounti[8])

$agaatLine9 = StringSplit($gaatline,"|")

EndIf

IF $acounti[9]>0 THEN

$gaatline = FileReadLine($filename,$acounti[9])

$agaatLine10 = StringSplit($gaatline,"|")

EndIf

if i'm not mistaken you could simplify this as follows.. not sure if its exactly what you're looking for but its generally more tidy

try this:

dim $i

dim $agaatline[11]
$agaatline = _ArrayCreate(StringSplit($gaatline,"|"))
select case $acounti
for $i = 1 to 10
     $gaatline = FileReadLine($filename,$acounti[$i])
     $agaatLine[$i] = StringSplit($gaatline,"|")
Next

im not sure about the $agaatLine bit, but the 1st line at least will work i think

Peace

Edited by Sarc
Link to comment
Share on other sites

if i'm not mistaken you could simplify this as follows.. not sure if its exactly what you're looking for but its generally more tidy

try this:

dim $i

dim $agaatline[11]
$agaatline = _ArrayCreate(StringSplit($gaatline,"|"))
select case $acounti
for $i = 1 to 10
     $gaatline = FileReadLine($filename,$acounti[$i])
     $agaatLine[$i] = StringSplit($gaatline,"|")
Next

im not sure about the $agaatLine bit, but the 1st line at least will work i think

Peace

StringSplit creates an array, so passing it to _ArrayCreate() i don't think would work the way that you intend.

this is untested, but it may work for you, although i think that creating an array instead of having 10 different gaat variables would be the way to go...

If IsArray($acounti) Then
    For $iterator = 0 To 9
    If $acounti[$iterator] > 0 Then
        $gaatline = FileReadLine($filename, $acounti[$iterator])
        Assign("$agaatLine" & $iterator, StringSplit($gaatline, "|")
    EndIf
    Next
    FileClose($filename)
EndIf
Link to comment
Share on other sites

Thanks you all, that worked pretty well... I had a few small typos to fix, but the following worked perfectly... and I learned a new function! (Assign)

Here's the final code that worked:

If IsArray($acounti) Then
    For $iterator = 0 To 9 Step 1
        If $acounti[$iterator] > 0 Then
            $gaatline = FileReadLine($filename, $acounti[$iterator])
            Assign("agaatLine" & $iterator, StringSplit($gaatline, "|"))
        EndIf
    Next
    FileClose($filename)
EndIf
Link to comment
Share on other sites

Thanks you all, that worked pretty well... I had a few small typos to fix, but the following worked perfectly... and I learned a new function! (Assign)

Here's the final code that worked:

If IsArray($acounti) Then
    For $iterator = 0 To 9 Step 1
        If $acounti[$iterator] > 0 Then
            $gaatline = FileReadLine($filename, $acounti[$iterator])
            Assign("agaatLine" & $iterator, StringSplit($gaatline, "|"))
        EndIf
    Next
    FileClose($filename)
EndIf
glad to help. so you know, the default step is 1, so adding that is kind of redundant...
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...