Get longest line's character count in multiline string Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
KolaBorat
Posts: 2
Joined: 24 Apr 2024, 09:16

Get longest line's character count in multiline string

25 Apr 2024, 09:17

Fighting with RegExMatch and I can't seem to wrap my head around the different elements of it.
I want the 'x' variable to be also responsive to a different length of the text that my macro info box might have when I add more macros down the line as 'y' does, but I am struggling to get the subpatterns for each new line.

Also don't know, if it's a better option to put the new lines into an actual new line, but that messed my ability to get the subpatterns even more.

Only idea, apart from getting the subpatterns to be rightly recognized (damn these RegEx elements :headwall:), is to loop it with a new StartingPos parameter each iteration and add it into a list that I will check the different lengths of after and add the longest one to the 'x' with an appropriate modifier. Or if there is any other option to get the same desired result that I don't know of?

If anyone could spare some time, I would really appreciate it. :thumbup:

Code: Select all

msg := "Alt+F1 - Command Hint`nAlt+Q - Bold+Paste`nF1 - Hello World`nF2 - This is a much longer text that might make the info box extremely long`nF4 - Thank you"

RegExMatch(msg, "(\b|`n)\K(.*?)(?=(\b|`n))", Rows)
x := 190 ;placeholder

y := (Count := StrLen(RegExReplace(msg, "[^`n]")) + 1) * 23 + 1

SplashTextOn, %x%, %y%, Command Hint, %msg%
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Get longest line's character count in multiline string  Topic is solved

25 Apr 2024, 13:25

Code: Select all

#Requires AutoHotkey v1.1.33.11
msg := "
(
Alt+F1 - Command Hint
Alt+Q - Bold+Paste
F1 - Hello World
F2 - This is a much longer text that might make the info box extremely long
F4 - Thank you
)"
MsgBox 64, Length of longest line, % longestLineCharCount(msg)

longestLineCharCount(str) {
 len := 0
 For each, line in StrSplit(str, "`n", "`r")
  len := Max(len, StrLen(line))
 Return len
}
If you are new to AHK, I recommend using its current version, which is v2, instead of this older deprecated version that is no longer developed.
KolaBorat
Posts: 2
Joined: 24 Apr 2024, 09:16

Re: Get longest line's character count in multiline string

29 Apr 2024, 04:08

Perfect, ty so much <3
I was so absorbed with getting the RegEx working I didn't bother looking for alternatives :facepalm:

I would like to get into v2, but from what I see it seems to me that some codes just work with v1, but are a hassle to convert to v2 and there are a lot more examples/forum threads for v1 atm anyway
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Get longest line's character count in multiline string

29 Apr 2024, 05:04

A v2 example is below.

Code: Select all

#Requires AutoHotkey v2.0
msg := '
(
Alt+F1 - Command Hint
Alt+Q - Bold+Paste
F1 - Hello World
F2 - This is a much longer text that might make the info box extremely long
F4 - Thank you
)'
MsgBox longestLineCharCount(msg), 'Length of longest line', 'Iconi'

longestLineCharCount(str) {
 len := 0
 For line in StrSplit(str, '`n', '`r')
  len := Max(len, StrLen(line))
 Return len
}
User avatar
Chunjee
Posts: 1589
Joined: 18 Apr 2014, 19:05
Contact:

Re: Get longest line's character count in multiline string

30 Apr 2024, 09:36

Just for fun:

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

longestString := A.maxBy(["string1", "string12", "string123"], A.size)
msgbox, % longestString
; => "string123"

msgbox, % A.size(longestString)
; => 9
I find this very readable and portable
User avatar
Chunjee
Posts: 1589
Joined: 18 Apr 2014, 19:05
Contact:

Re: Get longest line's character count in multiline string

30 Apr 2024, 09:44

Hopefully this is obvious but if you just want the character count you can fit it in one line simply enough:

Code: Select all

myMultiLineStr := "hello`nworld`nfoobar"
msgbox, % A.size(A.maxBy(strSplit(myMultiLineStr, "`n"), A.size))
; => 6
https://biga-ahk.github.io/biga.ahk/#/?id=map
https://biga-ahk.github.io/biga.ahk/#/?id=size
https://biga-ahk.github.io/biga.ahk/#/?id=maxby

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mikeyww, vanove and 176 guests