AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TF: Text file & Variables/String Library v3.3 [lib]
Goto page Previous  1, 2, 3 ... 8, 9, 10, 11, 12, 13  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Sat Mar 06, 2010 7:24 pm    Post subject: Reply with quote

There are various methods to go about it, one would be to use TF_Find to find the lines and pass these on to TF_RemoveLines like so:
Code:
text=
(join`r`n
this is test
with a number of lines
to show how you
can delete "names" from a
file or variable
)

MsgBox % TF_RemoveLines(Text, TF_Find(text, "", "", "with a number of lines|file or variable", 0))
Note TF_Find uses RegEx so the | means search sentence a OR sentence b

Another method would be to use TF_Replace but you would be left with empty lines unless you use `r`nto show how you`r`n but these fail with the first and lastlines because they don't start or end with `r`n. You could use TF_ReplaceInLines but you are left with empty lines as well OR you could have a look at TF_Substract where lines in list1 are removed in list2.
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
manek
Guest





PostPosted: Sat Mar 06, 2010 7:55 pm    Post subject: Reply with quote

so if i had a variable like this :
LinesToDel = line2|another line|random

TF_RemoveLines("!Text.txt", TF_Find("!Text.txt", "", "", LinesToDel, 0))

this would work? i tried it and it removed everything from the text file

then i tried this :

TF_Substract(LinesToDel, "!Text.txt", "0")

and it didnt remove anything
Back to top
manek
Guest





PostPosted: Sat Mar 06, 2010 8:22 pm    Post subject: Reply with quote

ok never mind
i got it to work with TF_Substract

i was just doing it completely wrong
Back to top
sunfire



Joined: 24 Mar 2010
Posts: 6
Location: Krailling, Germany

PostPosted: Wed Mar 24, 2010 1:02 pm    Post subject: Reply with quote

This is great work. A deep thank-you to hugov! Very Happy

Greetz, sunfire
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Wed Mar 24, 2010 1:15 pm    Post subject: Reply with quote

@sunfire: thats nice to hear. I'm always curious what functions people use and if they have any suggestions ...
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Fri Apr 16, 2010 3:48 pm    Post subject: Reply with quote

Update v3.3, 16 April 2010

* Fixed: If you used variables with any of the replacement functions or tried to remove empty lines from a VARIABLE in the following format: variable:=TF_..(variable,"search","replace") and the searchtext was NOT present in the variable it returned an empty variable (e.g. deleted the contents of "variable")
It affected TF_ReplaceInLines, TF_Replace, TF_RegExReplaceInLines, TF_RegExReplace, TF_RemoveBlankLines, TF_RangeReplace
The built-in check only worked correctly for FILES and with the introduction of variables in TF 3 this didn't surface during the test Sad

* Fixed: documentation error for TF_Merge (seperator and filename where swapped) and added examples on how to use TF_Merge in a Loop and with FileSelectFile

Bug reports welcome as I just found the bug and fixed it... Embarassed
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
brandon02852



Joined: 15 May 2009
Posts: 15

PostPosted: Sat Apr 24, 2010 6:45 pm    Post subject: Reply with quote

Hi I am trying to search a file for certain text and have it tell me the line. Then I store that line number in a variable and have the TF_RemoveLines function remove the line number present in the variable...

Here is what I have.


Code:
settingspingline1:=TF_Find("!settings.ini", "", "", "SettingsManager.floatSet", 1, 0)

If settingspingline1 != 0
{
TF_RemoveLines("!settings.ini", "%settingspingline1%,%settingspingline1%")
MsgBox, Values removed!
}


What the script is doing is deleting all the contents of the settings.ini file instead of just the line I stated in the function.


I hope someone can help...
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Sat Apr 24, 2010 6:56 pm    Post subject: Reply with quote

Your code highlighting the errors:

Code:
settingspingline1:=TF_Find("!settings.ini", "", "", "SettingsManager.floatSet", 1, 0) ; no need for the ! with TF_Find as it doesn't change the file it always returns a variable

If settingspingline1 != 0
{
TF_RemoveLines("!settings.ini", "%settingspingline1%,%settingspingline1%")
MsgBox, Values removed!
}


correct:

Code:
settingspingline1:=TF_Find("settings.ini", "", "", "SettingsManager.floatSet", 1, 0) ; ! removed

