Perplexed - Difference between Arrays and Hashes? | Codecademy (2024)

Skip to Content

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

banner

Close banner

points

Perplexed - Difference between Arrays and Hashes? | Codecademy (1)

Submitted by

Elton Fong

almost 11 years

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

Perplexed - Difference between Arrays and Hashes? | Codecademy (2)

Submitted by

Sticky

almost 11 years

4 comments

Perplexed - Difference between Arrays and Hashes? | Codecademy (3) Elton Fong

almost 11 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?

Perplexed - Difference between Arrays and Hashes? | Codecademy (4) Sticky

almost 11 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?

Perplexed - Difference between Arrays and Hashes? | Codecademy (5) Sticky

almost 11 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”).

Perplexed - Difference between Arrays and Hashes? | Codecademy (6) Alex J

almost 11 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

Perplexed - Difference between Arrays and Hashes? | Codecademy (7)

Submitted by

Sticky

almost 11 years

2 comments

Perplexed - Difference between Arrays and Hashes? | Codecademy (8) Elton Fong

over 10 years

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

Perplexed - Difference between Arrays and Hashes? | Codecademy (9) David sok

over 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.newh[: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

Perplexed - Difference between Arrays and Hashes? | Codecademy (10)

Submitted by

Alex J

almost 11 years

2 comments

Perplexed - Difference between Arrays and Hashes? | Codecademy (11) Sticky

almost 11 years

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

Perplexed - Difference between Arrays and Hashes? | Codecademy (12) Sticky

almost 11 years

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

Answer 52c420f98c1ccc50e7004588

votes

Permalink

Great answers.

points

Perplexed - Difference between Arrays and Hashes? | Codecademy (13)

Submitted by

Dan

over 10 years

1 comments

Perplexed - Difference between Arrays and Hashes? | Codecademy (14) surfer

over 10 years

i agree: great sticky and always great Alex thnxxx

Answer 552c028a76b8fe4eda000420

votes

Permalink

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

points

Perplexed - Difference between Arrays and Hashes? | Codecademy (15)

Submitted by

Matt Anderson

over 9 years

Popular free courses

  • Free CourseLearn SQLBeginner friendly,4 Lessons In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Language Fluency
  • Free CourseLearn JavaScriptBeginner friendly,11 Lessons Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Language Fluency
  • Free CourseLearn HTMLBeginner friendly,6 Lessons Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Language Fluency

Explore full catalog

Perplexed - Difference between Arrays and Hashes? | Codecademy (2024)
Top Articles
Most Profitable Events, Weekends, Holidays, and Times of the Year for Airbnb Hosts
Canada Vs Australia
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 6184

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.