Jump to content
Sign in to follow this  
Skeletor

Variable on For Next loop

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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
Share on other sites
14 minutes ago, Skeletor said:

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

Why do you need to do this? :)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
Share on other sites

@Skeletor
Mh... Let's try another way :)

What is the goal of your script?
Maybe this approach is not the best for your case...
 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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

 

Share this post


Link to post
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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
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)

 

Share this post


Link to post
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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
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.

Share this post


Link to post
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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
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.

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ssah8
      So im creating a bot that logs in on a website. The website got a dropdown menu, and I have to choose the right one to login (Student)
      #include <IE.au3>
      #inculude <Inet.au3>
      $oIE = _IECreate("<snip>")
      $Name = _IEGetObjByName($oIE, "ssusername")
      $Pass = _IEGetObjByName($oIE, "sspassword")
      $dropdown = _IEGetObjByName($oIE, "usertype")
      _IEPropertySet($Name, 'innerText', 'censored')
      _IEPropertySet($Pass, 'innerText', 'censored')
      _IEPropertySet($dropdown, 'innerText', 'Student') <------ How do I make the dropdown list select "Student"?
       
       
    • By adjist
      Hello all! 
       
      Getting this error :
      (22) : ==> Variable used without being declared.: if $vNumber = 0 Then if ^ ERROR  
      But I'm sure I have defined the variable, as in the top of my script has 
      Global $vNumber = 0  
      How would I go about fixing this?
       
    • By D2thunder
      Hi all, I wasnt active in this Forum forum for Years. 😅
      Since I need something with Json in AutoIt i write a little UDF to save a variable to file to read it with another language - and back. After a while until my other script grows i thought - lets integrate all variables wich is possible, because i want have arrays to.
      This is my first public UDF in Autoit - normaly my AutoIt scripts are quick an dirty.... - like the examples for this udf. - someone want to write nice examples?
      I tried to write this UDF properly, cared for processing speed, low mem consumption as possible, a short code, and a high usability without beeing very familiar with arrays.
      What is it for? - convert nearly all types of variables to a json string for saving it in a file or to an ini. Read it back in to a same formated variable and preserv variable types. All array types are supported with nested arrays, the only restriction is ram and array/stringsize - and time if the array is very big. You can use Filters for variable types and some other options.
      If anyone found a bug or any suggestions, please post it.
       
      get Json.au3 from here Json.au3
       
       
      JsonVar 2019.01.25.1.zip
    • By Pickpocketz88
      First I would just like to say HELLO! to anyone reading. It has been a while since I've posted to the Forums but I'm always crawling around.
      Now to the matter at hand. I have been looking high and low for a simplistic answer my burned out brain can find but to no avail. I've only recently upped my AutoIt skill and only by a little bit such as ordering my script neatly with my own UDFs and using Global/Dim more often to make my GUI creation understandable and easy to keep things orderly. My current problem however is figuring out how to make my newest endeavor work which is my own "Debugger". I've made a GUI with an Edit Control to display what my Variables are holding and other information from a concurrently running Script. I have access to all of the scripts I'm attempting to connect but I'm dumbfounded on how I would separately read variable information from one running script into another. I'll provide my "Debugger" script that I want to read variables into and a "Meta Script" I'd want to pass info from.
      #Region Include Files #include <AutoItConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <ColorConstantS.au3> #EndRegion #Region AutoIt Options ;Opt("TrayAutoPause", 0) ;Opt("TrayMenuMode", 3) #EndRegion #Region Hotkeys HotKeySet("{ESC}", "ExitProgram") HotKeySet("{PGDN}", "PauseProgram") HotKeySet("!1", "Snippet_1") HotKeySet("!2", "Snippet_2") HotKeySet("!3", "Snippet_3") HotKeySet("!4", "Snippet_4") HotKeySet("!5", "Snippet_5") HotKeySet("!6", "Snippet_6") HotKeySet("!7", "Snippet_7") HotKeySet("!8", "Snippet_8") HotKeySet("!9", "Snippet_9") #EndRegion #Region Global Variables #Region Globals Global $gMain, $ctrlEdit ;, $gParent #EndRegion #Region $gMain Params Dim $gMainW = @DesktopWidth / 2 Dim $gMainH = @DesktopHeight / 2 Dim $gMainX = (@DesktopWidth / 2) - ($gMainW / 2) Dim $gMainY = (@DesktopHeight / 2) - ($gMainH / 2) Dim $gMainStyle = $WS_POPUP Dim $gMainStyleEx = -1 ;Dim $gMainParent = $gParent #EndRegion #Region $ctrlEdit Params Dim $ctrlEditW = Round($gMainW * 0.98) Dim $ctrlEditH = Round($gMainH * 0.98) Dim $ctrlEditX = ($gMainW - $ctrlEditW) / 2 Dim $ctrlEditY = ($gMainH - $ctrlEditH) / 2 Dim $ctrlEditStyle = -1 Dim $ctrlEditStyleEx = -1 #EndRegion #EndRegion #Region GUI Initialization ;$gParent = GUICreate("", -1, -1, -1, -1, -1, $WS_EX_TOOLWINDOW) $gMain = GUICreate("", $gMainW, $gMainH, $gMainX, $gMainY, $gMainStyle, $gMainStyleEx) GUISetBkColor($COLOR_BLACK, $gMain) $ctrlEdit = GUICtrlCreateEdit("MainW: " & $gMainW & @CRLF & "MainH: " & $gMainH & @CRLF & "EditW: " & $ctrlEditW & @CRLF & "EditH: " & $ctrlEditH, $ctrlEditX, $ctrlEditY, $ctrlEditW, $ctrlEditH, $ctrlEditStyle, $ctrlEditStyleEx) GUISetState(@SW_SHOW, $gMain) #EndRegion MainFunction() #Region Main Function (GUI) Func MainFunction() While 1 $msg = GUIGetMsg() If $msg = $GUI_EVENT_CLOSE Then Exit EndIf WEnd EndFunc #EndRegion #Region Functions Func Functions() EndFunc #EndRegion #Region Program 1 Func Snippet_1() EndFunc #EndRegion #Region Program 2 Func Snippet_2() EndFunc #EndRegion #Region Program 3 Func Snippet_3() EndFunc #EndRegion #Region Program 4 Func Snippet_4() EndFunc #EndRegion #Region Program 5 Func Snippet_5() EndFunc #EndRegion #Region Program 6 Func Snippet_6() EndFunc #EndRegion #Region Program 7 Func Snippet_7() EndFunc #EndRegion #Region Program 8 Func Snippet_8() EndFunc #EndRegion #Region Program 9 Func Snippet_9() EndFunc #EndRegion #Region Pause/Exit Functions Func PauseProgram() While 1 Sleep(1000) WEnd EndFunc Func ExitProgram() Exit EndFunc #EndRegion Pause/Exit Functions #Region Snippets #CS #CE #EndRegion #Region Other Information #CS #CE #EndRegion That's the Debugger script. Please forgive anything ignorant but point it out if you will, I'll take any pointers to get better! (I usually use a Select to get $GUI_EVENT_CLOSE but this is early on)
      Now if I made another script with a basic GUI similar to this and wanted to read say the GUI Width ($gMainW) into the Debugger Edit Control could I do it? If so, could I do it for every variable I have in a script? I read something about the Run function and adding the variables as an option parameter I believe which I think I could do with an array to keep it from being super long and ugly but would that be the only way to do this? Any information is going to be appreciated and thank you in advance for your time!
       
      Edit: Sadly it just dawned on me that I could make a UDF that will create a child window that will do this instead of having a separate script trying to invade another... I'll still be keeping an eye on this for any comments but I apologize if I wasted your time!
    • By mlazovjp
      I am going through the AutoIt Wiki and soaking up a lot of great information.  On the “Best Coding Practices” page at https://www.autoitscript.com/wiki/Best_coding_practices#Scopes_of_Variables there seems to be a minor detail missing.  In the “Variable Initialization” section at https://www.autoitscript.com/wiki/Best_coding_practices#Variable_Initialization , there are references to a data type $o, but in the table listing prefixes and their data types, there does not appear to be a prefix “o”.  I assume that this is for Object?
×
×
  • Create New...