If settingspingline1 != 0
{
TF_RemoveLines("!settings.ini", settingspingline1,settingspingline1) ; removed " and % just pass on the variable settingspingline1
MsgBox, Values removed!
}


This should work.
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
brandon02852



Joined: 15 May 2009
Posts: 15

PostPosted: Sat Apr 24, 2010 7:06 pm    Post subject: Reply with quote

hugov


Thank you for a very quick and detailed response. This has remedied my problem and now I may continue to program!


Again I appreciate your assistance. Very Happy
Back to top
View user's profile Send private message
brandon02852



Joined: 15 May 2009
Posts: 15

PostPosted: Sun Apr 25, 2010 2:59 pm    Post subject: Reply with quote

Sorry to bother you again Hugo but I have run into another issue...


I am trying to write text to a line in a file...and it writes fine and to the proper place...but it is not writing the value of a variable...


Here is my code...

Code:
TF_InsertLine("!settings.ini","31",31,"SettingsManager.floatSet GSDefaultLatencyCompensation 0.0%ping%0000")
TF_InsertLine("!settings.ini","32",32,"SettingsManager.U32Set GSInterpolationTime %ping%")


and in the file it gives me

SettingsManager.floatSet GSDefaultLatencyCompensation 0.0%ping%0000
SettingsManager.U32Set GSInterpolationTime %ping%



I have also tried ping without the percent signs. Is there any way I can get this to work?
Back to top
View user's profile Send private message
brandon02852



Joined: 15 May 2009
Posts: 15

PostPosted: Tue Apr 27, 2010 8:28 pm    Post subject: Reply with quote

Never mind...I discovered a solution myself. If there is anyone here with a similar dilemma here is the solution.

You have to insert the line with the variable you want in the text. NO percent marks here.

Code:
TF_InsertLine("!settings.ini","31",31,"SettingsManager.floatSet GSDefaultLatencyCompensation 0.ping0000")


Then replace the text of the variable with the actual value of the variable

Code:
TF_ReplaceInLines("!settings.ini","31",31,"ping",ping)



And then you can write any variable in your script into a file.
Back to top
View user's profile Send private message
ruespe*
Guest





PostPosted: Wed Apr 28, 2010 6:41 am    Post subject: Reply with quote

You also can do it in the correct way, combining literals and variables:
Code:
TF_InsertLine("!settings.ini","31",31,"SettingsManager.floatSet GSDefaultLatencyCompensation 0.0" . ping . "0000")
TF_InsertLine("!settings.ini","32",32,"SettingsManager.U32Set GSInterpolationTime " . ping)
Back to top
brandon02852



Joined: 15 May 2009
Posts: 15

PostPosted: Wed Apr 28, 2010 9:23 pm    Post subject: Reply with quote

Oh thanks for the help ruespe...lots less code that way.
Back to top
View user's profile Send private message
stressbaby



Joined: 17 Aug 2009
Posts: 131

PostPosted: Sat Jun 26, 2010 5:51 pm    Post subject: RemoveDuplicateGroupsOfLines? Reply with quote

Is there a TF function that can remove duplicate or matching groups of lines? For example, could it find and remove the red below, but not the orange?

Code:
QT: test 1
test 1
here
QT: test 2
test 2
here and
here
QT: test 3
test 3
here
QT: test 1
test 1
here

QT: test 4
test 4

(edited for clarity)
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Sat Jun 26, 2010 7:11 pm    Post subject: Reply with quote

Not exactly, with with a bit of pre- and postprocessing you could:

Code:
var=
(join`r`n
QT: test 1
test 1
here
QT: test 2
test 2
here and
here
QT: test 3
test 3
here
QT: test 1
test 1
here
QT: test 4
test 4
)

stringreplace, var, var, `r`n, |||, all ; remove all lines breaks
stringreplace, var, var, QT:, `r`nQT:, all ; now put them back in on the QT: lines, that way we have one line per QT section which we can check for duplicates :wink:
var:=TF_RemoveDuplicateLines(var) ; remove duplicates
stringreplace, var, var, `n, , all ; get red of the new lines
stringreplace, var, var, |||, `n, all ; put them back in
MsgBox % Var
Either this or probably a convoluted regex(replace) with some backreferencing magic Shocked
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3 ... 8, 9, 10, 11, 12, 13  Next
Page 9 of 13

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group