Coder Social home page Coder Social logo

programming-univbasics-nds-hash-of-arrays-lab-sfo-web-010620's Introduction

Hash of Arrays

Learning Goals

  1. Recognize vocabulary term: "hash of arrays"
  2. Create a Hash of Arrays
  3. Read scalar data from a Hash of Arrays
  4. Modify scalar data in a Hash of Arrays

Introduction

The final "basic" nested structure is the Hash of Arrays.

Key Image: Result Set

The key image is to think of a result set. You might think of it as a weather report for a day where you sample different measurements multiple times per day. You might track :temperature at midnight, noon, and 6:00 p.m.; :rainfall_level at midnight, noon, and 6:00pm; and :humidity at midnight, noon, and 6:00pm. Those sample results would go inside of Arrays that are accessible via the keys :temperature, :rainfall_level, and :humidity.

Recognize Vocabulary Term: "Hash of Arrays"

"Hash of Arrays" is an infrequently used term. As you get more experienced with Ruby, it's typical to merely know that a Hash's key might point to scalar values (1.0, "Smith") or to an Array. While we're starting out with learning Ruby, though, let's briefly take time to acknowledge that this basic NDS exists.

Create a Hash of Arrays

It's most common to create Hash of Arrays in the "literal" format. We'll build on our weather example.

daily_weather = {
  temperature: [75, 80, 72],
  precipitation: [0.0, 0.01, 0.03]
  wind_velocity: [4, 3, 2]
  barometric_pressure: [30.32, 30.30, 30.20]
}

Read Scalar Data from a Hash of Arrays

To read data from a Hash of Arrays we provide:

  1. A key name
  2. An index
daily_weather = {
  temperature: [75, 80, 72],
  precipitation: [0.0, 0.01, 0.03]
  wind_velocity: [4, 3, 2]
  barometric_pressure: [30.32, 30.30, 30.20]
}

# Addition
daily_weather[:temperature][2] #=> 72

# Access the whole Array
daily_weather[:temperature] #=> [75, 80, 72]

Modify scalar data in a Hash of Arrays

Again, providing a key and an index will let you modify the inner Arrays:

daily_weather = {
  temperature: [75, 80, 72],
  precipitation: [0.0, 0.01, 0.03],
  wind_velocity: [4, 3, 2],
  barometric_pressure: [30.32, 30.30, 30.20]
}

daily_weather[:temperature][2] = 74 #=> 74
daily_weather[:temperature][2] #=> 74

Most often, we modify data in an HoA by using the key to get a hold of the Array so that we can use Array methods on the inner Array. Let's suppose we're adding new measurements for the day.

daily_weather = {
  :temperature => [75, 80, 72],
  :precipitation => [0.0, 0.01, 0.03],
  :wind_velocity => [4, 3, 2],
  :barometric_pressure => [30.32, 30.30, 30.20]
}

daily_weather[:temperature] << 76 #=> [75, 80, 72, 76]
daily_weather[:precipitation] << 1.01 #=> [0.0, 0.01, 0.03, 1.01]
daily_weather[:wind_velocity] << 2.2 #=> [4, 3, 2, 2.2]
daily_weather[:barometric_pressure] << 28.0 #=> [30.32, 30.3, 30.2, 28.0]

Lab

Guided by the tests, make sure that you are able to update and read from a HoA.

Conclusion

This concludes our learning of the "basic" nested data structures

  • Arrays of Arrays
  • Arrays of Hashes
  • Hashes of Hashes
  • Hashes of Arrays

By mixing these four "basic" nested data structures, we can build complex data structures that model our world's complexity as a data structures which Ruby can process โ€” with our help! โ€” to generate insights.

programming-univbasics-nds-hash-of-arrays-lab-sfo-web-010620's People

Watchers

 avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar  avatar Matt avatar Antoin avatar  avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.