Jump to content

Add detail in textfile


Recommended Posts

hi guys...

can some one help me to doing this job,

currently i have a bunch of file about 1~2 KB containing list of words, says...

aaa
aaerially
aam
aarogramme
aaron
aaronic
aarp
...

i want change it to...

int aaa = 3;
int aaerially = 9;
int aam = 3;
int aarogramme = 10;
int aaron = 5;
int aaronic = 7;
int aarp = 4;
...

so far i have a

$file = FileOpen("words.txt", 0)
$filer = FileOpen("test.txt", 1)

For $i = 1 To 192718 ;192718 is number of line
    $str = FileReadLine($file, $i)
    $len = StringLen($str)
    If $len <= 16 Then
        FileWriteLine($filer, "int "&$str&" = "&$len&';')
    EndIf
Next

FileClose($file)
FileClose($filer)

but it take much time to finish one file,

can anyone suggest me better solution,

Link to comment
Share on other sites

There's no reason to hard code the number of lines. Just loop on FileReadLine() until you get an error (EOF):

$sInFile = @ScriptDir & "\words.txt"
$sOutFile = @ScriptDir & "\test.txt"
$hInFile = FileOpen($sInFile, 0)
$hOutFile = FileOpen($sOutFile, 1)

While 1
    $str = FileReadLine($hInFile)
    If @error Then ExitLoop
    $len = StringLen($str)
    If $len <= 16 Then
        FileWriteLine($hOutFile, "int " & $str & " = " & $len & ';')
    EndIf
WEnd

FileClose($hInFile)
FileClose($hOutFile)

You'll have to try it to see how much not resetting the pointer every time will help execution speed.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...