Swap/move lines in text file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Swap/move lines in text file

23 Jan 2014, 22:20

i can only dream of writing this kind of RegEx in this lifetime! :shock: however, i resolve to regale myself still with this kind of RegEx in the next lifetime. :lol: :ugeek:
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
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Swap/move lines in text file

23 Jan 2014, 23:09

Guest10 wrote:i can only dream of writing this kind of RegEx in this lifetime! :shock: however, i resolve to regale myself still with this kind of RegEx in the next lifetime. :lol: :ugeek:
AlphaBravo is something of a savant when it comes to RegEx.

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")
}
EDIT: I am something of an idiot savant. You can't just plop a variable in the needle of the RegEx like that.

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
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Swap/move lines in text file

24 Jan 2014, 00:19

thanks, all dutifully booked and archived for prosperity. :lol: :ugeek:
User avatar
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

24 Jan 2014, 01:01

@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:

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")
 }
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?
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
kiwichick
Posts: 142
Joined: 21 Jan 2014, 22:03

Re: Swap/move lines in text file

24 Jan 2014, 01:09

dmg 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. :D
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 takes :-)
kiwichick
Posts: 142
Joined: 21 Jan 2014, 22:03

Re: Swap/move lines in text file

24 Jan 2014, 01:17

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
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.
kiwichick
Posts: 142
Joined: 21 Jan 2014, 22:03

Re: Swap/move lines in text file

24 Jan 2014, 01:25

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:

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")
 }
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?
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?

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.
kiwichick
Posts: 142
Joined: 21 Jan 2014, 22:03

Re: Swap/move lines in text file

24 Jan 2014, 01:34

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 :-)
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Swap/move lines in text file

24 Jan 2014, 01:35

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?
I assume it is a habit from another programming language. Some languages require it. It generally designates the variable as a global variable.

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
kiwichick
Posts: 142
Joined: 21 Jan 2014, 22:03

Re: Swap/move lines in text file

24 Jan 2014, 01:52

FanaticGuru wrote:
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?
I assume it is a habit from another programming language. Some languages require it. It generally designates the variable as a global variable.

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
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.
User avatar
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

24 Jan 2014, 02:44

Yay! Glad it worked. Good luck with the remainder of your script. :)
kiwichick wrote:Thank you so so much dmg. You've been legend :-)
Yes, they do call me: THE LEGENDARY DMG! (strikes hands-on-hips hero pose... Then ruins the effect by sneezing violently and sniffling.)
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
kiwichick
Posts: 142
Joined: 21 Jan 2014, 22:03

Re: Swap/move lines in text file

24 Jan 2014, 03:19

dmg wrote:Yay! Glad it worked. Good luck with the remainder of your script. :)
progress is coming along nicely (albeit slowly) :-)
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.)
:lol: :lol: :lol: :lol: :lol: :lol:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, hiahkforum, RussF and 204 guests