1

I feel like I'm missing some basic understanding... hopefully this isn't considered too broad or subjective, as I'm not positive which Stack Exchange site to post to.

VBA's associative array is the Dictionary. My extremely gross understanding is that a Dictionary is just a multi-dimensional array; to find a value in the matrix, you'd still have to iterate and find a matching value in the first row of the matrix, which would then be used to output values in the nth row of the same column within the matrix.

If the above is in any way correct, then how is Dictionary more efficient than a standard multi-dimensional array?

Egalth
  • 962
  • 9
  • 22
Cyril
  • 6,448
  • 1
  • 18
  • 31
  • In which sense "more efficient"? – Egalth Nov 26 '18 at 16:28
  • @Egalth I guess that in itself could be a whole question. I have been told that finding data (like a phone book by last name) using an associative array, rather than a standard array is faster to output the related information (phone number for person x). In terms of efficiency, I believe I would be talking about speed, rather than ease of writing the code. I do not know enough about dictionaries to speak to the amount of terms that exist in the IDE to make that quicker to write. – Cyril Nov 26 '18 at 16:31
  • A dictionary is a colleciton of key-value pairs, so in that sense it's a 2-dimensional structure, whereas an array can have more than 2 dimensions. But I guess you could just make the dictionary value a collection of arrays so that it could be used to model more than 2 dimensions, which leads back to your question. Another difference is of course that keys must be unique in a dictionary. Curious to see if you get any more elaborate answer. – Egalth Nov 26 '18 at 16:45
  • Slightly off-topic, but I guess you might find this reference useful, a comparison of dictionaries vs. collections: https://stackoverflow.com/a/32479872/5457466 – Egalth Nov 26 '18 at 16:46
  • 2
    A `Dictionary` is ***not*** a multi-dimensional array - it hashes the keys for look-ups. The "efficiency" of using a `Dictionary` will scale with the number of elements. – Comintern Nov 26 '18 at 16:51
  • @Comintern, wouldn't it be accurate to say that a dictionary is a two-dimensional array with a hash function, so that dictionaries could be considered a subset of multi-dimensional arrays? It's synonymous with "associative array", after all. – Egalth Nov 26 '18 at 17:16
  • @Egalth [No, it wouldn't be accurate to say that.](https://en.wikipedia.org/wiki/Hash_table) – Comintern Nov 26 '18 at 17:21
  • @Comintern, so an associative array is not an array? – Egalth Nov 26 '18 at 17:26
  • 1
    @Egalth No, it's not (necessarily) a *two dimensional* array. – Comintern Nov 26 '18 at 17:29
  • @Comintern, but the dictionary is (by definition) a collection of 2-tuples with certain properties... – Egalth Nov 26 '18 at 17:32
  • 2
    @Egalth Again, no, it isn't. I'd suggest reading the [Wikipedia page](https://en.wikipedia.org/wiki/Hash_table) I linked before or [the SO answer](https://stackoverflow.com/q/730620/1188513) linked below. You're confusing a data structure with an implementation. Even if we assume that it is "a collection of 2-tuples with certain properties", that doesn't mean it's ***stored*** as an array of 2-tuples. – Comintern Nov 26 '18 at 17:41
  • @Comintern, thanks for clarifying that it relates to how data is stored; I already read both of those links you provided, and that part is not obvious. – Egalth Nov 26 '18 at 17:49

2 Answers2

2

to find a value in the matrix, you'd still have to iterate and find a matching value in the first row of the matrix, which would then be used to output values in the nth row of the same column within the matrix.

That's not how dictionaries work.

Dictionary lookups are hash lookups (keys must be unique), making them roughly O(1), whereas iterating the first row of the matrix as you describe would be O(n)... which means the more items you're looking at, the more advantageous a dictionary is vs. an array... assuming you're not iterating the keys (i.e. assuming you're retrieving items by key).

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • Mat's Mug explains it again... thank you! The hash lookup has me confused, but gives me something else to look into for better understanding. Is the hash-table essentially a dictionary with all values as variant, where the dictionary requires you specify the dimension for each value? Sorry for the secondary question... – Cyril Nov 26 '18 at 17:09
  • s.o.b.... look and you shall find. thanks again; that was not showing up when i was googling after seeing your answer. That post gave me... basically every answer that i might be trying to ask. Very much appreciated! – Cyril Nov 26 '18 at 17:17
  • @MathieuGuindon, would it be accurate to say that a dictionary is a special type of array? – Egalth Nov 26 '18 at 17:32
  • @Egalth not any more than a `Stack` is some kind of array. Or a `Queue`. Or any other data structure. Not everything is an array. – Mathieu Guindon Nov 26 '18 at 17:41
  • Ok, makes sense. But after all, a dictionary can also be called an "associative array"... The link you provided had a wonderful non-technical explanation, thanks. But it's a bit tricky to distinguish the fundamentals from the semantics. – Egalth Nov 26 '18 at 17:44
0

Use Bounds Like: Lbound(array) to Ubound (array) it will go through via each cells in row wise

Manoj Babu
  • 31
  • 2