Page 1 of 1

Arrays and text find and replace?

Posted: 25 May 2022, 12:25
by Krd
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

Re: Arrays and text find and replace?

Posted: 25 May 2022, 12:37
by BoBo

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. 						

Re: Arrays and text find and replace?

Posted: 25 May 2022, 13:16
by flyingDman
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.

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

Posted: 25 May 2022, 13:17
by flyingDman
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

Re: Arrays and text find and replace?

Posted: 25 May 2022, 13:33
by sofista
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

Re: Arrays and text find and replace?

Posted: 25 May 2022, 14:54
by Krd
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:

Re: Arrays and text find and replace?

Posted: 25 May 2022, 15:19
by Chunjee
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