1

I am having an issue trying to figure out how to work with an array inside a hash in ruby. I am running a command (last command on linux if you must know), capturing output from it and trying to insert unique data about each user that appeared in this log into the hash, each having its own array. Example below:

hash = {
  "userbob1" => ["Bob User", 10, "10.10.2016"],
  "userjim2" => ["Jim User", 4, "9.16.16"]
}

and so forth.

I have been able to successfully insert an array into a hash as a whole, but how would I go about retrieving specific values of that array, or adding values to it after I added it to the hash?

I'd imagine something like this:

hash["userbob1"[0]]  #=> "Bob User"
1
  • You need to correct your example. For "userjim1", Bob User and 10.10.2016are not valid Ruby objects. I expect you want ["Bob User", 10, "10.10.2016"] and similar for "userjim2". Commented Nov 30, 2016 at 20:59

3 Answers 3

1

You've got to navigate through each part of the structure sequentially, and remember each part of that code is evaluated as it goes left to right:

hash["userbob1"][0]

What you were doing is asking for "userbob1"[0] which of course evaluates to "u", and that value is then used as a hash key, which is wrong.

Just think of each part as something you can evaluate and append to:

hash
hash["userbob1"]
hash["userbob1"][0]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you both! Most have been running a little low on caffeine! My last question is, if I added a blank array to the hash for each user, is there a way to use the array.push method to add values to it?
Try evaluating these things in irb to see how they behave. If you're referencing an array, you can do anything to it you can do to an array: hash["userbob1"][0] << 'Another field' for example, where << is an alias for push.
1
hash = { "userbob1" => ["Bob User", 10, "10.10.2016"],
         "userjim2" => ["Jim User", 4, "9.16.16"] }

arr = hash["userbob1"]
  #=> ["Bob User", 10, "10.10.2016"] 

Retrieve a value from the array

Retrieve the value at index 1.

arr[1]
  #=> 10 

which is the same as

hash["userbob1"][1]

Append the array

Append the array with "cat",

arr << "cat"
hash
  #=> {"userbob1"=>["Bob User", 10, "10.10.2016", "cat"],
  #    "userjim2"=>["Jim User", 4, "9.16.16"]} 

which is the same as

hash["userbob1"] << "cat"

or

hash["userbob1"].push("cat")

Replace the array with another array

arr.replace [1,2,3]
hash
  #=> {"userbob1"=>[1, 2, 3], "userjim2"=>["Jim User", 4, "9.16.16"]} 

which is the same as

hash["userbob1"].replace [1,2,3]

or

hash["userbob1"] = [1,2,3]
  #=> [1,2,3]
hash
  #=> {"userbob1"=>[1, 2, 3], "userjim2"=>["Jim User", 4, "9.16.16"]} 

Note

arr = [1,2,3]
hash
  #=> {"userbob1"=>["Bob User", 10, "10.10.2016"],
  #    "userjim2"=>["Jim User", 4, "9.16.16"]}

(The hash is not altered.)

Reverse the array

arr.reverse!
hash
  #=> {"userbob1"=>["10.10.2016", 10, "Bob User"],
  #    "userjim2"=>["Jim User", 4, "9.16.16"]} 

which is the same as

hash["userbob1"].reverse!

Note

arr.reverse
hash
  #=> {"userbob1"=>["Bob User", 10, "10.10.2016"],
  #    "userjim2"=>["Jim User", 4, "9.16.16"]}

The hash is unchanged because arr.reverse returns the reversed array but does not change it.

1 Comment

Thanks for the detailed breakdown! Very helpful as well!!
0

Your syntax is close to correct. Try hash["userbob1"] [0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.