This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Elton Fong
over 10 years

Perplexed - Difference between Arrays and Hashes?

I am utterly at a lose on the difference between an array and hashes? Can someone explain these two me in layperson terms?

Thanks a bunch!

Answer 52a6a7b19c4e9d5bca0011e9

52 votes
Best answer

Permalink

Both are collections used to store and retrieve (lookup) data.

You can think of an array as a list. Just a collection of data in some kind of order. You can look up a piece of data by using the number it comes in on the list, like, it’s first in the list, or second… like you have a shopping list and you ask, “OK, what’s the first thing on the list? Milk. What’s the second? Lettuce.” shopping_array[0] would give you "Milk", and shopping_array[1] would give "Lettuce".

You can think of a hash as a collection of pairs of information, and each pair is made of a name and a value. The name is called a key, and it must be unique. You use the key to return the value paired to it. So for a shopping list, it’d be more like a list where your finicky girlfriend wanted to make sure you got the right kind of stuff when you went shopping, and next to “milk” she wrote “low fat”, and next to “fish” it said “salmon”. So milk is the key for “low fat”. You can imagine it would look like a table when she wrote it, and hashes are sometimes called hash tables. You’d look at the list thinking, “What do we want for fish?” and the list would answer “salmon”. So the important thing with a hash isn’t the order of the information, the important thing is what is paired to what. shopping_hash["fish"] gives you "salmon".

Examples

So, a simple array might just contain some days of the week:

days = ["Monday", "Tuesday", "Wednesday"]

A hash could contain numbers followed by their names in Spanish:

dias = { "Monday" => "lunes", "Tuesday" => "martes", "Wednesday" => "mercoles" }

Here the English word would be the key, and the Spanish equivalent would be the value.

Hashes are called dictionaries in Python, and it might be easy to think of them this way, because a dictionary essentially has a key (the word) and a value (the definition). In terms of computer performance, hashes in Ruby are said to be very fast for looking up information compared to arrays.

Lookups:

Arrays: To retrieve data from our array, we can use the index (order) number, starting from 0. So, days[0] will return "Monday", days[1] will return "Tuesday".

Hashes: With a hash you use the key value instead of the index number. So dias["Monday"] returns "lunes", because in the dias hash, "Monday" is the key for the value lunes.

Now, you can store any kind of value in both arrays and hashes, as I understand. And as we’ve seen in the exercises, you can have arrays inside of arrays, and also hashes in arrays, hashes in hashes, and all sorts of other things. The basics, however, are pretty straightforward.

Hopefully I haven’t made any mistakes explaining this. If you try to play with a few of your own in labs.codecademy it should all come together for you.

points
Submitted by Sticky
over 10 years

4 comments

Elton Fong over 10 years

Hi Sticky, Thank you so much for clarifying this up for me. You explanation is so much clearer. So, can we also say that a multidimensional array is also equivalent to a hash in Ruby or am I thinking wrong on this one?

Sticky over 10 years

A multidimensional array is still different from a hash, especially because you’re still calling it by its index number, not by a key.

So for the array multidimensional array m = [ [4, 40], [5, 50] ], if you wanted to find the 40 in your array and you entered m[0], the first index, you’d get [4, 40]. You’d find 40 by entering m[0][1], so the first index of the m array, and the second index of the array stored at m[0].

But for the equivalent hash, m = { 4 => 40, 5 => 50 } you’d find or lookup 40 by going m[4] because 4 is its key. If you tried to do m[0] it wouldn’t work! m[0][1] would also give you “nil”. Hashes can’t be used with index numbers.

