 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Sat Sep 12, 2009 12:40 pm Post subject: |
|
|
| yes it does .. it gives a message box displaying 8 and i guess the loop breaks then |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Sep 12, 2009 1:12 pm Post subject: |
|
|
okay this one works .. i just had to shift x-- one line up ..
why TF_tail displays 9 ... when there aer total 11 lines ? |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Sep 12, 2009 1:12 pm Post subject: |
|
|
okay this one works .. i just had to shift x-- one line up ..
why TF_tail displays 9 ... when there aer total 11 lines ?
| Code: | test=
(
1
2
3
4
5
6
7
8
9
)
FileDelete, MyTest.txt
FileAppend, %test%, MyTest.txt
F = MyTest.txt
loop
{
x := TF_Tail(F,1)
msgbox % x "." ; displays x and . on a newline ... why it shows 9?
StringTrimRight, x, x, 1 ; remove trailing `n
x--
var := TF_ReadLines(F,x,x)
if ( var != "")
{
msgbox % var
break
}
}
Esc::ExitApp |
|
|
| Back to top |
|
 |
Guest
|
Posted: Sat Sep 12, 2009 1:14 pm Post subject: |
|
|
| okay i m sorry ..o m g .. sorry .. and i got it .. it shows the content on the line 11 that is the last line okay |
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 4511 Location: Belgrade
|
Posted: Sat Sep 12, 2009 1:31 pm Post subject: |
|
|
I don't know why did you limit this library by forcing text to reside in file.
It would be great if you change it to accept Text instead of FileName.
Afterall, its easy to read file urself and feed the text into, and oposite requires changes to the functions. You could add helper function for file reading:
| Code: | res := TF_ReadLines("line1`line2`line3", ....)
res := TF_ReadLines(TF("TextFile.txt"), ....) |
Reference could be used also for perforamance benefits on large files.
| Code: |
TF_ReadLines(text, ....)
TF("TextFile.txt", t)
TF_ReadLines(t ... )
|
You can even use both:
| Code: | TF(TextFile, ByRef t="") {
FileRead, t, %TextFile%
if IsOmitted(t)
return t
}
TF_ReadLines(text, ....)
TF_ReadLines(TF("TextFile.txt") ... )
TF("TextFile.txt", t)
TF_ReadLines(t ... )
|
IsOmitted is some mechanism to detect if var is omited (or just at default value). While we wait for official way to have it, you can use some of the workarounds on the forum. _________________
 |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Sep 12, 2009 1:41 pm Post subject: |
|
|
okay what is this not working
| Code: |
test=
(
1
2
3
4
5
6
7
8
9
)
FileDelete, MyTest.txt
FileAppend, %test%, MyTest.txt
F = MyTest.txt
x := TF_Tail(F,1)
StringTrimRight, x, x, 1 ; remove trailing `n
loop
{
msgbox % x "." ; displays x and . on a newline .only the frist time .
x--
var := TF_ReadLines(F,x,x)
msgbox % var
if ( var = "`n")
continue
else
{
msgbox %var%
break
}
}
Esc::ExitApp |
|
|
| Back to top |
|
 |
Guest
|
Posted: Sat Sep 12, 2009 1:58 pm Post subject: |
|
|
FINALLY !!!!! .is ther any other way to it..
| Code: |
test=
(
1
2
3
4
5
6
7
8
9
)
FileDelete, MyTest.txt
FileAppend, %test%, MyTest.txt
F = MyTest.txt
x := TF_CountLines(F)
msgbox % x
loop
{
msgbox % x "." ; displays x and . on a newline .only the frist time .
x--
var := TF_ReadLines(F,x,x)
msgbox % var
if ( var = "`n")
continue
else
{
msgbox %var%
break
}
}
Esc::ExitApp |
|
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Sat Sep 12, 2009 2:21 pm Post subject: |
|
|
@guest | Code: | ; i want to read line the last line and then check if line above
; it is blank , if yes ,then check the line above it and if its
; blank then check the line above it and so on untill it finds a
; non blank line.
test=
(
1 this is a line of text
2 this is a line of text
3 this is a line of text
4 this is a line of text
5 this is a line of text
6 this is a line of text
7 this is a line of text
8 this is a line of text and this is the line I want to get
9 this is the last line of text in the file
)
FileDelete, MyTest.txt
FileAppend, %test%, MyTest.txt
F = MyTest.txt
TF_RemoveBlankLines("!" . F) ; does what the name suggests
FileReadLine, GetTheLineYouWant, %F%, % TF_CountLines(F) - 1 ; uses countlines to get the number of lines and substract 1
MsgBox % GetTheLineYouWant |
_________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Last edited by SoLong&Thx4AllTheFish on Sat Sep 12, 2009 3:02 pm; edited 1 time in total |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Sat Sep 12, 2009 2:29 pm Post subject: |
|
|
| majkinetor wrote: | | I don't know why did you limit this library by forcing text to reside in file. | Because my focus is on ... textfiles I've been using the utilities as outlined in the DOC page for years before AHK even existed and use(d) batch files for the most part. If ain't broke don't fix it, and I love those unix cli utils, they do one thing and they do it extremely well and you can glue them together to do almost anything.
Thanks for the feedback I'll study it closely. I'm not a programmer as you may have noticed, I'm more like an if then else guy.
Edit: and I like the fact TF reads and (over)writes for me as I have to do multiple things in succession and speed is not really an issue for me. _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
ribbet.1
Joined: 20 Feb 2007 Posts: 197 Location: D.C.
|
Posted: Sat Sep 12, 2009 4:02 pm Post subject: |
|
|
| Here's some feedback: You rock! |
|
| Back to top |
|
 |
Carcophan
Joined: 24 Dec 2008 Posts: 1308 Location: :noitacoL
|
Posted: Thu Sep 17, 2009 9:44 pm Post subject: |
|
|
Hi HugoV. I have finally gotten around to playing with the (your) TF library. I have been working with IfInStr loops and fileRead/append ect. I wanted to give your Lib a go, and noticed a slight issue with the documentation. Thank you BTW
Granted, most people are smarter and more experienced than I am, but for newer AHKers, it might help to include information about how to use the TF Lib in a loop.
I searched the forum post, your help file and documentation... the only time the word 'loop' appears is in a bug fix notation.
Reason I mention this, and maybe I am coding wrong (imagine that ) but I cannot get two or three different TF_ commands to run on a single file.
| Code: |
!z::
FileSelectFile, VarWhichFile, , C:\filepath\Test\, Select a '.TXT' file , *.txt
F = %VarWhichFile%
TF_RemoveBlankLines(F)
;TF_Replace(F, "Word1", "") ;take the word out, by replacing with a space
;TF_Replace(F, "Word2", "")
;TF_Replace(F, "Word3", "")
;TF_Replace(F, "Word4", "")
MsgBox % TF_Countlines(F)
return
|
Any of these lines work by them selves, but if I uncomment two lines at once, only one of the two work. It doesn't appear that it is in order either, because I have had Word2 be replaced, but the RemoveBlankLines wouldn't run as well, or visa versa.
Or am I just this god aweful at AHK?  |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Fri Sep 18, 2009 6:37 am Post subject: |
|
|
@Carcophan. Thanks for trying my lib. Yes I can see how the doc could be improved as well as the examples with a loop and better explanation of the ! parameter (there is only one example of how to use ! in the examples script I just saw) thanks for pointing it out. I'll put it on my TODO list.
Now to your question try this test script below and see if you can understand how it works | Code: | ; Examples of using a loop with TF
; first we create a few unique test files
File=a`ntest`nfile`nlooks`nlike`nthis`n
FileDelete, __File1TFtest.TFTXT
FileDelete, __File2TFtest.TFTXT
FileDelete, __File3TFtest.TFTXT
FileDelete, __File4TFtest.TFTXT
FileAppend, %File%, __File1TFtest.TFTXT
FileAppend, %File%, __File2TFtest.TFTXT
FileAppend, %File%, __File3TFtest.TFTXT
FileAppend, %File%, __File4TFtest.TFTXT
; Now we can use a loop, remember that by Default
; TextFile.txt will become TextFile_copy.txt
; Example 1, loop all files
Loop, *.TFTXT
{
TF_Replace(A_LoopFileName, "this", "that") ; or use A_LoopFileLongPath
}
; now you have 4 new files __File?TFtest_copy.TFTXT
; If you want to run multiple TF Functions on the same file you
; will either have to parse on the new filename e.g __File1TFtest_copy.TFTXT
; but this will result in __File1TFtest_copy_copy.TFTXT etc etc so you
; can use the ! to OVERWRITE the source file like so
TF_Replace("!__File1TFtest.TFTXT", "this", "that") ; you can use "!" . F in your example
; if you look in __File1TFtest.TFTXT you will see that 'this' has been replaces with 'that'
; Now in a Loop
Loop, *.TFTXT
{
TF_Replace("!" . A_LoopFileName, "this", "that") ; or use A_LoopFileLongPath
} |
http://www.autohotkey.net/~hugov/tf-lib.htm says:
| Quote: | ! Prefix: If TextFile starts with a ! (eg: "!c:\sample.txt") it will overwrite the text file, otherwise it will save the new file to a copy of the text file eg: Filename_copy.txt (All, apart from reading functions because there is no output file)
Tip: you can use concatenation to add the !, e.g. "!" . Filename.txt, see the examples in the AHK thread. |
| Code: | ; Now for your example
; Note the ORIGINAL FILE will be overwritten with the "NEW" file
TF_RemoveBlankLines("!" . F)
TF_Replace("!" . F, "Word1", "") ;take the word out, by replacing with a space
TF_Replace("!" . F, "Word2", "")
TF_Replace("!" . F, "Word3", "")
TF_Replace("!" . F, "Word4", "") | In your script above it will only run the LAST TF you include in your script as each new TF will overwrite the file_copy so it does do all the words but just uses the original (e.g. unchanged) input file again and again hence the use of ! _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
Carcophan
Joined: 24 Dec 2008 Posts: 1308 Location: :noitacoL
|
Posted: Fri Sep 18, 2009 6:01 pm Post subject: |
|
|
Thank you, this was very helpfull as well.
| Quote: | | "(eg: "!c:\sample.txt") " |
Before, I was attempting to put the '!' with the 'F' like: 'F = !C:\path' to show that I wanted to overwrite the variable each time, its much clearer now. TF_Replace("!" . F, "word", "`;word")
After I posted my original issue here I played around a little more, and i did happen to notice "it will only run the LAST TF you include in your script " It does infact run the last TF command! Not that you didn't already know that, but learning it on my own before being told was refreshing.
Just a general question, as to how the Lib functions:
Why is there a need to concatinate the '!' to the 'F' with the period in this way, as opposed to 'F = !C:\path'? It makes sense to "!".F only for the files that you would want that done too, but if you wanted every single TF_replace, why is my 'F = !C:\path' logic wrong? It is still overwritten each time through, like Var := %Var%, sort of like assigning the value of a variable to itself? |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Fri Sep 18, 2009 7:12 pm Post subject: |
|
|
No particular need to use concatenation every time, this also works | Code: | File=a`ntest`nfile`nlooks`nlike`nthis`n
FileDelete, __NewFile.TXT
FileAppend, %File%, __NewFile.TXT
FileSelectFile, VarWhichFile, , , Select a '.TXT' file , *.txt
F=!%VarWhichFile%
TF_Replace(F, "this", "that") | I use most of my TFs in loops with the A_LoopFileLongPath so concatenation is useful there.
The use of ! in general is more convenient than adding a parameter for overwrite for example, it is shorter, otherwise it would have been TF_Replace(F, "this", "that", OverWrite) or something and some functions already have quite a few parameters... Might take a bit of getting used to but I like it  _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Sat Sep 19, 2009 3:20 pm Post subject: |
|
|
To allow working with strings also, I suggest following:
| Code: | ; Created by Tuncay *2009-09-19*
; TextFile can be a path to an existing file or a variable holding the data of a file.
; ErrorLevel holds the line count of TextFile.
; Returns 0 if TextFile is leaved as is and 1 if the content of an existing
; file is read into that variable.
TF_LoadTextFile(ByRef TextFile)
{
StringReplace, TextFile, TextFile, `n, `n, UseErrorLevel
If (ErrorLevel = 0 AND FileExist(TextFile))
{
FileRead, TextFile, %TextFile%
StringReplace, TextFile, TextFile, `n, `n, UseErrorLevel
If (TextFile != "")
ErrorLevel++
Return, 1
}
Else
{
If (TextFile != "")
ErrorLevel++
Return, 0
}
}
; Replaced by Tuncay *2009-09-19*.
TF_CountLines(TextFile) ; was TotalLines in v1
{
TF_LoadTextFile(TextFile)
Return ErrorLevel
}
TF_ReadLines(TextFile, StartLine = 1, EndLine = 0)
{
TF_MatchList:=_MakeMatchList_(TextFile, StartLine, EndLine) ; create MatchList
TF_LoadTextFile(TextFile) ;Edited.
Loop, Parse, TextFile, `n, `r ;Edited.
{
If A_Index in %TF_MatchList%
OutPut .= A_LoopField "`n"
Else if (A_Index => EndLine)
Break
}
StringTrimRight, OutPut, OutPut, 1 ;?Added: Trim last new line from loop.
Return OutPut
} |
The TextFile variable is checked if it is a path without new lines, then if it is an existing file, the content will be used as TextFile. That has the advantage that we can use the function with strings also, not only with files on disk.
In a quick review I changed the CountLines to work with strings also. Now it counts correctly (it did not worked correct before I think) and the ReadLines had added a last not needed new line character, which is trimmed by me. The TF_LoadTextFile(ByRef TextFile) can be used at any place where FileRead, Str, %TextFile% is in use.
I did not looked at other functions, they seem to complicated to me to touch them. Hope you adapt this to other functions.
The only problem with this function is, if TextFile should be a string (just after FileRead a textfile) holding just one line of an existing path... that would be loaded. This is very rare, so it is fair enough to use this.
On a sidenote, I am currently not using this set of functions (may be in future I would). So I am not sure currently about the dependency of functions and how far the changed would break it. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|