Getting last element in the array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Getting last element in the array

Post by roysubs » 28 Feb 2021, 00:48

I get some frustrations working with arrays in Autohokey. Can't quite get my head around it with the old style arrays and the new style etc, so was looking for help. In the below, I get problems as soon as I try to use arrays. tmp creates an array ok but then I need to get the last element (which I think should be "5" as 5 elements in the array) but %tmp_last% is "8" (!?!?). Ah, it's because there are 3 spaces after "ya64ug". How can I split an array by "any amount of white space"? I want to get the tag value, which I think should be "tmp.5" so I've tried to do that, but again, failure. How do I fix this so that the last MsgBox returns "78"?

Code: Select all

username := "ya64ug   ,  tag = 78"
MsgBox, %username%
tmp := StrSplit(username, " ")
tmp_last := % tmp.MaxIndex()
MsgBox, %tmp_last%
tag := tmp%tmp_last%
MsgBox, %tag%
User avatar
Chunjee
Posts: 1418
Joined: 18 Apr 2014, 19:05
Contact:

Re: Getting last element in the array

Post by Chunjee » 28 Feb 2021, 01:56

tmp_last := % tmp.MaxIndex() will give you the length of the array. 8 in this case, the string being split has a lot of spaces.

Its probably best to use .Count() though as sparse arrays won't .MaxIndex() in a predictably.
More info: https://www.autohotkey.com/docs/objects/Object.htm#MinMaxIndex

You access psudoarrays by obj%index%; objects/arrays should be read and written with obj[index]; if you only want the last item then the index would be array.Count(). Since you want the value there and not the index number; array[array.Count()] but array[array.MaxIndex() would work the same.

Code: Select all

username := "ya64ug   ,  tag = 78"
array := StrSplit(username, " ")
msgbox, % array[array.Count()]
; => 78

arrays come with a few methods; one you might find useful is .pop() which returns and removes the last item from the array (modifying it)

Code: Select all

username := "ya64ug   ,  tag = 78"
array := StrSplit(username, " ")
msgbox, % array.pop()
; => 78

;array is now ["ya64ug", "", "", ",", "", "tag", "="]


If you don't want to think about all that; https://biga-ahk.github.io/biga.ahk/#/?id=last shortcuts to the point (and does not modify the array:

Code: Select all

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

username := "ya64ug   ,  tag = 78"
array := StrSplit(username, " ")
msgbox, % A.last(array)
; => 78
Last edited by Chunjee on 28 Feb 2021, 02:07, edited 1 time in total.
Rohwedder
Posts: 7623
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Getting last element in the array

Post by Rohwedder » 28 Feb 2021, 02:02

Hallo,
or as close as possible to your script:

Code: Select all

username := "ya64ug   ,  tag = 78"
MsgBox, %username%
tmp := StrSplit(username, " ")
tmp_last := tmp.MaxIndex()
MsgBox, %tmp_last%
tag := tmp[tmp_last]
MsgBox, %tag%
User avatar
boiler
Posts: 16913
Joined: 21 Dec 2014, 02:44

Re: Getting last element in the array  Topic is solved

Post by boiler » 28 Feb 2021, 02:12

Or your script breaking it up as desired into only 5 elements (wherever there are one or more spaces):

Code: Select all

username := "ya64ug   ,  tag = 78"
MsgBox, %username%
tmp := StrSplit(RegExReplace(username, " +", " "), " ")
tmp_last := tmp.MaxIndex()
MsgBox, %tmp_last%
tag := tmp[tmp_last]
MsgBox, %tag%
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Re: Getting last element in the array

Post by roysubs » 28 Feb 2021, 04:03

All such great and informative answers, thanks so much guys, think I've cracked the "pseudoarray vs new arrays" problems that I've had in Autohotkey up to now with your help (and for the RegExReplace to normalise the multiple spaces, that's made it a lot easier thanks boiler). :thumbup: :D
User avatar
boiler
Posts: 16913
Joined: 21 Dec 2014, 02:44

Re: Getting last element in the array

Post by boiler » 28 Feb 2021, 06:45

In case you just want a fast way to get the last part of your string (without even having to split it into an array):

Code: Select all

username := "ya64ug   ,  tag = 78"
RegExMatch(username, "\S+$", tag)
MsgBox, %tag%
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Re: Getting last element in the array

Post by roysubs » 28 Feb 2021, 08:12

oh wow, that will populate %tag%, that is very nice, I've not used RegExMatch much and didn't think of it's usage in this situation, great to know, ta. :)
So, "\S" is the last space?, then "+" would be, what anything after that? Not really getting how "+" does that.
Ah, no, it's not that: "\S+" would match any spaces, but since we don't pick any others, it will just get the last match, then the "$" is end of line. I don't quite see how it is capturing the "tag" part, but if it works it works, so that's great. :)
User avatar
boiler
Posts: 16913
Joined: 21 Dec 2014, 02:44

Re: Getting last element in the array

Post by boiler » 28 Feb 2021, 08:21

\S means match any non-space character, (\s with a lowercase s matches a space), so with the + and the $, it matches all the non-space characters at the end of the string.
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Re: Getting last element in the array

Post by roysubs » 28 Feb 2021, 08:39

Ah yes, I see it now. Perfect. :)
Post Reply

Return to “Ask for Help (v1)”