Jump to content

Help with loop


Recommended Posts

hello, im trying to read a text file, read every line and add the same text aftereach line

here's  my simple code

#include <File.au3>
$file = @ScriptDir & "\name.txt"
local $array
 _FileReadToArray ($file,$array)
For $x = 1 To $array[0]
    $read = FileReadLine ($file,$x)
    if StringInStr ($read,$read) Then
    _FileWriteToLine($file,$x,$read & ":" & $aaa)
EndIf
    Next

 

i want this text file

123

abc

xyz

 

to be 

123:123

abc:abc

xyz:xyz

 

but instead im getting this

123:123

123:123

123:123

any help ? ty & sorry for my bad english

Link to comment
Share on other sites

Link to comment
Share on other sites

Add the $bOverWrite  parameter to your _FileWriteToLine and set it to True, otherwise it just moves all the lines down one.

Also, your If statement is not needed, $read will always include itself in the search, so it's pointless to check to see if it includes itself in the string.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Its working perfectly thank you ! :)

just one more thing

i have another code & im having some troubles with

my other code is trying to find a specific line & check if it contains a text on it it gonna copie it in a another text file 

the text im looking for is points = 400 or more 

401, 402

but the code is so slow and showing random things

 

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


Local $List = @DesktopDir & "\xxxx.txt"
Local $Result = @DesktopDir & "\RESULT.txt"
FileDelete ($Result)
Sleep (500)
Local $Text
_FileReadToArray($List, $Text)

$Points = "points = "
$plus = 400

For $i = 1 To $Text[0]
$plus = 400
Do
    $plus = $plus +1
  
     If StringInStr($Text[$i],$Points & $plus)  Then

        FileWrite ($Result,$Text[$i]& " " & $i & "." & @CRLF)

    EndIf
Until $plus = 10000
Next

 

Link to comment
Share on other sites

If I understand you correctly you need to find all rows that have points between 400 and 10000 ? But points must be at some specific places.  So you need to grab the location of  the points you are looking for.  If you want more helps, just upload the file you are struggling with, then it will be much easier for us to find solution for you.  Cause currently it is just a guessing game.

Link to comment
Share on other sites

24 minutes ago, Nine said:

If I understand you correctly you need to find all rows that have points between 400 and 10000 ? But points must be at some specific places.  So you need to grab the location of  the points you are looking for.  If you want more helps, just upload the file you are struggling with, then it will be much easier for us to find solution for you.  Cause currently it is just a guessing game.

Hello, thanks for the reply

i want the script to extract the lines of ppl whos having 400 points or more & copy them in the result.txt file

 

RESULT.txt script.au3 xxxx.txt

Link to comment
Share on other sites

Try this :

#include <File.au3>

Local $List = @ScriptDir & "\xxxx.txt"
Local $Result = @ScriptDir & "\RESULT.txt"
FileDelete ($Result)
Sleep (500)
Local $Text
_FileReadToArray($List, $Text)

Local $Points = "points = ", $plus = 400, $aArray

For $i = 1 To $Text[0]
  $aArray = StringSplit ($Text[$i], $Points, $STR_ENTIRESPLIT+$STR_NOCOUNT)
  If Int ($aArray[1]) >= $plus Then FileWriteLine ($Result, $aArray[0] & $Points & $aArray[1])
Next

 

Link to comment
Share on other sites

8 minutes ago, Nine said:

Try this :

#include <File.au3>

Local $List = @ScriptDir & "\xxxx.txt"
Local $Result = @ScriptDir & "\RESULT.txt"
FileDelete ($Result)
Sleep (500)
Local $Text
_FileReadToArray($List, $Text)

Local $Points = "points = ", $plus = 400, $aArray

For $i = 1 To $Text[0]
  $aArray = StringSplit ($Text[$i], $Points, $STR_ENTIRESPLIT+$STR_NOCOUNT)
  If Int ($aArray[1]) >= $plus Then FileWriteLine ($Result, $aArray[0] & $Points & $aArray[1])
Next

 

you nailed it ! 

Thank you ♥  :=)

Link to comment
Share on other sites

if you want even faster result (in case of a large file), use handle of file :

#include <File.au3>

Local $List = @ScriptDir & "\xxxx.txt"
Local $Result = @ScriptDir & "\RESULT.txt"
Local $Text
_FileReadToArray($List, $Text)

Local $Points = "points = ", $plus = 400, $aArray
Local $hFile = FileOpen ($Result, $FO_OVERWRITE)

For $i = 1 To $Text[0]
  $aArray = StringSplit ($Text[$i], $Points, $STR_ENTIRESPLIT+$STR_NOCOUNT)
  If Int ($aArray[1]) >= $plus Then FileWriteLine ($hFile, $aArray[0] & $Points & $aArray[1])
Next

FileClose ($hFile)

 

Link to comment
Share on other sites

1 minute ago, Nine said:

if you want even faster result (in case of a large file), use handle of file :

#include <File.au3>

Local $List = @ScriptDir & "\xxxx.txt"
Local $Result = @ScriptDir & "\RESULT.txt"
Local $Text
_FileReadToArray($List, $Text)

Local $Points = "points = ", $plus = 400, $aArray
Local $hFile = FileOpen ($Result, $FO_OVERWRITE)

For $i = 1 To $Text[0]
  $aArray = StringSplit ($Text[$i], $Points, $STR_ENTIRESPLIT+$STR_NOCOUNT)
  If Int ($aArray[1]) >= $plus Then FileWriteLine ($hFile, $aArray[0] & $Points & $aArray[1])
Next

FileClose ($hFile)

 

That was way faster thank you very much !!

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