What does "linear array" mean? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
john_c
Posts: 480
Joined: 05 May 2017, 13:19

What does "linear array" mean?

15 Apr 2018, 04:31

From the docs:
In AutoHotkey v1.x, simple arrays and associative arrays are the same thing. However, treating [] as a simple linear array helps to keep its role clear, and improves the chance of your script working with a future version of AutoHotkey, which might differentiate between simple arrays and associative arrays.
What does "linear array" mean?

Code: Select all

simpleArray := ["Item 1", "Item 2"]
associativeArray := {"Key A": "Value A", "Key B": "Value B"}
User avatar
Masonjar13
Posts: 1530
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: What does "linear array" mean?

15 Apr 2018, 13:12

Linear means "arranged in a straight path." So, a linear array would be an array in which each subsequent key adheres to being linear, such as numerically. When you do array:=["item 1","item 2"], this is creating an associative array/object where each key is increasing from 1.

Code: Select all

simpleArray := ["Item 1", "Item 2"]
associativeArray := {"Key A": "Value A", "Key B": "Value B"}

for i,a in simpleArray
    msgbox % "key: " . i . "`nvalue: " . a

for i,a in associativeArray
    msgbox % "key: " . i . "`nvalue: " . a
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: What does "linear array" mean?

15 Apr 2018, 13:18

- AFAIK a linear array is an array that only uses integers as key names: e.g. 1, 2, 3.
- One thing I'm not sure about is if gaps are allowed: e.g. 1, 3, 5.
- Another thing I'm not sure about is if it can contain negative integers or zero e.g. -1, 0, 1.
- Note: in AutoHotkey, these are equivalent:

Code: Select all

oArray := ["a", "b", "c"]
oArray := {1:"a", 2:"b", 3:"c"}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Noesis
Posts: 299
Joined: 26 Apr 2014, 07:57

Re: What does "linear array" mean?  Topic is solved

16 Apr 2018, 00:30

I actually think the docs are being slightly misinterpreted here. I tend to think that it is NOT saying there is a "linear array" vs "associative array", instead the linear aspect is trying to describe a "simple array", i.e. it says "simple linear array", linear being the adjective used to describe the nature of a simple array.

So given this, In my mind, there is only "Simple Array" vs "Associative Array". A simple array, is linear, and contains contiguous integer index numbers without gaps which increases by 1.

It just happens to also be true that a simple array is the same as an associative array, under the hood since ahk doesn't really have strict data types at all so it's just an "array" which is incidentally just an "object", as far as ahk is concerned.

If you start thinking there is some new "linear array" type, then it really is just opening a can of worms. Indexes of a,b,c,d are they linear ?, what about indices of 1, 1.5, 2, 2.5, 3, 3.5, etc. You could argue that they both are linear (the first example is especially dubious, as I could see how you could argue it is, but it could also easily be argued it isn't), but in either case they are not simple arrays, and the same would apply to indices of 2,4,6,8, which is linear but not really IMO a simple array due to the gaps, and the difficulty you will ultimately have maintaining it with records being inserted, added and removed, compared to a simple array which has built in methods for handling these operations.

To me a simple array can be viewed by using a "Loop, % simplearray.maxIndex()" and using A_Index for the indices, if it can't it's no longer simple, and thus more an associative array. And I'd say if you have stringed Integers, (even if linear, and contiguous) it's more on the associative array side of perceived array types.
just me
Posts: 7754
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: What does "linear array" mean?

16 Apr 2018, 02:48

As an example: Data Structures In Java - Linear Array
A linear array is a plain data structure which doesn't need to store any "key". The array elements are referred by index/position.
Currently, even AHK's 'simple arrays' are associative arrays referred by a key stored in the object.
john_c
Posts: 480
Joined: 05 May 2017, 13:19

Re: What does "linear array" mean?

11 Sep 2019, 17:19

jeeswg wrote:
15 Apr 2018, 13:18
- One thing I'm not sure about is if gaps are allowed: e.g. 1, 3, 5.
No, gaps are not allowed. With gaps, it will be an associative array, not simple.
Associative arrays can be sparsely populated - that is, {1:"a",1000:"b"} contains only two key-value pairs, not 1000.
- https://www.autohotkey.com/docs/Objects.htm
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: What does "linear array" mean?

24 Oct 2019, 18:14

- So, I might define a linear array like so:
- A linear array is a collection of key-value pairs that only allows integers as key names: e.g. 1, 2, 3.
- The typical minimum key name is 0 or 1 (0-based arrays or 1-based arrays).
- AHK v1 arrays are 1-based when using the [] syntax, although the AHK v1 basic object allows 0 and negative integers to be the minimum.
- The typical maximum key name is some large power of 2 minus 1.
- Depending on the specification for the array, or how the array variable is defined: the maximum key name is fixed at loadtime, or, the array size can be changed at runtime.
- Gaps are allowed: i.e. if by gaps you mean: keys that have no value.

- Associative arrays are similar, although there is more flexibility in what you can have as key names e.g. strings.

- Here is an interesting discussion of sparse arrays v. dense arrays:
javascript - What exactly is a dense array? - Stack Overflow
https://stackoverflow.com/questions/39030196/what-exactly-is-a-dense-array

- I would also check this link that just me posted:
Data Structures In Java - Linear Array
https://www.c-sharpcorner.com/article/data-structure-linear-array/

- Going over various sources, it's actually very difficult to say anything that doesn't have caveats.
- E.g. what is dense, what is a gap, is a linear array strictly: an area of memory with items of a fixed size?
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”

Who is online

Users browsing this forum: denysy1, Google [Bot], lblb, wineguy and 62 guests