What’s The Difference Between an Array and a Hash?

Hash != Array

When you are first learning how to code you will be introduced to all sort of stuff. At first it’s going to be how to configure your computer, then maybe basic syntax. After that though, you will be introduced to the most fundamental of building blocks. Operators, Booleans, Strings, Loops, Hashes and Arrays.

Today I want to take a quick moment to briefly cover the key distinctions between an Arrays and Hashes. For those unfamiliar with the two, it can almost appear as if any differences are purely stylistic – this is not the case.

In short, an Array is an ordered list of items/elements whereas a Hash stores data by associated keys. This is the main distinction between the two that should be made at the outset. Let’s use a quick example to highlight the differences and showcase when and where to use one over the other.

 

Scenario

We want to group together a list of test grades from students and from that list, output the average class score:

What do you think would work best here? An Array or a Hash? Why don’t we start by asking what we will be doing with this data.

  • Do we need to make sure each test score is tied to a specific student for the purposes of creating a class average?

No, we merely need a convenient way of collecting test scores and from that collection we will calculate an average. In this case, an Array would work best based on the requirements of the program.

We could do something like the following:

Screenshot at Apr 07 23-05-02.png

As you can see, we have an array called students which holds an assortment of student grades. In this example, all we care about is grouping the data together, we don’t need to create associations with key/value pairs.

We then iterate through student_grades and add each element to our variable, test_scores_sum. From there, we create a variable average_score which calculates the average grade based on the data provided. It is also important to note that structuring the logic in this way will allow us to account for future alterations to the student_grades array. If we decide to add more tests, average_score will continue to be accurate because we are dividing the sum by the amount of elements inside of our array.

 

Scenario

We want to group together a bunch of information about a specific person and be able to access that data based on the attributes of that person

For this scenario we should use a hash for reasons that you will see shortly. What types of attributes should we track? (Let’s limit it to 4):

  • Hair Color
  • Height
  • Eye Color
  • Name

Great, let’s get started!

fewf23.png

So in this example we are defining a variable person that is going to hold various attribute values (name, height, hair color and eye color). The reason we chose to use a hash over an array for this example is because of the nature of the data being stored.

Hashes are great for grouping similar information that needs to be labeled. In the case of our grades example, we didn’t need to worry about labeling the tests, because all we were concerned with was using them to achieve an average grade. In this case, however, we actually want to retrieve this data in a way where we preserve it as it exists in the hash.

It would make sense to use a hash in this case because of how we are grouping the information. If our codebase expanded to accommodate another person, we may want to refactor our code to change person to the name of that person and then remove the key of name. We could then throw all of those hashes inside of an array and if needed, iterate through that array to arrive at our person hashes!

Check out these resources to get a better grasp on the difference between hashes and arrays:

 

 

Submit a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s