 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Tom
Joined: 20 Dec 2009 Posts: 15
|
Posted: Sun Jan 03, 2010 9:11 pm Post subject: |
|
|
Thanks
Not to sure I understand the first part ?
Would that just match on b ??
As for the second part it 'should' always be at the end in the same position.. But it will always be a single character at the right most position.
Is it possible to read the line right > left and capture the first character ?
Thanks for your advice  |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Mon Jan 04, 2010 11:51 am Post subject: |
|
|
1) The "b" was only an example of how to use XTF_Find.
2) You can use TF_Readlines to read the first line and use StrLen to determine the length of the lines and use that value for TF_ColGet and a simple StringReplace to remove the `n as I explained earlier. | Code: | Text=
(
aMode,,0000003e,4
aMode,,0000003e,A
aMode,,0000003e,C
aMode,,0000003e,3
)
LastCol:=StrLen(TF_ReadLines(Text, 1, 1, 1))
LastChars:=TF_ColGet(Text, 1, 0, LastCol, LastCol)
StringReplace, LastChars, LastChars, `n,,All
MsgBox % LastChars | and no I'm not going to strip these functions for you.
As an alternative to 2) you probably could use a regexmatch/replace to do the same _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
Tom
Joined: 20 Dec 2009 Posts: 15
|
Posted: Mon Jan 04, 2010 9:27 pm Post subject: |
|
|
Thanks Hugov
Unfortunately that didn't work..
| Code: | | GetLine:=TF_Find(ReceivedData,"","","disengage",1,1) |
In this ReceivedData is a constantly streaming / changing variable.
I suppose what I need it to do is capture each entry that has aMode, grab the last digit and repeat doing this until a new command presents.
eg:
aMode,,0000003e,4
aMode,,0000003e,A
aMode,,0000003e,C
aMode,,0000003e,3
upDating,8765,Server
I should then have the full string of aMode characters in a var to use.
But how the heck do I do that !!
Thanks |
|
| Back to top |
|
 |
