Jump to content

Variable on For Next loop


Skeletor
 Share

Recommended Posts

Hi Guys,

Is it possible to get a variable on your For..Next loop? 
 

Local $Lines1 = _FileCountLines(C:\temp\test.txt)
Local $linesToCount2 = $Lines1 + 2
$var = Number($linesToCount2)

For $count = 1 To _FileCountLines($FileRead2) Step 1
    For $i = $var To $count
    Next
    ;Code does stuff here
Next

Somehow my code doesn't work even though I thought I could convert the variable to a Integer / Number.

This code I posted above does not move to the next value.

But the code below does... why is that?

For $count = 1 To _FileCountLines($FileRead2) Step 1
    For $i = 2 To $count
    Next
    ;Code does stuff here
Next

 

Why is the For loop resetting itself?
Is it because the program does not cache the variable and needs to keep on acquiring this variable each time?

If so , how would you make this variable static?

 

Edited by Skeletor

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

For $count = 1 To _FileCountLines($FileRead2) Step 1

Made a mistake.. suppose to put the For in the front...updated Original Post.

Edited by Skeletor

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

@Skeletor

#include <File.au3>

Global $intStart = _FileCountLines(@ScriptDir & "\File.csv"), _
       $intTo = 10, _
       $intStep = 1

ConsoleWrite("The For...Next...Step loop will count from " & $intStart & " to " & $intTo & " with the step of " & $intStep & @CRLF)

For $i = $intStart To $intTo Step $intStep
    ConsoleWrite($i & @CRLF)
Next

Content of File.csv:

"SomeValue";"SomeValue";"SomeValue";"SomeValue"
"SomeValue";"SomeValue";"SomeValue";"SomeValue"
"SomeValue";"SomeValue";"SomeValue";"SomeValue"
"SomeValue";"SomeValue";"SomeValue";"SomeValue"
"SomeValue";"SomeValue";"SomeValue";"SomeValue"

What's the issue then?
I am sorry if I'm not undestanding your question :)
 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

@FrancescoDiMuro, thanks for that, yes your code would work, but nest it inside another For Step Next loop.

That's my problem. Example like below... might be vague but hope you get to see what I'm trying here... 
 

Local $Lines1 = _FileCountLines(C:\temp\test.txt)
Local $linesToCount2 = $Lines1 + 2
$var = Number($linesToCount2)

For $count = 1 To _FileCountLines($FileRead2) Step 1
    For $i = $var To $count
    Next
    ;Code does stuff here
Next

 

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

@Skeletor If you used actual numbers you can see why your code won't work for example:

In this example we're assuming test.txt has 5 lines and FileRead2 has 10 lines, you can see how it will fail based on that code, but as FrancescoDiMuro pointed out you need to explain what you're trying to achieve.

