Better understanding of pseudo-arrays Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Better understanding of pseudo-arrays

20 Feb 2018, 04:48

There are 2 examples here. The 1st works as expected. The 2nd works a bit strange for me - to take the 1st item (foo) it's need to ask for second item (%pseudoArray2%).

Please, give me some explanation about how it works.

This works as expected:

Code: Select all

pseudoArray := strSplit("foo bar baz", " ")
firstItem := pseudoArray.1 ; Access the 1st item in the pseudo-array
msgBox, %firstItem%
This - not:

Code: Select all

inputString = "foo bar baz"
stringSplit, pseudoArray, inputString, " "
; firstItem = %pseudoArray1% ; msgBox will be empty
; firstItem = %pseudoArray2% ; msgBox will be foo
msgBox, %firstItem%
gregster
Posts: 8988
Joined: 30 Sep 2013, 06:48

Re: Better understanding of pseudo-arrays  Topic is solved

20 Feb 2018, 05:06

First of all, StrSplit results in an Array, not an PseudoArray (compare docs). You are actually accessing the array with object syntax (an array is an object)

In the second example, you are suffering syntax problems - you are defining inputString as a string including the "s. You can check with msgbox % inputstring

Either do inputString = foo bar baz instead ("legacy" method for backwards compatibility with older AHK versions), or, better inputString := "foo bar baz"
(notice the : ) - then you will put the string foo bar baz as contents of the variable.
The latter ("expression") syntax is the preferred method for variable assigments and rather future-proof for AHK v2, afaik. See https://autohotkey.com/docs/Variables.htm

That said, you are missing the syntax of StringSplit:
Delimiters contains one or more characters (case sensitive), each of which is used to determine where the boundaries between substrings occur in InputVar. Since the delimiter characters are not considered to be part of the substrings themselves, they are never copied into OutputArray.
Thus, you are taking literal " and space as delimiters ( which will be excluded from the resulting strings).

Instead do this: For a space character, better use the built-in AHK variable A_space:

Code: Select all

inputString := "foo bar baz"
msgbox % inputstring
stringSplit, pseudoArray, inputString, %A_Space%
 firstItem = %pseudoArray1% 
; firstItem = %pseudoArray2% 
msgBox, %firstItem%
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Better understanding of pseudo-arrays

20 Feb 2018, 05:22

gregster wrote: ...
Thank you very much.

The syntax error you mentioned crepted in my code due to my inattention. Very stupid error, I agree.

It's very interesting for me that StrSplit results as object-based array. However, as I understand StringSplit results as pseudo-array?
gregster
Posts: 8988
Joined: 30 Sep 2013, 06:48

Re: Better understanding of pseudo-arrays

20 Feb 2018, 05:27

Yes, StringSplit results in a pseudo-array. That has historical reasons (and has been kept for backwards compatibility).
The function version StrSplit() is much newer. The "old" command "StringSplit" was introduced back in the day when AHK didn't support real arrays (and other objects).
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Better understanding of pseudo-arrays

20 Feb 2018, 05:31

gregster wrote:...
Very useful, thank you again.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Better understanding of pseudo-arrays

20 Feb 2018, 13:39

Keeping it simple:

Code: Select all

q:: ;StrSplit v. StringSplit
vText := "a,b,c"

oArray := StrSplit(vText, ",")
MsgBox, % oArray.Length() " " oArray.1 " " oArray.2 " " oArray.3

StringSplit, vPseudoArray, vText, % ","
MsgBox, % vPseudoArray0 " " vPseudoArray1 " " vPseudoArray2 " " vPseudoArray3

vIndex := 2
MsgBox, % oArray[vIndex]
MsgBox, % vPseudoArray%vIndex%
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: gongnl, jaka1, Rohwedder and 260 guests