AlphaBravo wrote:Code: Select all
list := ( "apple apple rhubarb grape rhubarb grape banana apple rhubarb grape" ) MsgBox % RegExReplace(list, "s)(.*?)(\V+)(\r?\n)(grape)", "$1$4$3$2") return
Swap/move lines in text file
Re: Swap/move lines in text file
i can only dream of writing this kind of RegEx in this lifetime! however, i resolve to regale myself still with this kind of RegEx in the next lifetime.
- FanaticGuru
- Posts: 1907
- Joined: 30 Sep 2013, 22:25
Re: Swap/move lines in text file
AlphaBravo is something of a savant when it comes to RegEx.Guest10 wrote:i can only dream of writing this kind of RegEx in this lifetime! however, i resolve to regale myself still with this kind of RegEx in the next lifetime.
Like many things you don't really have to understand it to use it.
If it makes you feel better it can be turned into a nifty looking function that will bewilder your friends and impress the ladies.
Code: Select all
list :=
(
"apple
apple
rhubarb
grape
rhubarb
grape
banana
apple
rhubarb
grape"
)
Msgbox % _UpShift(list, "Grape")
_upshift(list, line)
{
return RegExReplace(list, "s)(.*?)(\V+)(\r?\n)(%line%)", "$1$4$3$2")
}
This should do better.
Code: Select all
list :=
(
"apple
apple
rhubarb
grape
rhubarb
grape
banana
apple
rhubarb
grape"
)
Msgbox % _UpShift(list, "grape")
_upshift(list, line)
{
return RegExReplace(list, "s)(.*?)(\V+)(\r?\n)" line, "$1" line "$3$2")
}
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Re: Swap/move lines in text file
thanks, all dutifully booked and archived for prosperity.
- dmg
- Posts: 287
- Joined: 02 Oct 2013, 01:43
- Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
- Contact:
Re: Swap/move lines in text file
@kiwichick:
Found it! I really should not be that excited about though. It was a simple error I should have caught immediately. AutoHotkey works internally (like when you assign text to a var) with line breaks as new line characters. By default Windows files express line breaks as both a return character and a new line character.
When the FileRead command gets the file content it gets both characters for each line break. But my function is designed to only work with `n, not `r`n. This should fix the issue:
The *t option for FileRead tells it to convert the content before placing it in the variable. Alternatively I could have used StringReplace to convert the content in the function itself.
BTW:
It is interesting that you use $ in your vars. I have never seen that in AHk before and was not aware that character was allowed. Do you do that to make the vars easier to locate visually/by search command?
Found it! I really should not be that excited about though. It was a simple error I should have caught immediately. AutoHotkey works internally (like when you assign text to a var) with line breaks as new line characters. By default Windows files express line breaks as both a return character and a new line character.
When the FileRead command gets the file content it gets both characters for each line break. But my function is designed to only work with `n, not `r`n. This should fix the issue:
Code: Select all
$final = C:\SCRIPTING\AutoHotkey\AstrologistAutoHotkey\final.ahk
$shift = Gui, 2:Font
fileread, list, *t %$final%
msgbox, % list
msgbox, % _upshift(list, $shift)
_upshift(list, line)
{
items := {}
loop, parse, list, `n
{
items.insert(a_loopfield)
}
loop, % items.maxindex()
{
if (items[a_index] = line)
{
items.remove(a_index)
items.insert(a_index - 1, line)
}
}
loop, % items.maxindex()
{
out .= items[a_index] . "`n"
}
return rtrim(out, "`n")
}
BTW:
It is interesting that you use $ in your vars. I have never seen that in AHk before and was not aware that character was allowed. Do you do that to make the vars easier to locate visually/by search command?
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
Re: Swap/move lines in text file
Well it's reassuring to know there's nothing wrong my code Good luck with your investigation and I look forward to hearing back from you - however long it takesdmg wrote: Yikes! I have been over your code several times and I can't find a problem. Everything checks out except the output of the FileRead command. It is getting the file content but for some reason the function I wrote does not work on it correctly. I will play with it some more and trace through the entire thing later. Sorry for the delay, and bear in mind that your code appears to be fine. It is MY code that is misbehaving.
Re: Swap/move lines in text file
Thanks for your contribution. I know a wee bit about regex so it's not too daunting for me, although I don't really understand what your code is doing. But I don't think I know how I would use your example with variables for the list and shift expression.AlphaBravo wrote:Code: Select all
list := ( "apple apple rhubarb grape rhubarb grape banana apple rhubarb grape" ) MsgBox % RegExReplace(list, "s)(.*?)(\V+)(\r?\n)(grape)", "$1$4$3$2") return
Re: Swap/move lines in text file
This is so frustrating I want to scream!! That didn't work either. I wonder if uploading the actual files for you to look at would help?dmg wrote:@kiwichick:
Found it! I really should not be that excited about though. It was a simple error I should have caught immediately. AutoHotkey works internally (like when you assign text to a var) with line breaks as new line characters. By default Windows files express line breaks as both a return character and a new line character.
When the FileRead command gets the file content it gets both characters for each line break. But my function is designed to only work with `n, not `r`n. This should fix the issue:The *t option for FileRead tells it to convert the content before placing it in the variable. Alternatively I could have used StringReplace to convert the content in the function itself.Code: Select all
$final = C:\SCRIPTING\AutoHotkey\AstrologistAutoHotkey\final.ahk $shift = Gui, 2:Font fileread, list, *t %$final% msgbox, % list msgbox, % _upshift(list, $shift) _upshift(list, line) { items := {} loop, parse, list, `n { items.insert(a_loopfield) } loop, % items.maxindex() { if (items[a_index] = line) { items.remove(a_index) items.insert(a_index - 1, line) } } loop, % items.maxindex() { out .= items[a_index] . "`n" } return rtrim(out, "`n") }
BTW:
It is interesting that you use $ in your vars. I have never seen that in AHk before and was not aware that character was allowed. Do you do that to make the vars easier to locate visually/by search command?
And I use the $ because it was the variable character of the first scripting language I started to learn. And, yes, because using an uncommon (ahk allowed) character means I can customize my Notepad++ language so that the variables stand out. Very helpful when learning Although I think the $ isn't going to be allowed in AutoHotkey v2.
Re: Swap/move lines in text file
OOOOOPS!!!! CANCEL THAT LAST POST!!!!
I forgot I had made a change to the file so of course the script couldn't find what I wanted it to. But I fixed it now and it works, YAY!!!
Thank you so so much dmg. You've been legend
I forgot I had made a change to the file so of course the script couldn't find what I wanted it to. But I fixed it now and it works, YAY!!!
Thank you so so much dmg. You've been legend
- FanaticGuru
- Posts: 1907
- Joined: 30 Sep 2013, 22:25
Re: Swap/move lines in text file
I assume it is a habit from another programming language. Some languages require it. It generally designates the variable as a global variable.dmg wrote:@kiwichick:BTW:
It is interesting that you use $ in your vars. I have never seen that in AHk before and was not aware that character was allowed. Do you do that to make the vars easier to locate visually/by search command?
In AHK though it just looks pretty. Though it is not a bad idea if it helps you keep track of things.
Some people like to put lower case letters before their variables to designate the type of variable like: oRecords and sName for object and string.
Others always make variables start with a lower case and commands with an upper case like: Sleep cooldown
AHK doesn't care but I have debated on adopting a style from another language but I seem to just end up naming the variables something descriptive at the time without consistency in the starting character or case signifying anything. Sometimes they start uppercase and sometimes they are lowercase, sometimes I use Under_Score and others times I use CamelCase just what ever seems the most helpful at the time.
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Re: Swap/move lines in text file
I downloaded a custom ahk language file for Notepad++ It makes built-in variables bright pink making them easier to in the context of the whole script. I just added $ to it. The $ is allowed by AutoHotkey so I used that - it could have been any letter, as you say, but it would end up colouring too many words that weren't variables.FanaticGuru wrote:I assume it is a habit from another programming language. Some languages require it. It generally designates the variable as a global variable.dmg wrote:@kiwichick:BTW:
It is interesting that you use $ in your vars. I have never seen that in AHk before and was not aware that character was allowed. Do you do that to make the vars easier to locate visually/by search command?
In AHK though it just looks pretty. Though it is not a bad idea if it helps you keep track of things.
Some people like to put lower case letters before their variables to designate the type of variable like: oRecords and sName for object and string.
Others always make variables start with a lower case and commands with an upper case like: Sleep cooldown
AHK doesn't care but I have debated on adopting a style from another language but I seem to just end up naming the variables something descriptive at the time without consistency in the starting character or case signifying anything. Sometimes they start uppercase and sometimes they are lowercase, sometimes I use Under_Score and others times I use CamelCase just what ever seems the most helpful at the time.
FG
- dmg
- Posts: 287
- Joined: 02 Oct 2013, 01:43
- Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
- Contact:
Re: Swap/move lines in text file
Yay! Glad it worked. Good luck with the remainder of your script.
Yes, they do call me: THE LEGENDARY DMG! (strikes hands-on-hips hero pose... Then ruins the effect by sneezing violently and sniffling.)kiwichick wrote:Thank you so so much dmg. You've been legend
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
Re: Swap/move lines in text file
progress is coming along nicely (albeit slowly)dmg wrote:Yay! Glad it worked. Good luck with the remainder of your script.
dmg wrote: Yes, they do call me: THE LEGENDARY DMG! (strikes hands-on-hips hero pose... Then ruins the effect by sneezing violently and sniffling.)
Who is online
Users browsing this forum: Chunjee, hiahkforum, RussF and 204 guests