Local $Lines1 = 5        ;~ _FileCountLines(C:\temp\test.txt)
Local $linesToCount2 = 7 ;~ $Lines1 + 2
$var = 7                 ;~ Number($linesToCount2) (You don't require Number() function since its already a number)

For $count = 1 To 10     ;~ _FileCountLines($FileRead2) Step 1
    For $i = 7 To 1      ;~ $var To $count (Won't work you need to use Step - 1) However when it reaches 7 it will need to go Step + 1
    Next
    ;Code does stuff here
Next

 

Link to comment
Share on other sites

Thanks @FrancescoDiMuro

The For Step Next loop works great, but only if the nested For Next loop start value is static... 
Somehow I would like to make that start value static. 

Aim:

Loop through file
Insert Data
Get how many lines in the file
Use that to go to the next line
Use For Next loop to go to the last line (mentioned in step above) and start appending the data from another csv file ....

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

Guys, I have trimmed my code below.. 

Here is my aim, 

Read the first CSV file and insert into test.txt... then the next csv file must be read and start after the first one, 

;Read first CSV file... note the For loop has a static value, thats because I forced the starting postition.
For $count = 1 To _FileCountLines($FileRead2) Step 1
    $string = FileReadLine($FileRead2, $count)
    $data = StringSplit($string, ",", 1)
    $value1 = $data[1]
    $value2 = $data[2]


        For $i = 2 To $count
        Next

    FileWrite("C:\tmp\test.txt", $value1 "A" & $i)
    FileWrite("C:\tmp\test.txt", $value2 "B" & $i)

Next

;Next CSV below must start after the csv above
; CSV must start after the above csv... this must be dynamic
Global $Lines3 = _FileCountLines($FileRead2)
Local $linesToCount3 = $Lines1 + 2

For $count = 1 To _FileCountLines($FileRead3) Step 1
    $string = FileReadLine($FileRead3, $count)
    $data = StringSplit($string, ",", 1)
    $value1 = $data[1]
    $value2 = $data[2]


        For $i = $linesToCount3 To $count
        Next

    FileWrite("C:\tmp\test.txt", $value1 "A" & $i)
    FileWrite("C:\tmp\test.txt", $value2 "B" & $i)

Next

 

Edited by Skeletor
Code was wrong, corrected the code

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

Couldn't you just use an array and add the values you want to a new array variable?

#include <Array.au3>
#include <File.au3>

Local $aFileRead1, $sFileName1 = @ScriptDir & "\FileName1.csv"
Local $aFileRead2, $sFileName2 = @ScriptDir & "\FileName2.csv"

_FileReadToArray($sFileName1, $aFileRead1, 0, ",")
_FileReadToArray($sFileName2, $aFileRead2, 0, ",")

_ArrayConcatenate($aFileRead1, $aFileRead2)

_ArrayDisplay($aFileRead1)

 

Link to comment
Share on other sites

@Skeletor
Essentially, you could easily work with arrays, instead of getting line by line your text, and writing it to the "destination file".
Expand as much as you want $arrSourceFiles; the content of all the source files will be written in the "Destination File" :)

#include <File.au3>
#include <FileConstants.au3>

Global $strDestinationFile = @ScriptDir & "\DestinationFile.txt", _
       $arrSourceFiles[] = [@ScriptDir & "\SourceFile1.csv", _
                            @ScriptDir & "\SourceFile2.csv"], _
       $arrFileContent, _
       $hdlDestinationFile


$hdlDestinationFile = FileOpen($strDestinationFile, $FO_APPEND)
If $hdlDestinationFile = -1 Then
    ConsoleWrite("Error while opening the file '" & $strDestinationFile & "'." & @CRLF)
Else
    For $i = 0 To UBound($arrSourceFiles) - 1 Step 1
        _FileReadToArray($arrSourceFiles[$i], $arrFileContent, $FRTA_NOCOUNT)
        If @error Then
            ConsoleWrite("Error while reading the file '" & $arrSourceFiles[$i] & "'. Error: " & @error & @CRLF)
        Else
            ConsoleWrite("The file '" & $arrSourceFiles[$i] & "' has been read correctly." & @CRLF)
            _FileWriteFromArray($hdlDestinationFile, $arrFileContent)
            If @error Then
                ConsoleWrite("Error while writing the data into '" & $strDestinationFile & "'. Error: " & @error & @CRLF)
            Else
                ConsoleWrite("The data read from the file '" & $arrSourceFiles[$i] & "' has been write correctly to '" & $strDestinationFile & "'." & @CRLF)
            EndIf
        EndIf
    Next

    FileClose($hdlDestinationFile)
EndIf

Here it is all the "sample" :)

Sample.zip

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Thanks guys, I was also checking why this did not work.

I tested the For Next loop and from 1 to 3 , I get the loop counting. Meaning 
 

For
    For $i = 1 To $count
    Next
Next
; Each cycle increments by 1 
; starts with 1,2,3,etc...
;////////////////////////////////////////////
For
    For $i = 2 To $count
    Next
 Next
; Each cycle increments by 1 
; starts with 2,3,4,etc...
;////////////////////////////////////////////
For
    For $i = 3 To $count
    Next
