Strings as arrays

Propose new features and changes
RFM
Posts: 28
Joined: 21 May 2016, 12:50

Strings as arrays

17 Feb 2017, 09:31

One of my favorite languages treated strings as an array and could do the following (the "=" in that language is ":=" in AHK and "!" is the rest of line comment initiator):

myString = "This is my test string."
Char3 = myString[3] ! Char3 = "i"
Char7 = myString[7] ! Char7 = "s"
Chars3to7 = myString[3 : 7] ! Chars3to7 = "is is"
It did not do the following, which I would have liked:
CharsStartingAt3size5 := myString[3 # 5] ! CharsStartingAt3size5 = "is is"
Of course, you usually used variables in place of the numbers.

You could also do this:
myString[3] = "a" ! myString = "Thas is my test string."
myString[4] = "t" ! myString = "That is my test string."
myString[6 : 10] = "was a" ! myString = "That was a test string."
It did not do the following, which I would have liked:
myString[3 # 8] = "ere's my" ! myString = "There's my test string."

The above was a tremendous time saving syntax. It has the SubStr function as well to access characters within the string, and, of course, you could concatenate strings together to change the string, but the above syntax was so much simpler and quicker.

Sorry, though I have time to make a suggestion, I don't have time for an ongoing conversation about my suggestion!
This is my one and only post to this thread.
This is the "Wish List", this is my wish.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Strings as arrays

17 Feb 2017, 09:34

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Strings as arrays

17 Feb 2017, 10:50

Guest wrote:Just for reference:

Sinkfaze comes pretty close to this wish https://autohotkey.com/board/topic/5456 ... ipulation/
nice, I didn't know about that PythonStr library that sinkfaze created

Guest

Re: Strings as arrays

17 Feb 2017, 11:08

Don't forget to checkout the link in the post beneath sinkfaze by jethrow https://autohotkey.com/board/topic/4670 ... ies-ahk-l/
just me
Posts: 9456
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Strings as arrays

18 Feb 2017, 12:06

StrSplit() can be used to convert a string into an array of characters. But it won't permit myString[3 : 7].
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Strings as arrays

18 Feb 2017, 18:21

It's a minor convenience (syntax sugar) over simply using SubStr, but SubStr is less obscure for the uninitiated.

Code: Select all

myString := "This is my test string."
Char3 := myString[3] ; Char3 := "i"
Char7 := myString[7] ; Char7 := "s"
Chars3to7 := myString[3,,7] ; Chars3to7 := "is is"

CharsStartingAt3size5 := myString[3, 5] ; CharsStartingAt3size5 = "is is"

ListVars
Pause

StrIndex(s, i, n:="", j:="") {
    static _ := ("".base.__Get := Func("StrIndex"))
    return n="" ? j="" ? SubStr(s,i,1) : SubStr(s,i,j-i+1) : SubStr(s,i,n)
}
TAC109
Posts: 1112
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: Strings as arrays

18 Feb 2017, 18:36

Wow!

What just happened?!
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Strings as arrays

19 Feb 2017, 11:46

Interesting ... I never really played around this. :D
Edit*

Code: Select all

; ##############################################################################################################################################################
; EXAMPLE
; ##############################################################################################################################################################
; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
; NEW
; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Str := "This is my test string."


; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
; GET
; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MsgBox % Str																				; This is my test string.
MsgBox % Str[]																			; This is my test string.
MsgBox % Str[7]																			; s
MsgBox % Str[17, 7]																	; string.
MsgBox % Str[0]																			; 23 (length)
MsgBox % Str.Length																	; 23 (length)


; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
; CALL
; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MsgBox % Str.Compare("This is my test string.")						; 1 (TRUE, CaseSensitive = 0)
MsgBox % Str.Compare("This is my test strinG.")						; 1 (TRUE, CaseSensitive = 0)
MsgBox % Str.Compare("This is my test strinG.", 1)					; 0 (FALSE, CaseSensitive = 1)

MsgBox % Str.InStr("my")															; 9


; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
; SET
; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Str[] := "This is my new test string."
MsgBox % Str 																			; This is my new test string.
Str[3] := 1
MsgBox % Str																				; Th1s is my new test string.
Str[12, 3] := "NEW"
MsgBox % Str																				; Th1s is my NEW test string.




















; ##############################################################################################################################################################
; DEFAULT BASE OBJECT
; ##############################################################################################################################################################
__DefaultBaseObject() {
	static _ := ("".base.base := {__Get: Func("__DefaultBaseObject__Get"), __Set: Func("__DefaultBaseObject__Set"), __Call: Func("__DefaultBaseObject__Call")})
}


; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
; GET
; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__DefaultBaseObject__Get(Var, Param*) {
	;X := Var[]
	if (!Param.MaxIndex())
		return (Var)
	
	;X := Var[0]	|	X := Var.Length
	else if (Param[1] = 0 || Param[1] = "Length")
		return (StrLen(Var))
	
	;X := Var[X], / X != 0
	return (Param[2] = "" ? (Param[3] = "" ? SubStr(Var, Param[1], 1) : SubStr(Var, Param[1], Param[3] - Param[1] + 1)) : SubStr(Var, Param[1], Param[2]))
}


; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
; SET
; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__DefaultBaseObject__Set(ByRef Var, Param*) {
	;Var[] := X
	if (Param.MaxIndex() = 1)
		Var := Param[1]
	
	;Var[X1] := X2
	else if (Param.MaxIndex() = 2)
		Var := SubStr(Var, 1, Param[1] - 1) . Param[2] . SubStr(Var, Param[1] + 1)
	
	;Var[X1, X2] := X3
	else if (Param.MaxIndex() = 3)
		Var := SubStr(Var, 1, Param[1] - 1) . Param[3] . SubStr(Var, Param[1] + Param[2])
	
	return (0)
}


; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
; CALL
; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__DefaultBaseObject__Call(Var, FuncName, Param*) {
	;bool Compare(Var, CaseSensitive?)
	if (FuncName = "Compare")
		return (Param[2] ? (Var == Param[1]) : (Var = Param[1]))
	
	;int InStr(Needle, CaseSensitive?, StartingPos=1, Occurrence=1)
	else if (FuncName = "InStr")
		return InStr(Var, Param[1], !!Param[2], Param[3] = "" ? 1 : Param[3], Param[4] = "" ? 1 : Param[4])
}
https://github.com/flipeador/AutoHotkey ... Object.ahk
I could replace a character doing this:

Code: Select all

Str := "This is my test string."

Pos			:= 3
Replace	:= "1"
NumPut(Ord(Replace), Str, (Pos - 1) * 2, "Uchar")

MsgBox % Str
But not work for all (someone could explain?):

Code: Select all

Str := "This is my test string."

Pos			:= 3
Replace	:= "•"
NumPut(Ord(Replace), Str, (Pos - 1) * 2, "Uchar")

MsgBox % Str
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Strings as arrays

19 Feb 2017, 11:52

Code: Select all

Str := "This is my test string."

Pos			:= 3
Replace	:= "•"
NumPut(Ord(Replace), Str, (Pos - 1) * 2, A_IsUnicode?"USHORT":"UCHAR")

MsgBox % Str
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Strings as arrays

19 Feb 2017, 11:53

I normally don't like youtube videos but noone explains it better than this guy does:
Recommends AHK Studio
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Strings as arrays

19 Feb 2017, 12:01

HotKeyIt wrote:

Code: Select all

Str := "This is my test string."

Pos			:= 3
Replace	:= "•"
NumPut(Ord(Replace), Str, (Pos - 1) * 2, A_IsUnicode?"USHORT":"UCHAR")

MsgBox % Str
I must pay more attention :roll: :facepalm: .
Thanks HotKeyIt.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Strings as arrays

19 Feb 2017, 12:42

Please don't ignore me :cry:
Recommends AHK Studio
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Strings as arrays

19 Feb 2017, 13:18

nnnik wrote:Please don't ignore me :cry:
Haha, I have not ignored you, it seems that the video you have posted does not appear to me, but I have already extracted the URL. I had to activate the subtitles to understand what he is saying (my English is medium).
I've seen other vídeos too. Thanks nnnik ;) .
User avatar
niczoom
Posts: 78
Joined: 09 Mar 2016, 22:17

Re: Strings as arrays

14 Jun 2019, 00:11

Great video! I didn't know much about UTF8, now I do.
[AHK] 1.1.23.05 x32 Unicode
[WIN] 10 Pro x64 Version 5111 (Build 10586.218)

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 60 guests