Arrays and text find and replace? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Arrays and text find and replace?

Post by Krd » 25 May 2022, 12:25

Hello,

How can I use this find the word SCRIPTS:

Code: Select all

F12::
Clipboard :="Recommended for new scripts due to its superior speed and reliability."
Text := StrSplit(clipboard,"/")								
Find := StrSplit(Text.1,"due").1								
Msgbox %Find%
return
What I want is the last word before the word "DUE". I don't want to directly find the word SCRIPTS but as showed above becauce the same word would be in another place sevral times and would mess up things.


And if anyone could help me to do the below in a better way as I can not use dates and formates to do it myself yet. So I do it the dumbs way :)

How to replace the code under with a better way to do it?

Code: Select all

Clipboard := "25.05.2022"
FindYear := StrSplit(clipboard,"/")								
Year := StrSplit(FindYear.1,".").3								
Msgbox, %Year%
SendInput, Clipboard
Sleep, 1000
Send, {End}
Sleep, 1000
Send, {Backspace 4}
SendInput, 2025         ;All this is doing is to turn the date in clipboard into 25.05.2025. 
return

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Arrays and text find and replace?

Post by BoBo » 25 May 2022, 12:37

Code: Select all

Clipboard := "25.05.2022"			; European (German?) date format
FindYear := StrSplit(clipboard,"/")	; FindYear contains only this item:'25.05.2022' - bc there's no '/' split separator in '25.05.2022'.						
Year := StrSplit(FindYear.1,".").3	; should split '25.05.2022' (the value of the first item in the FindYear-array) at every '.'-character, and keep its 3rd items value.								
Msgbox, %Year%					    ; '2022'
SendInput, Clipboard                ; sending the string'Clipboard'
Sleep, 1000
Send, {End}                         ; jump to the end of that string
Sleep, 1000
Send, {Backspace 4}				    ; remove the strings last four characters: 'Clipboard' = 'Clipb'
SendInput, 2025         			; adding '2025' to 'Clipb' = 'Clipb2025' 
return
:wtf:

Code: Select all

Clipboard := "25.05.2022"
d := StrSplit(clipboard,".")
MsgBox % d.1 "." d.2 "." d.3+3 ; will fail on "30.02.2025" therefore check out AHK's EnvAdd/-Sub command. 						

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Arrays and text find and replace?

Post by flyingDman » 25 May 2022, 13:16

First lets assume that you always start from a valid date. The only time that you can run into problems is if your starting date is 29.02.****. If you just add x to the year, the resulting date my not be valid. So, convert the starting date to YYYYMMDD format, add 1 day (march 1 exists in any year) then add x * 10000000000 then subtract 1 day.

Code: Select all

x := 3										; years to add or subtract
curdat := "29.02.2020"						; valid date
d:= StrSplit(curdat,".")
_curdat := d.3 . d.2 . d.1
_curdat += 1, days							; add 1 day and add subtract it later
_curdat += x * 10000000000
_curdat += -1, days							; subtract the 1 day we added above
FormatTime, ndat, %_curdat%, dd.MM.yyyy
msgbox % ndat
Note: you can also subtract 1 day first and then add it back. The result is not the same. That's the subjective element here! You have 2 valid answers.
Last edited by flyingDman on 25 May 2022, 22:12, edited 2 times in total.
14.3 & 1.3.7

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Arrays and text find and replace?  Topic is solved

Post by flyingDman » 25 May 2022, 13:17

Re: your first question:

Code: Select all

needle := "due"
Clipboard := "Recommended for new scripts due to its superior speed and reliability."

for a,b in c := StrSplit(clipboard," ")
	(b = needle) && rslt := c[a-1]
msgbox % rslt
return
14.3 & 1.3.7

sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Arrays and text find and replace?

Post by sofista » 25 May 2022, 13:33

Krd wrote:
25 May 2022, 12:25
Hello,

How can I use this find the word SCRIPTS:

Code: Select all

F12::
Clipboard :="Recommended for new scripts due to its superior speed and reliability."
Text := StrSplit(clipboard,"/")								
Find := StrSplit(Text.1,"due").1								
Msgbox %Find%
return
What I want is the last word before the word "DUE". I don't want to directly find the word SCRIPTS but as showed above becauce the same word would be in another place sevral times and would mess up things.
What about a regex approach?

Code: Select all

Clipboard :="Recommended for new scripts due to its superior speed and reliability."
RegExMatch(Clipboard, "i)\s(\w+)\s(?=due)", word)
MsgBox, % word    ; output -> scripts

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Arrays and text find and replace?

Post by Krd » 25 May 2022, 14:54

wow, great.

Thank you all!

Bobo, thank you for your noob friendly answers! I try to reuse your codes as I learn from your explanations! :)

The dates part is driving the noob crazy as I try to find workarounds. The date in clipboard is always valid, It wouldn't be a much of a problem if it fails 10 days a year :)
I can just find 10 execuses which would be minimum 100 if I did the same thing manually :lolno:


Much appreciated! :bravo:

User avatar
Chunjee
Posts: 1418
Joined: 18 Apr 2014, 19:05
Contact:

Re: Arrays and text find and replace?

Post by Chunjee » 25 May 2022, 15:19

Krd wrote:
25 May 2022, 12:25
How can I use this find the word SCRIPTS:
Low effort approach:

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

Clipboard := "Recommended for new scripts due to its superior speed and reliability."
array := A.words(Clipboard)
msgbox, % array[A.indexOf(array, "due") - 1]
; => "scripts"
https://biga-ahk.github.io/biga.ahk/#/?id=words
https://biga-ahk.github.io/biga.ahk/#/?id=indexof

Post Reply

Return to “Ask for Help (v1)”