How to initialize static arrays to zero Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
paik1002
Posts: 311
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] [Download] GeSHi © Codebox Plus

abc(max_index)
{
static Array := []
return
}
Helgef
Posts: 3147
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] [Expand] [Download] GeSHi © Codebox Plus


Good luck.


Edit: For the record, although the above works, this is more correct,

Code: [Select all] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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: 311
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] [Expand] [Download] GeSHi © Codebox Plus


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] [Download] GeSHi © Codebox Plus

static Var := 0
Helgef
Posts: 3147
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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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”

Who is online

Users browsing this forum: jmcall10, S7ewie, swagfag and 20 guests