Array-related terminology: values vs. elements

Discuss Autohotkey related topics here. Not a place to share code.
Forum rules
Discuss Autohotkey related topics here. Not a place to share code.
john_c
Posts: 368
Joined: 05 May 2017, 13:19

Array-related terminology: values vs. elements

14 Nov 2019, 01:19

Code: Select all

arr := {"aaa": "foo", "bbb": "bar"}
Which terminology is correct: 1 or 2 or some another?

1

* aaa and bbb are the keys.
* foo and bar are the values.
* "aaa": "foo" and "bbb": "bar" are the elements (= key-value pairs).

2

* aaa and bbb are the keys.
* foo and bar are the values (= elements. The word "elements" is just a synonym for "values").
* "aaa": "foo" and "bbb": "bar" are the key-value (= key-element) pairs.

Note that this confusion is not just my own.

Code: Select all

; https://www.autohotkey.com/docs/commands/For.htm
For Key, VALUE in Expression

; https://www.autohotkey.com/docs/misc/Arrays.htm
For Index, ELEMENT in Array
Last edited by john_c on 29 Nov 2019, 01:27, edited 1 time in total.
gregster
Posts: 3914
Joined: 30 Sep 2013, 06:48

Re: Array-related terminology: values vs. elements

14 Nov 2019, 07:49

As I see it:
In a strict sense, each element of an array (a value) has (at minimum) one index or key which allows to identify it.
In a broader sense, you could probably also call each key:value-relationship one element.

Generally, this terminology is not as fixed as some computer scientists and purists want it to be. It often depends on the context in which a word is used - and also when, for example, in which decade, or where, for example at which professors's course or in which book, it was used; or on which programming language you use.

For example, in german, nowadays with the interwebs and stuff we often call an array an "Array", but at least many programmers, computer scientists and "older" (ähem) people like me still remember and sometimes use the words Datenfeld or Feldvariable or just Feld for an array (among a number of other possible synonyms, depending on your personal programming history and background).

But sometimes, Datenfeld or Feld are also used to describe the single elements/values of an array/Feld (but most people might then use another term to describe the array itself, to be able to differentiate).

So, there seems to be no clear answer to your question. Context matters.
User avatar
Xtra
Posts: 1636
Joined: 02 Oct 2015, 12:15

Re: Array-related terminology: values vs. elements

14 Nov 2019, 16:19

Code: Select all

for Cat, Forum in ahk := {E: "E", A: "S", S: "", Y: "Y"}
    vote := Forum vote
MsgBox, 4096, Cat Forum, % vote
john_c
Posts: 368
Joined: 05 May 2017, 13:19

Re: Array-related terminology: values vs. elements

14 Nov 2019, 16:22

Yes, I like cats, but I'm not sure what exactly you mean...
User avatar
boiler
Posts: 3915
Joined: 21 Dec 2014, 02:44

Re: Array-related terminology: values vs. elements

14 Nov 2019, 16:44

This is a good way to keep your key-element pairs together.
Image
john_c
Posts: 368
Joined: 05 May 2017, 13:19

Re: Array-related terminology: values vs. elements

15 Nov 2019, 14:54

Thanks, gregster. Also, some opinions are now posted here: https://softwareengineering.stackexchange.com/questions/401140

From the link above:

Code: Select all

string colour[] = { "red", "blue", "red" }     // language with classical arrays
Array := {"title": "Doe", "author": "Doe"}     // language with associative arrays
* If the elements are immutable, the combination key-value (or index-value) is a better candidate for being an element than the value alone. 0-red, 1-blue, 2-red are distinct elements.
* If the elements are mutable, the combination key-value no longer defines correctly the element. In this semantic, if I change colour[1] from "red" to "green", it's still the same element, but with a new value. Here, the element is the unique object associated with the key/index.

Code: Select all

Array := {"a": "aaa", "b": "bbb", "c": "aaa"}