Tom
Joined: 20 Dec 2009 Posts: 15
|
Posted: Mon Jan 04, 2010 10:01 pm Post subject: |
|
|
Manage to get this far:
| Code: | if ReceivedData contains aMode
GetLine:=TF_Find(ReceivedData,"","","aMode",0,1)
StringSplit, amode, GetLine, `,,
if amode4 is number
MsgBox % amode4 |
This returns each entry.. How do I join them ? amode4 + amode4 etc ..
I'm wondering if this would work ??
| Code: | if ReceivedData contains aMode
{
GetLine:=TF_Find(ReceivedData,"","","aMode",0,1)
StringSplit, amode, GetLine, `,,
var:=amode4 + amode4 etc.. HELP On this
if receivedData not contains aMode
MsgBox % var
} |
Would that work ?? Or something similar ?
Many Thanks  |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Tue Jan 05, 2010 8:04 am Post subject: |
|
|
Because aMode lines can be different length and can have a varying number of commas stringsplit might not be best, a simple parsing loop might do the trick. Pretty basic stuff really, why don't you want to learn the basics of AHK it is all very well documented | Code: | test=
(join`n
aMode,,A0000003e,4
aMode,,,,,,0000003e,A
aMode,0000003e,C
aMode,,0000003e,3
aMode,,00xx,Z
)
var=
Loop, Parse, test, `n, `r
var .= SubStr(A_LoopField, InStr(A_LoopField, ",", false , 0) + 1, 1)
MsgBox % var | Sorry, but this is the last I'll do for your script.
Edit: it is nice idea to introduce a get column X from the right regardless of the length of the lines, I'll put it on my to TODO list...  _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
badcoder Guest
|
Posted: Sun Jan 17, 2010 12:13 am Post subject: |
|
|
Hi folks!
Why isn't it possible to use a variable for "Text" in "TF_Replace"? Somehow I can't use relative paths. Any suggestions? |
|
| Back to top |
|
 |
Guest
|
Posted: Sun Jan 17, 2010 12:20 am Post subject: |
|
|
| badcoder wrote: | | Why isn't it possible to use a variable for "Text" in "TF_Replace"? Somehow I can't use relative paths. Any suggestions? |
Ah... I've forgot to remove the " marks. |
|
| Back to top |
|
 |
forkinpm
Joined: 24 Jan 2009 Posts: 13 Location: Aldersbach, Germany
|
Posted: Tue Jan 26, 2010 5:11 pm Post subject: Our "low-key" dialogue |
|
|
Hallo!
I have completed my first piece of documentation and would like to share it with you before any of the material is seen by other forum members.
I have taken much longer than I had intended but, I have been productive.
The results are interesting:
I propose the following.
If you send me a private email adress, I will send you the introduction.
Depending on your level of interest, then I can send you the rest of the material.
The introduction will have references to the remainder of the material.
I hope to hear from you and I do apologise for the inordinate delay.
I hope that you enjoyed your Christmas and the New Year celebrations.
Regards, forkinpm. _________________ I am building my platform based on a set of steps to take my English texts through segmenting into phrases, resequencing segments based on German rules, adding head-words and then translating with my dictionary.
Regards, forkinpm. |
|
| Back to top |
|
 |
badmojo
Joined: 11 Nov 2005 Posts: 202
|
Posted: Thu Jan 28, 2010 2:52 am Post subject: |
|
|
can someone explain what is wrong with this code? i'm trying to remove all lines that ends with a "\" but somehow it doesn't work..
| Code: | #Include %A_ScriptDir%\tf.ahk
F=!C:\Temp\Startup_log.ini
TF_RegExReplace(F, ".*\\\s*$", "") |
|
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Thu Jan 28, 2010 6:42 am Post subject: |
|
|
| badmojo wrote: | | can someone explain what is wrong with this code? i'm trying to remove all lines that ends with a "\" but somehow it doesn't work |
| Code: | F=
(join`r`n
hello this
is a Text \
to see how it
works \
bye bye
)
MsgBox % TF_RegExReplace(F, "im)(.*)\\\s*$", "$1") | you have to remember the parts before the \ and put it back via $1
Edit: oops I read the question incorrectly, this should do the trick | Code: | F=
(join`r`n
hello this
is a Text \
to see how it
works \
bye bye
)
MsgBox % TF_RegExReplace(F, "m).*\\\s*", "") | The secret is in the m) see the regex docs _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
badmojo
Joined: 11 Nov 2005 Posts: 202
|
Posted: Thu Jan 28, 2010 6:56 am Post subject: |
|
|
ah, i see.. the 'm' stands for multiple lines, i guess.. thanks.
the help files mentions (?im) as the method to switch options, why in TF, the opening bracket is dropped? just curious.. |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Thu Jan 28, 2010 7:03 am Post subject: |
|
|
| badmojo wrote: | | the help files mentions (?im) as the method to switch options, why in TF, the opening bracket is dropped? just curious.. |
The doc says http://www.autohotkey.com/docs/commands/RegExMatch.htm
| Quote: | Options (case sensitive)
At the very beginning of a regular expression, specify zero or more of the following options followed by a close-parenthesis. For example, the pattern "im)abc" would search for abc ... | so the options don't start with a ( but do end with a ) _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Sat Feb 20, 2010 8:48 am Post subject: |
|
|
Update v3.2:
- Fixed: TF_AlignRight, due to a bug it didn't work as it should have, it prepended the number of spaces rather than aligning the text at the specified width
- Changed: TF_SplitFileByLines: New options for SplitAt, now three methods available, see notes at function description (split at specific lines or alternating lines)
- Changed: TF_RemoveLines: New option for StartLine, if negative value is used it will remove the last X lines from file, see notes at function description
- Changed: TF_GetData (helper function) should now avoid unnecessary IfNotExist for files, for scripts with many loops in combination with variables it should improve the speed slightly _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Last edited by SoLong&Thx4AllTheFish on Sat Feb 20, 2010 1:58 pm; edited 1 time in total |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Sat Feb 20, 2010 8:57 am Post subject: |
|
|
Experimental function: SwapLines
This function will allow you to swap lines in a variable or text file, valid options:
1,5 -> Swap lines 1 and 5
1-5,13-27 -> swap lines 1 to 5 with lines 13-27
To test this function you do need TF.ahk, see first post, as it depends on some helper functions.
Not sure if it should be added to TF, so give it a spin and let me know if you have any suggestions or find errors
| Code: |
;#include tf.ahk ; remove comment if you don't include TF in your library
test=
(
1-this is line 1
2-this is line 2
3-this is line 3
4-this is line 4
5-this is line 5
6-this is line 6
7-this is line 7
8-this is line 8
9-this is line 9
10-this is line 10
)
MsgBox % TF_SwapLines(test, "2,4")
;MsgBox % TF_SwapLines(test, "1-5,6-12") ; Note 12 > end of file, should work correctly
;MsgBox % TF_SwapLines(test, "2-4,8-10")
;MsgBox % TF_SwapLines(test, "1-2,5-10")
;MsgBox % TF_SwapLines(test, "1,5-10")
;MsgBox % TF_SwapLines(test, "2-4,8")
;MsgBox % TF_SwapLines(test, "1,10")
; swap 2 lines or two sections of lines in a text file, HugoV
TF_SwapLines(Text, SwapLines)
{
IfInString, SwapLines, + ; check if "incremental" values where used if so Return
Return
StringReplace, SwapLines, SwapLines, `,,`,, UseErrorLevel
If (ErrorLevel > 1) ; too many parameters
Return
TF_GetData(OW, Text, FileName)
StringSplit, Line, Text, `n, `r
Text=
Loop % Line0
Order .= A_Index ","
Order:= "," . Order
IfInString, SwapLines, - ; check if it is a section of lines if so make use of MakeMatchList
{
StringSplit, Swap, SwapLines, `,
Loop % Swap0
If (InStr(Swap%A_Index%,"-") > 0)
{
StringSplit, SectionsCheck, Swap%A_Index%, - ; built in check to see if last line of swap section isn't beyond end of file to prevent inserting empty lines
If (SectionsCheck2 > Line0)
Swap%A_Index% := SectionsCheck1 "-" Line0
Swap%A_Index% := _MakeMatchList(TextFile, Swap%A_Index%)
}
}
Else ; just swap two lines
{
StringSplit, Swap, SwapLines, `,
}
StringReplace, Order, Order, `,%Swap2%`,, `,%Swap1%`,
StringReplace, Order, Order, `,%Swap1%`,, `,%Swap2%`,
StringTrimRight, Order, Order, 1 ; remove trailing ,
StringTrimLeft, Order, Order, 1 ; remove leading ,
Loop, Parse, Order, `, ; create output with lines in the new order (the swapped lines)
; Text .= Line%A_Index% "|" Line%A_LoopField% "`n" ; test line remove comment and comment line below to show test results side by side
Text .= Line%A_LoopField% "`n"
Return TF_ReturnOutPut(OW, Text, FileName, 0)
} |
_________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Sat Feb 20, 2010 11:20 am Post subject: |
|
|
I have looked in but dont know how it works. Here is mine version of basic swapping two lines only. It determines and preserves correct new line of file or source string. Developed it in some hours. So it may contain some stupid bugs. May be it have some lines which gives you any idea for your own function.
| Code: | #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
test=
(
1-this is line 1
2-this is line 2
3-this is line 3
4-this is line 4
5-this is line 5
6-this is line 6
7-this is line 7
8-this is line 8
9-this is line 9
10-this is line 10
)
; just swap line 3 with 4.
swapped1 := swaplines(test, 3, 4)
msgbox %swapped1%
; puts 6 at position after 10. because there is no more then 10 lines.
swapped2 := swaplines(test, 6, 12)
msgbox %swapped2%
swaplines(ByRef _source, _start, _end)
{
If (r := InStr(_source, "`r") ? "`r" : "")
{
nchar := "`r`n"
nlen := 2
}
Else
{
nchar := "`n"
nlen := 1
}
Loop, Parse, _source, `n, %r%
{
If (A_Index < _start)
{
beginLines .= A_LoopField . nchar
}
Else If (A_Index > _end)
{
endLines .= A_LoopField . nchar
}
Else If (A_Index = _start)
{
swapLine1 := A_LoopField . nchar
}
Else If (A_Index = _end)
{
swapLine2 := A_LoopField . nchar
}
Else
{
swapLinesBetween .= A_LoopField . nchar
}
}
new := beginLines . swapLine2 . swapLinesBetween . swapLine1 . endLines
StringTrimRight, new, new, %nlen%
Return new
} |
edit: removed an unnecessary line. _________________ {1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--
Last edited by Tuncay on Sat Feb 20, 2010 2:20 pm; edited 1 time in total |
|
| 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
|