How to initialize static arrays to zero Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
paik1002
Posts: 355
Joined: 28 Nov 2015, 02:45

How to initialize static arrays to zero

22 Apr 2017, 05:14

Asumming that the size of the array is known, e.g. max_index, how do I initialize static arrays to zero (or some other value)?

Code: Select all

abc(max_index)
{
   static Array := []
   return
}
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to initialize static arrays to zero  Topic is solved

22 Apr 2017, 08:05

There are many ways to do this, this is one suggestion, test code and brief instructions in code

Code: Select all

arr:=abc()
for k, v in arr
	str.=v " " 
MsgBox, % str

abc()
{
	static max_index:=5		; How many.
	static fill:="0"		; What to fill with, note, single char.
	static Array := fillArr(fill,max_index)
	return array
}

fillArr(char,n){
	; Returns an array of length n whose elements consist of on single symbol: char
	; 0<=ord(char)<=255
	static enc:=A_IsUnicode?"UTF-8":""
	VarSetCapacity(str,n,ord(char))
	return StrSplit(StrGet(&str,n,enc))
}
Good luck.
Edit: For the record, although the above works, this is more correct,

Code: Select all

fillArr(char,n){
	; Returns an array of length n whose elements consist of on single symbol: char
	; 0<=asc(char)<=255
	VarSetCapacity(str,n,asc(char))
	return StrSplit(StrGet(&str,n,""))
}
and this is an extension, which allows for an arbitrary string instead of just one character,

Code: Select all

fillArr(str,n,del:="`n"){
	; Returns an array of length n whose elements consist of the string str.
	VarSetCapacity(var,n*strlen(n+strlen(del))*(A_IsUnicode?2:1),1)
	s:=StrSplit(StrReplace(StrGet(&var,n,""),chr(1),str . del,,n),del), s.pop()	
	return s 
}
Edit: Fixed awkward mistake.
Last edited by Helgef on 11 May 2017, 18:01, edited 2 times in total.
paik1002
Posts: 355
Joined: 28 Nov 2015, 02:45

Re: How to initialize static arrays to zero

22 Apr 2017, 09:48

Helgef wrote:There are many ways to do this, this is one suggestion, test code and brief instructions in code

Code: Select all

arr:=abc()
for k, v in arr
	str.=v " " 
MsgBox, % str

abc()
{
	static max_index:=5		; How many.
	static fill:="0"		; What to fill with, note, single char.
	static Array := fillArr(fill,max_index)
	return array
}

fillArr(char,n){
	; Returns an array of length n whose elements consist of on single symbol: char
	; 0<=ord(char)<=255
	static enc:=A_IsUnicode?"UTF-8":""
	VarSetCapacity(str,n,ord(char))
	return StrSplit(StrGet(&str,n,enc))
}
Good luck.

Thank you.
FYI, I was struggling a while with the manual and thought that I had perhaps missed a simpler solution, noting that static (or global) variables are very elegant and simple to initialize, but I guess it means that AHK does not support such a solution for arrays afterall.

Code: Select all

static Var := 0
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to initialize static arrays to zero

22 Apr 2017, 11:20

paik1002 wrote: FYI, I was struggling a while with the manual and thought that I had perhaps missed a simpler solution, noting that static (or global) variables are very elegant and simple to initialize, but I guess it means that AHK does not support such a solution for arrays afterall.

Code: Select all

static Var := 0
I'm not sure I get your point. There are no built-in functions for filling arrays, but you can still initialise static arrays, either by making a custom function that returns an array (my previous example) on the desired form, or hardcode it,

Code: Select all

f(){
	static arr:=[0,0,0,0,0]	; This is fine for shorter arrays, but gets tedious for longer arrays.
}
Cheers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 326 guests