If the elements are immutable:
Keys:     a, b, c
Values:   aaa, bbb
Elements: a: aaa, b: bbb, c: aaa

If the elements are mutable:
Keys:     a, b, c
Values:   aaa, bbb
Elements: aaa, bbb, aaa
Last edited by john_c on 29 Nov 2019, 00:36, edited 3 times in total.
john_c
Posts: 368
Joined: 05 May 2017, 13:19

Re: Array-related terminology: values vs. elements

28 Nov 2019, 03:44

As I understand, AHK array elements are mutable:

Code: Select all

Array := {"a": "aaa", "b": "bbb", "c": "ccc"}
MsgBox,,, % Array["b"]
Array["b"] := "xxx"
MsgBox,,, % Array["b"]
Since the array elements are mutable,

Code: Select all

Array := {"a": "aaa", "b": "bbb", "c": "aaa"}
; Keys:     a, b, c
; Values:   aaa, bbb
; Elements: aaa, bbb, aaa

; Correct.
For Key, Value In Array
    ; That is, for each key-value pair.

; Not correct.
For Key, Element In Array
    ; That is, for each key-element pair.
    ;
    ; The word "element" is misused.
    ; Elements (aaa, bbb, aaa) are _already_ distinctive 
    ; things. Thus, "key-element pair" is a tautology.
The names in this function looks correct to me:

Code: Select all

RemoveValueFromArray(Array, ValueToRemove) {
    For Index, Value In Array
        If (Value = ValueToRemove)
            Array.RemoveAt(Index)
}

Array := ["foo", "bar", "baz", "bar"]
RemoveValueFromArray(Array, "bar") ; => ["foo", "baz"]
Last edited by john_c on 29 Nov 2019, 01:05, edited 5 times in total.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Array-related terminology: values vs. elements

29 Nov 2019, 01:03

From reading that Stack Overflow link, it looks like 'element' may be commonly used in a specific PHP context, but that it is unusual to use 'element' in that way in other programming languages.

I think of it like this (although perhaps others have a different interpretation):

folder/file/file name/data
map/key/key name/value
array/key/index/value (also: array/key/key name/value)

and, strictly speaking, 'key-value pair' should be 'key name-value pair',
and you could also have 'index-value pair'

My folder has 4 files.
My folder has 4 elements. [unusual]
The files have file names: a.txt, b.txt, c.txt, d.txt.
The data in each file is 10 bytes in size. [also, simply: each file is 10 bytes in size.]

My map has 4 keys.
My map has 4 elements. [OK, but not particularly common?]
My map has 4 values. [unusual?]
The keys have key names: A, B, C, D.
The value in each key is a string/number/object.

My array has 4 keys.
My array has 4 elements. [OK, but not particularly common?]
My array has 4 values. [unusual?] [maybe it's more common to use 'values' for a linear array with no gaps, cf. a map]
The keys have indexes: 1, 2, 3, 4.
The value in each key is a string/number/object.
Last edited by jeeswg on 29 Nov 2019, 01:33, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
john_c
Posts: 368
Joined: 05 May 2017, 13:19

Re: Array-related terminology: values vs. elements

29 Nov 2019, 01:15

@jeeswg Stack Overflow link is not the most interesting in this thread. There is Software Engineering link. My posts here are based on Christophe's answer.

Edit: I removed Stack Overflow link. It is now excessive.
Last edited by john_c on 29 Nov 2019, 01:30, edited 1 time in total.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Array-related terminology: values vs. elements

29 Nov 2019, 01:29

I like Christophe's answer, or rather, he hints at the right questions.
In simple terms, an element is a thing. Is the key name and value, one thing, or, is the value, one thing (with the key name being largely irrelevant).
I think it's better to say that the key name and value are one thing.

If we go from ["a", "b"] to ["b", "a"]. I'd say we changed the values for elements 1 and 2, rather than say we gave elements "a" and "b" new key names.
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 “General Discussion”

Who is online

Users browsing this forum: No registered users and 25 guests