Turning a column of text into hotstrings Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
duzymichal
Posts: 16
Joined: 31 Mar 2017, 04:11

Turning a column of text into hotstrings

26 Oct 2017, 18:45

Hello

I have two colums of text converted from excel into a text file. I would like to create a series of hotstrings that would turn each of the line in the left column into the corresponding line from the right column, when typed 'n' at the beginning. So I'd like to turn something like this:

(when I preview this text the columns seem to be one space apart, in reality they're further apart)

LK.140282.102.27 HTTS
SX.740456.266 HTTS
TT.702968.120 B&RI-CO (C)
GB.301555.340 HLLS
YH.541353.315.16 MID (VS)


into this:

::nLK.140282.102.27:: HTTS
return
::nSX.740456.266:: HTTS
return
::nTT.702968.120:: B&RI-CO (C)
return
::nGB.301555.340:: HLLS
return
::nYH.541353.315.16:: MID (VS)
return
::nRF.901361.303:: MID (VS)
return

Every single line in the left column begins with a letter and ends with a digit. Every single line in the right column begins with a letter and ends with either a letter or parentheses.

I'd be happy to do this by hand but the columns consist of almost half a million lines. Could you please help me with this with RegEx or a similar type of artistry? It would be an amazing help :) .
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Turning a column of text into hotstrings  Topic is solved

26 Oct 2017, 20:27

So there are a few approaches.

You could maybe learn the RegEx hotstrings. This isn't a native function of AHK, you'd need this library. It is on the old forums, so hopefully the files you need are available still: https://autohotkey.com/board/topic/1147 ... ntry667522 I personally have never used these.

You could alternatively use RegEx to create a separate ahk script (or even the same one). What I would actually do is just use RegExReplace() to replace the first space in each line (you can set a limit to how many replacements are made), and stick :: on front. To go line by line, I would use Loop, Read.

Code: Select all

Loop, Read, yourfile.txt
SendInput % "::" RegExReplace(A_LoopReadLine," ","::",,1){Enter}
return
The % at the start forces expressions. This basically lets you stick the RegExReplace() function inside the SendInput command, rather than doing it on a line above and then calling the variable in SendInput. The {Enter} at the end is just to put it on a new line. You can use StrReplace() instead actually, which may give better performance.

Notice that you don't need the return after each auto-replace hotstring.

A third approach is to actually use the Input command (triggered by n). You could then use the Loop, Read to search for what you just typed by using RegExMatch(). And if there is a match, then use RegExReplace() to remove the first part of the string (which you typed) by replacing it with nothing "" and send the remainder. Again though StrReplace() may be faster; you may use InStr() instead of RegExMatch().
duzymichal
Posts: 16
Joined: 31 Mar 2017, 04:11

Re: Turning a column of text into hotstrings

28 Oct 2017, 10:21

Thanks for the reply.
It returns an error in the second line on an a sample I've given at the beginning:

Error Unexpected "{"
Line#
001:Loop,Read, example2.txt
002:SendInput,"::"RegExReplace(A_LoopReadLine," ","::",,1){enter}
003:Return
004:Exit

I've actually resolved it with a simple VBA script still in Excel. Thanks for help :)
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Turning a column of text into hotstrings

28 Oct 2017, 10:52

Ooops, I didn't test that part (if I even tested the code, not sure I did). It should have quotes around the enter keystroke:

SendInput, % "::" RegExReplace(A_LoopReadLine," ","::",,1) "{Enter}"

This is because that % forced Expression mode, so literal strings like the first :: and the {Enter} should be wrapped in quotation marks.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHK_user, Google [Bot], JnLlnd, MannyKSoSo, RevDoug and 61 guests