Also, Ruby treats hashes and arrays very differently under the hood. Again, hashes are faster. And hashes don’t necessarily store themselves in any given order (except that the pairs are always kept together. If you return an entire hash, the pairs will be together, but the order that the pairs come in might be different. Just now I entered m = { 4 => 40, 5 => 50 } into a Ruby interpreter, and Ruby spit back out, => {5=>50, 4=>40}. Not the same order as I entered, right? Ruby and other languages do this to optimize performance, but if you think about it, it means that there simply is no index number for a given pair in a hash.

Arrays, you access by index. Hashes, you access values by keys.

Makes sense?

Sticky over 10 years

Also to remember, an array inside of an array can have any number of values. You could have this_array =[ [1, 2, 3, 4, 5], [“soap”, “toothpaste”, “towels”] ]. But a key/value pair in a hash will always have two and only two parts (it’s a pair after all), the key and the value (even if that one value is some container itself that has several things inside of it, like a box full of magazines is still one “box of mags”).

Alex J over 10 years

Wow, great explanation, Sticky! Would you mind editing your answer (or, if the 3000 character limit won’t allow it, add a new answer instead) to include the contents of your very long comment? In the comments there’s no code highlighting so it’s very difficult to read.

Answer 52a870d38c1cccf6870022b4

12 votes

Permalink

So, is a multidimensional array the same as a hash?

A multidimensional array is still different from a hash, especially because you’re still calling it by its index number, not by a key.

So for the array multidimensional array m = [ [4, 40], [5, 50] ], if you wanted to find the 40 in your array and you entered m[0], the first index, you’d get [4, 40]. You’d find 40 by entering m[0][1], so the first index of the m array, and the second index of the array stored at m[0].

But for the equivalent hash, m = { 4 => 40, 5 => 50 } you’d find or lookup 40 by going m[4] because 4 is its key. If you tried to do m[0] it wouldn’t work! m[0][1] would also give you "nil". Hashes can’t be used with index numbers.

Also, Ruby treats hashes and arrays very differently under the hood. Again, hashes are faster. And, arrays, you access by index. Hashes, you access values by keys. Makes sense?

Also to remember, an array inside of an array can have any number of values. You could have this_array =[ [1, 2, 3, 4, 5], ["soap", "toothpaste", "towels"] ]. But a key/value pair in a hash will always have two and only two parts (it’s a pair after all), the key and the value (even if that one value is some container itself that has several things inside of it, like a box full of magazines is still one “box of mags”).

points
Submitted by Sticky
over 10 years

2 comments

Elton Fong over 10 years

Got it! Thanks Alex for all the help and clarification!

David sok about 10 years

^ Lol that was Sticky..gotta give credit where credit is due!

Answer 52a767337c82caeb7e000118

2 votes

Permalink

Awesome explanation, @Sticky! I’ve pinned this thread to the top (see the green star in the list), so hopefully more people will stumble upon this question and read you very helpful answer.

Just two minor remarks that I’d like to add to this:

  1. I don’t think you mentioned another important difference: in a Hash every key must be unique, so this code will create a Hash with only one key-value pair, not two (the value gets overwritten on the third line:

    h = Hash.new
    h[:a] = "A"
    h[:a] = "B"
    h.size               #=> 1
    

Arrays, by contrast, allow several copies of the same thing:

    a = [1, 1, 1, 2]
    a.size               #=> 4
  1. In Ruby v1.9 and later, Hashes actually do preserve the order of the pairs. As you can read in the documentation,

    Hashes enumerate their values in the order that the corresponding keys were inserted

so, for example:

    numbers = Hash.new
    numbers[:one]   = 1
    numbers[:two]   = 2
    numbers[:three] = 3
    numbers[:one]   = 999
    puts numbers.values.last        #=> 3

you can rely upon the fact that the last value will be the one you inserted last (3), not the one that was last updated (999).

points
Submitted by Alex J
over 10 years

2 comments

Sticky over 10 years

Thanks Alex! I’m very happy that my comments can be helpful. OK, I’ll update the thread later today.

Sticky over 10 years

I think it was over the word limit, so I entered a new answer below…

Answer 52c420f98c1ccc50e7004588

0 votes

Permalink

Great answers.

points
Submitted by Dan
over 10 years

1 comments

surfer about 10 years

i agree: great sticky and always great Alex thnxxx

Answer 552c028a76b8fe4eda000420

0 votes

Permalink

Can hashes also be called associative arrays or are they different?

points
Submitted by Matt Anderson
about 9 years