Next
; Each cycle increments by 1 
; starts with 3,4,5,etc...
;////////////////////////////////////////////
For
    For $i = 4 To $count
    Next
Next
; Does not increment
; Static is 4 on each cycle
;////////////////////////////////////////////
For
    For $i = 5 To $count
    Next
Next
; Does not increment
; Static is 5 on each cycle
;////////////////////////////////////////////

and the higher you go it does not loop.. Is this a bug?

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

1 hour ago, Skeletor said:

Read the first CSV file and insert into test.txt... then the next csv file must be read and start after the first one, 

;Read first CSV file... note the For loop has a static value, thats because I forced the starting postition.
For $count = 1 To _FileCountLines($FileRead2) Step 1
    $string = FileReadLine($FileRead2, $count)
    $data = StringSplit($string, ",", 1)
    $value1 = $data[1]
    $value2 = $data[2]


        For $i = 2 To $count
        Next

    FileWrite("C:\tmp\test.txt", $value1 "A" & $i)
    FileWrite("C:\tmp\test.txt", $value2 "B" & $i)

Next

;Next CSV below must start after the csv above
; CSV must start after the above csv... this must be dynamic
Global $Lines3 = _FileCountLines($FileRead2)
Local $linesToCount3 = $Lines1 + 2

For $count = 1 To _FileCountLines($FileRead3) Step 1
    $string = FileReadLine($FileRead3, $count)
    $data = StringSplit($string, ",", 1)
    $value1 = $data[1]
    $value2 = $data[2]


        For $i = $linesToCount3 To $count
        Next

    FileWrite("C:\tmp\test.txt", $value1 "A" & $i)
    FileWrite("C:\tmp\test.txt", $value2 "B" & $i)

Next

 

couldn't work , the inner For ... next is skipped as begin is greater end.

Link to comment
Share on other sites

2 hours ago, FrancescoDiMuro said:

@Skeletor
Essentially, you could easily work with arrays, instead of getting line by line your text, and writing it to the "destination file".
Expand as much as you want $arrSourceFiles; the content of all the source files will be written in the "Destination File" :)

#include <File.au3>
#include <FileConstants.au3>

Global $strDestinationFile = @ScriptDir & "\DestinationFile.txt", _
       $arrSourceFiles[] = [@ScriptDir & "\SourceFile1.csv", _
                            @ScriptDir & "\SourceFile2.csv"], _
       $arrFileContent, _
       $hdlDestinationFile


$hdlDestinationFile = FileOpen($strDestinationFile, $FO_APPEND)
If $hdlDestinationFile = -1 Then
    ConsoleWrite("Error while opening the file '" & $strDestinationFile & "'." & @CRLF)
Else
    For $i = 0 To UBound($arrSourceFiles) - 1 Step 1
        _FileReadToArray($arrSourceFiles[$i], $arrFileContent, $FRTA_NOCOUNT)
        If @error Then
            ConsoleWrite("Error while reading the file '" & $arrSourceFiles[$i] & "'. Error: " & @error & @CRLF)
        Else
            ConsoleWrite("The file '" & $arrSourceFiles[$i] & "' has been read correctly." & @CRLF)
            _FileWriteFromArray($hdlDestinationFile, $arrFileContent)
            If @error Then
                ConsoleWrite("Error while writing the data into '" & $strDestinationFile & "'. Error: " & @error & @CRLF)
            Else
                ConsoleWrite("The data read from the file '" & $arrSourceFiles[$i] & "' has been write correctly to '" & $strDestinationFile & "'." & @CRLF)
            EndIf
        EndIf
    Next

    FileClose($hdlDestinationFile)
EndIf

Here it is all the "sample" :)

Sample.zip

That might be fine to use for a straight copy... however, i'm manipulating the data.. "rearranging it"...Then I would need to insert it into another file... 

 

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

No you just insert data into the array then afterwards you'd just write the entire array to file.

It would be simpler if you show an example of the two files and then what you expect it to look like afterwards

You don't have to use real data just need to understand what you're trying to achieve.

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

×
×
  • Create New...