Parsing only certain parts of a string split Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Parsing only certain parts of a string split

15 May 2019, 20:57

This is a hotkey I use to choose words and to parse a delimited text file with *. Once it finds a match (I want it to match the first split part only, it sends the second (which would be a corrected spelling usually) and if a third part exists a MsgBox pops up with the before, after and definition or explanation. This is what my txt file contains for an example:

carpometacarpal*carpometacarpal*The carpometacarpal joint of the thumb (pollex), also known as the first carpometacarpal joint, or the trapeziometacarpal joint (TMC) because it connects the trapezium to the first metacarpal bone, plays an irreplaceable role in the normal functioning of the thumb.

1st*first*

first*1st*

It usually works perfect but with the ones I have above I see this problem. Since it is parsing I assume the whole section from top down it is seeing the word first in the definition part of carpometacarpal and sending the carpometacarpal. I want it to only search the first word in the section and if that is found, send the second part and msgbox the definition. Those last 2 parts I think I have down fine. It's the only matching the first part. I'm sure it can be done with regex but I'm not good at that yet and I'm hoping for something I can understand so I can use in other places easily if need be.

Thank you very much in advance and I hope that makes sense.

Code: Select all

Capslock & j::

;  ask forum how to instring only the first part individually but if the third section exists and only send - not parse - that if first part only matches

Numpad1::
SetStoreCapslockMode off
Gosub wordsaveoptions2  ;  this just chooses and copies the word(s) I'm looking to search with

Sleep 300
SearchText := clipboard  ;  added " to try cause someone else used them 

Loop, read, C:\Dropbox KHGmail\Dropbox\AHK\dictionary definition popup.txt

IfInString, A_LoopReadLine, %searchtext% ; worked was okay

{
{
;msgbox, 4, , %searchtext% found, 1

Loop, parse, A_LoopReadLine, *
StringSplit, string, A_LoopReadLine, *, ; %A_Space%
Ifinstring, A_LoopReadline, %searchtext%
If searchtext = %string2%
;Msgbox, 0, IT IS HERE, I EXIST %string2%, 0.7
Send {DELETE}
SetStoreCapslockMode off
SLeep 350
Send %string2%
}

If String3 is space
{
;msgbox, 0, , the definition is missing, 1
SetStoreCapslockMode off
Reload
Return
}
Else
If String3 is not space
{
SetTimer, WinMoveMsgBox2, 50
Msgbox, 0, Found it!!!, Searched ~~~ %String1%`n`n`nCorrected Version ~~~  %String2%`n`n`nDefinition ~~~`n`n%String3%, 6
SetStoreCapslockMode off
Return
}}

SearchText = %searchtext%
IfnotInString, A_LoopReadLine, "%searchtext%" || IfnotInString, A_LoopField, "%searchtext%"
{
Msgbox, 0, IT IS NOT HERE, NOT FOUND %searchtext% in search string, 0.7
Send %searchtext%
SetStoreCapslockMode off
Return
}
SetStoreCapslockMode off
Return

wordsaveoptions2: ; for temporary hotkey saving
Msgbox, 0, How many, Choose how many words., 0.3
wordsaveoptionsnomsgbox:
clipback := ClipboardAll
clipboard=
Input, Character, L1 T1
if (ErrorLevel = "Timeout")
{
Send {CTRLDOWN}{SHIFTDOWN}{RIGHT}{CTRLUP}{LEFT}{SHIFTUP}
Sleep 300
Send {CTRLDOWN}c{CTRLUP}
Sleep 300
ClipWait
return
}Else
If ((character ~= "1|o"))
{
Send {CTRLDOWN}{RIGHT}{CTRLUP}{SPACE}{CTRLDOWN}{LEFT}{CTRLUP}{DELETE}{CTRLDOWN}{SHIFTDOWN}{LEFT}{CTRLUP}{SHIFTUP}{CTRLDOWN}c{CTRLUP}
Clipwait, 1
}
Else
If ((character ~= "2|t"))
{
Send {CTRLDOWN}{RIGHT 2}{CTRLUP}{SPACE}{CTRLDOWN}{LEFT}{CTRLUP}{DELETE}{CTRLDOWN}{SHIFTDOWN}{LEFT 2}{CTRLUP}{SHIFTUP}{CTRLDOWN}c{CTRLUP}
ClipWait
}
Else
If ((character ~= "3|w"))
{
Send {CTRLDOWN}{RIGHT 3}{CTRLUP}{SPACE}{CTRLDOWN}{LEFT}{CTRLUP}{DELETE}{CTRLDOWN}{SHIFTDOWN}{LEFT 3}{CTRLUP}{SHIFTUP}{CTRLDOWN}c{CTRLUP}
ClipWait
}
Else
If ((character ~= "f|4"))
{
Send {CTRLDOWN}{RIGHT 4}{CTRLUP}{SPACE}{CTRLDOWN}{LEFT}{CTRLUP}{DELETE}{CTRLDOWN}{SHIFTDOWN}{LEFT 4}{CTRLUP}{SHIFTUP}{CTRLDOWN}c{CTRLUP}
ClipWait
}
Else
If ((character ~= "v|5"))
{
Send {CTRLDOWN}{RIGHT 5}{CTRLUP}{SPACE}{CTRLDOWN}{LEFT}{CTRLUP}{DELETE}{CTRLDOWN}{SHIFTDOWN}{LEFT 5}{CTRLUP}{SHIFTUP}{CTRLDOWN}c{CTRLUP}

ClipWait
}
Else
If ((character ~= "6|x"))
{
Send {CTRLDOWN}{RIGHT 6}{CTRLUP}{SPACE}{CTRLDOWN}{LEFT}{CTRLUP}{DELETE}{CTRLDOWN}{SHIFTDOWN}{LEFT 6}{CTRLUP}{SHIFTUP}{CTRLDOWN}c{CTRLUP}

ClipWait
}
Else
If ((character ~= "7|n"))
{
Send {CTRLDOWN}{RIGHT 7}{CTRLUP}{SPACE}{CTRLDOWN}{LEFT}{CTRLUP}{DELETE}{CTRLDOWN}{SHIFTDOWN}{LEFT 7}{CTRLUP}{SHIFTUP}{CTRLDOWN}c{CTRLUP}
ClipWait
}
Else
if Character=S
{
Reload
}
Return

A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Re: Parsing only certain parts of a string split

16 May 2019, 03:54

It's hard to test your script without actually seeing the text. Try attaching the text file or even better putting an example into a string.

Code: Select all

Loop, read, C:\........\textfile.txt
; This is fine for on your machine, but

Code: Select all

; It's easier for others to see if you put your text into a string or attach the file to the forum.
String_Example =
(
Some text*etc*etc
Some more text*etc*etc
Even more text*etc
You get the idea
)

SearchText := clipboard 
Loop, parse, String_Example, `n ;Or whatever delimiter you choose
var := StrSplit(A_loopfield, "*").1
For clarity, leave out parts of your code that are not necessary to understanding specifically what you want to do. I don't need to see 10 lines of code after an if then statement. Just use message boxes.

Code: Select all

; Don't include all of this
if (ErrorLevel = "Timeout")
{
Send {CTRLDOWN}{SHIFTDOWN}{RIGHT}{CTRLUP}{LEFT}{SHIFTUP}
Sleep 300
Send {CTRLDOWN}c{CTRLUP}
Sleep 300
ClipWait
return

;When this will suffice:
if (ErrorLevel = "Timeout")
     Msgbox, Timed Out
In other words clean up and simplify your code so the rest of us can better help you. Also, you'll often find the solution saving you the trouble of asking for help.
just me
Posts: 9482
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Parsing only certain parts of a string split  Topic is solved

16 May 2019, 04:30

Maybe something like this (pseudocode):

Code: Select all

...
SearchText := Clipboard
Loop, Read, C:\Dropbox KHGmail\Dropbox\AHK\dictionary definition popup.txt
{
   StrSplit, Part, A_LoopReadLine, *
   If (Part0 = 3) ; if splitted into 3 parts
   {
      If (Part1 = SearchText) ; if Part1 equals SearchText
      {
         ...
         Send, %Part2% ; send Part2
         ...
         If (Part3 <> "") ; if Part3 has contents
         {
            ...
            Msgbox, 0, Found it!!!, Searched ~~~ %Part1%`n`n`nCorrected Version ~~~  %Part2%`n`n`nDefinition ~~~`n`n%Part3%, 6
            ...
         }
         ...
      }
   }
}
...
:?:
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: Parsing only certain parts of a string split

16 May 2019, 18:46

@A_Perry_1984
Thank you for your comments. I did include in my post a bit of what my larger text file is which is this:

String:
carpometacarpal*carpometacarpal*The carpometacarpal joint of the thumb (pollex), also known as the first carpometacarpal joint, or the trapeziometacarpal joint (TMC) because it connects the trapezium to the first metacarpal bone, plays an irreplaceable role in the normal functioning of the thumb.

1st*first*

first*1st*

I'm sorry about the way I left my code. I did clean some of it up but I was afraid if I took more out it would not make sense. I'll take your advice if I do a post again. Thank you again.
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: Parsing only certain parts of a string split

16 May 2019, 18:47

@just me Thank you very much I will try that out, I appreciate your help.

Kelly
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: Parsing only certain parts of a string split

16 May 2019, 19:08

I got an error on the Strsplit, part line but that is okay I will try to figure it out, thank you very much for your help. It gave me an idea of how to get the pieces looked at separately.
just me
Posts: 9482
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Parsing only certain parts of a string split

17 May 2019, 02:30

It must be StringSplit (command). Sorry!
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: Parsing only certain parts of a string split

17 May 2019, 13:33

It’s okay I had added the parenthesis which made the strsplit part work. I’m still trying to figure out how the rest works to fit it in my hotkey but I do appreciate your help.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, KenC80, Proxima and 253 guests