# Hash-tastic

This week I learned about the glorious data storage object that is the Hash. The following will be some fun information on how to differentiate, define, and reference arrays and hashes. Arrays and hashes are both constucts used for data storage. Their construction and syntax are different, so depending on what you’re trying to do, one will likely be better than the other for specific applications.

# Define

### Array

Ordered, iteger-indexed collections of any object. Array indexing starts at 0 and goes to (n-1), where n is the number of array indices. A negative index is assumed to be relative to the end of the array—that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.

### Hash

A collection of key-value pairs. Indexing is done via arbitrary keys of any object type. Hashes order their values in the sequence that the corresponding keys were inserted. By default, hashes return 'nil' when accessing keys that do not exist in the hash. It’s possible to define this value to something other than 'nil' (see below).

# Create

Array.new is functionally the same as array = [] for creating a new array, except Array.new has some fun options for initializing the array however you like. For example:

Array.new # => []
Array.new(2) # => [nil,nil]
Array.new(3,"apple") # => ["apple","apple","apple"]

Or the following:

example_array = [1,2,3]
copycat_array = Array.new(example_array) # => [1,2,3]

The procedure for creating a new hash is all but identical: Hash.new, or hash = {}. Similarly, Hash.new can do the following:

Hash.new # => {}
Hash.new(2) # => {}
Hash.new(3,"apple") # => probably a syntax error (don't do this)

The Hash.new(argument) is the default value for accessing keys that do not exist in the hash. If you were to create an empty hash with the argument ‘2’, and then try to access a nonexistent key (since our hash is empty in this example, ALL keys are nonexistent), the hash would return ‘2’ instead of returning ‘nil’.

# Access

This is the easiest part:

puts array[integer] # => value of the array at the specified integer
puts hash["key"] # => value of the hash at the specified key

Arrays have integer index-value pairs, hashes have key-value pairs. Try creating and then accessing array/hash values at indices/keys that do not exist. That way, you’ll be able to recognize it if you ever do it unintentionally.

# Bonus

puts array.index(value) # => index of the array at the specified value
puts hash.key(value) # => key of the hash at the specified value

For duplicate values at different indices/keys, the above two statements will return the first index/key that corresponds to the specified value. But don’t take my word for it, try it for yourself!
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.21265950798988342, "perplexity": 1890.5368654469332}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-51/segments/1544376827769.75/warc/CC-MAIN-20181216143418-20181216165418-00584.warc.gz"}