It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Given:
hash = { "value" => 4, "details" => "I am some details"}, {"value" => 5, "details" => "I am new details"}
can I do something like:
hash.each do |key, value|
puts "#{key} is #{value}"
end
to get something like:
{ "value" => 4, "details" => "I am some details"} is {"value" => 5, "details" => "I am new details"} is
If a hash table (map) is not what I want to do this with, what would be? Databases are out of the question. The user should be able to continue to add on to the end with another {} if they need to filling out the same details as what's in the first two.
You've created an array of hashes, which you can do more explicitly as:
hashes = [{:value => "foo"}, {:value => "bar"}]
You can then append with
hashes << {:value => "baz"}
If you're ever wondering what type of variable you're working with, you can do var.class:
hash = { "value" => 4, "details" => "I am some details"}, {"value" => 5, "details" => "I am new details"}
hash.class #=> Array
A Map is a mapping of Distinct Keys to Values; there are Map variations which relax this, but Ruby's Hashmap follows the standard Map ADT.
In this case an Array of two different Hashes (each with a "value" and a "details") is being created.
> x = [1,2] # standard Array literal
=> [1,2]
> x = 1,2 # as in the posted code, no []'s, but ..
=> [1,2] # .. the same: the =, production created the Array here!
So it's not a Hash in hash, but rather an Array (containing two Hash elements) :)
Compare the results with the following and note that b is nil each time:
["one","two","three","four"].each do |a,b|
puts ">" + a + "|" + b
end
This is why it prints "hash1.to_str is hash2.to_str is" as the each iterates over the Array, as discussed above and only the first argument is populated with a meaningful value - one of the hashes.
I don't understand what "databases are out of the question" means in this context. Sounds like you're creating a data store, and so far it looks like you have a numeric ID with a related value.
A hash is a hash; you'd add values to the hash:
h = { "4" => "foo" } # Initial values
h["5"] = "ohai"
h["6"] = "kthxbai"
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Could someone please explain lines 5 and 6? On line 5, is |word| a parameter? Why is it needed there? Also, on line 6, are {|a, b| b} also parameters. How should one read line 6? What is it doing?
puts "Input something: " # 1
text = gets.chomp # 2
words = text.split # 3
frequencies = Hash.new(0) # 4
words.each { |word| frequencies[word] += 1 } # 5
frequencies = frequencies.sort_by {|a, b| b} # 6
frequencies.reverse! # 7
On line 5, is |word| a parameter?
Yes, it's a block argument.
Why is it needed there?
From Array#each's documentation: "Calls the given block once for each element in self, passing that element as a parameter."
Example:
words = ["foo", "bar", "baz"]
words.each { |word| puts word }
The block is called three times. On the first pass, its block argument word is set to "foo", on the second pass it's set to "bar" and on the third pass it's set to "baz". Each time word is printed using puts.
Output:
foo
bar
baz
In your example, a hash is used to store the word frequencies. Within the each loop, the word's count is incremented.
How should one read line 6? What is it doing?
Enumerable#sort_by sorts a collection by the block's result. For example, to sort an array of strings by the string's length you would use:
["xxx", "xx", "x"].sort_by { |str| str.length }
#=> ["x", "xx", "xxx"]
Since frequencies is a hash, the block is called for each pair. Therefore, two arguments are set - a is the pair's key and b is the pair's value:
frequencies = { "foo" => 3, "bar" => 2, "baz" => 1}
frequencies = frequencies.sort_by { |a, b| b }
#=> [["baz", 1], ["bar", 2], ["foo", 3]]
It sorts the hash by its values. Note that sort_by returns an array. The array is assigned to the frequencies variable.
Instead of a and b you could use more descriptive argument names:
frequencies.sort_by { |word, count| count }
This question already has answers here:
Converting a nested hash into a flat hash
(8 answers)
Closed 8 years ago.
Here is a structure of hash of arrays:
[
{
"key1" => [
"value1",
{"key2" => ["value2"]},
{"key3" => [
"value3",
{
"key4" => "value4"
}
]
}
]
},
{
"anotherKey1" => [],
}
]
I want desired output for that structure like filepaths:
/key1/value1
/key1/key2/value2
/key3/value3
/key3/key4/value4
How can I do that without inventing a wheel? Simple recursion could help, but is there any ready-to-go modules?
I do not think you would be reinventing any wheels to do this. You would like to traverse a nested structure of arrays and hashes and react completely different to the elements depending on whether something is an Array or a Hash. No library function is going to do exactly that for you, as you would need to vary more than one thing with blocks in order to be as flexible as you might like to be.
In short: write your recursive function to do this.
(Btw: The top level of your data structure is an array of hashes, not a hash of arrays …)
I decided to write my own wheel (thanks for Patru, vote up).
And I have this function:
def flat_hash_of_arrays(hash,string = "",delimiter="/",result = [])
# choose delimiter
hash.each do |key,value|
# string dup for avoid string-reference (oh, Ruby)
newString = string + delimiter + key
# if value is array
if value.is_a?(Array)
# if array not empty
value.each do |elementOfArray|
# if a string, I dont need recursion, hah
if elementOfArray.is_a?(String)
resultString = newString + delimiter + elementOfArray
# add new object
result << resultString
end
# if a hash, I need recursion
if elementOfArray.is_a?(Hash)
flat_hash_of_arrays(elementOfArray,newString,delimiter,result)
end
end
end
end
end
and test it:
flatten_hash = {
"key1" => [
"value1",
{"key2" => ["value2"]},
{"key3" => [
"value3",
{
"key4" => "value4"
}
]
},
"value4",
{
"key4" => ["value5"],
}
]
}
result = []
flat_hash_of_arrays(flatten_hash,"","/",result)
puts result
output is:
/key1/value1
/key1/key2/value2
/key1/key3/value3
/key1/value4
/key1/key4/value5
fine!
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
If I have this array:
["1","2","3,a","4,b"]
how can I get this array from it?
["1","2","3","4"].
Assuming you have all integers, (If not I guess you get the idea :))
["1","2","3,a","4,b"].collect {|i| i.to_i}
With an array like:
ary = ["1","2","3,a","4,b"]
I'd use:
ary.map{ |s| s[/\A(\d+)/, 1] }
Which results in:
[
[0] "1",
[1] "2",
[2] "3",
[3] "4"
]
It simply finds the numerics at the start of the strings and returns them in a new array.
Try:
["1","2","3,a","4,b"].map(&:to_i)
=> [1, 2, 3, 4]
to get array of strings
["1","2","3,a","4,b"].map(&:to_i).map(&:to_s)
=> ["1", "2", "3", "4"]
This should also works...
array=["1","2","3,a","4,b"]
for i in 0..array.length()-1
if array[i].include?(",")
ab=array[i].split(",")
array[i]=ab[0]
end
end
print array #["1","2","3","4"]
Try this delete option :
irb(main):014:0> a = ["101","2zd","3,a","10,ab"]
=> ["101", "2zd", "3,a", "10,ab"]
irb(main):015:0> count = a.count
irb(main):016:0> for i in 0..count
irb(main):017:1> puts a[i].delete("^0-9")
irb(main):018:1> end
101
2
3
10
UPDATED Code ( Use of Each Loop - Thanks to the Tin Man)
irb(main):005:0> a = ["101","2zd","3,a","10,ab"]
=> ["101", "2zd", "3,a", "10,ab"]
irb(main):006:0> b = []
=> []
irb(main):007:0> a.each do |t|
irb(main):008:1* b << t.delete("^0-9")
irb(main):009:1> end
=> ["101", "2zd", "3,a", "10,ab"]
irb(main):010:0> puts b
101
2
3
10
=> nil
I have a set of data that is an array of hashes, with each hash representing one record of data:
data = [
{
:id => "12345",
:bucket_1_rank => "2",
:bucket_1_count => "12",
:bucket_2_rank => "7",
:bucket_2_count => "25"
},
{
:id => "45678",
:bucket_1_rank => "2",
:bucket_1_count => "15",
:bucket_2_rank => "9",
:bucket_2_count => "68"
},
{
:id => "78901",
:bucket_1_rank => "5",
:bucket_1_count => "36"
}
]
The ranks values are always between 1 and 10.
What I am trying to do is select each of the possible values for the rank fields (the :bucket_1_rank and :bucket_2_rank fields) as keys in my final resultset, and the values for each key will be an array of all the values in its associated :bucket_count field. So, for the data above, the final resulting structure I have in mind is something like:
bucket 1:
{"2" => ["12", "15"], "5" => ["36"]}
bucket 2:
{"7" => ["25"], "9" => ["68"]}
I can do this working under the assumption that the field names stay the same, or through hard coding the field/key names, or just using group_by for the fields I need, but my problem is that I work with a different data set each month where the rank fields are named slightly differently depending on the project specs, and I want to identify the names for the count and rank fields dynamically as opposed to hard coding the field names.
I wrote two quick helpers get_ranks and get_buckets that use regex to return an array of fieldnames that are either ranks or count fields, since these fields will always have the literal string "_rank" or "_count" in their names:
ranks = get_ranks
counts = get_counts
results = Hash.new{|h,k| h[k] = []}
data.each do |i|
ranks.each do |r|
unless i[r].nil?
counts.each do |c|
results[i[r]] << i[c]
end
end
end
end
p results
This seems to be close, but feels awkward, and it seems to me there has to be a better way to iterate through this data set. Since I haven't worked on this project using Ruby I'd use this as an opportunity to improve my understanding iterating through arrays of hashes, populating a hash with arrays as values, etc. Any resources/suggestions would be much appreciated.
You could shorten it to:
result = Hash.new{|h,k| h[k] = Hash.new{|h2,k2| h2[k2] = []}}
data.each do |hsh|
hsh.each do |key, value|
result[$1][value] << hsh["#{$1}_count".to_sym] if key =~ /(.*)_rank$/
end
end
puts result
#=> {"bucket_1"=>{"2"=>["12", "15"], "5"=>["36"]}, "bucket_2"=>{"7"=>["25"], "9"=>["68"]}}
Though this is assuming that :bucket_2_item_count is actually supposed to be :bucket_2_count.
I've got an array of hashes representing objects as a response to an API call. I need to pull data from some of the hashes, and one particular key serves as an id for the hash object. I would like to convert the array into a hash with the keys as the ids, and the values as the original hash with that id.
Here's what I'm talking about:
api_response = [
{ :id => 1, :foo => 'bar' },
{ :id => 2, :foo => 'another bar' },
# ..
]
ideal_response = {
1 => { :id => 1, :foo => 'bar' },
2 => { :id => 2, :foo => 'another bar' },
# ..
}
There are two ways I could think of doing this.
Map the data to the ideal_response (below)
Use api_response.find { |x| x[:id] == i } for each record I need to access.
A method I'm unaware of, possibly involving a way of using map to build a hash, natively.
My method of mapping:
keys = data.map { |x| x[:id] }
mapped = Hash[*keys.zip(data).flatten]
I can't help but feel like there is a more performant, tidier way of doing this. Option 2 is very performant when there are a very minimal number of records that need to be accessed. Mapping excels here, but it starts to break down when there are a lot of records in the response. Thankfully, I don't expect there to be more than 50-100 records, so mapping is sufficient.
Is there a smarter, tidier, or more performant way of doing this in Ruby?
Ruby <= 2.0
> Hash[api_response.map { |r| [r[:id], r] }]
#=> {1=>{:id=>1, :foo=>"bar"}, 2=>{:id=>2, :foo=>"another bar"}}
However, Hash::[] is pretty ugly and breaks the usual left-to-right OOP flow. That's why Facets proposed Enumerable#mash:
> require 'facets'
> api_response.mash { |r| [r[:id], r] }
#=> {1=>{:id=>1, :foo=>"bar"}, 2=>{:id=>2, :foo=>"another bar"}}
This basic abstraction (convert enumerables to hashes) was asked to be included in Ruby long ago, alas, without luck.
Note that your use case is covered by Active Support: Enumerable#index_by
Ruby >= 2.1
[UPDATE] Still no love for Enumerable#mash, but now we have Array#to_h. It creates an intermediate array, but it's better than nothing:
> object = api_response.map { |r| [r[:id], r] }.to_h
Something like:
ideal_response = api_response.group_by{|i| i[:id]}
#=> {1=>[{:id=>1, :foo=>"bar"}], 2=>[{:id=>2, :foo=>"another bar"}]}
It uses Enumerable's group_by, which works on collections, returning matches for whatever key value you want. Because it expects to find multiple occurrences of matching key-value hits it appends them to arrays, so you end up with a hash of arrays of hashes. You could peel back the internal arrays if you wanted but could run a risk of overwriting content if two of your hash IDs collided. group_by avoids that with the inner array.
Accessing a particular element is easy:
ideal_response[1][0] #=> {:id=>1, :foo=>"bar"}
ideal_response[1][0][:foo] #=> "bar"
The way you show at the end of the question is another valid way of doing it. Both are reasonably fast and elegant.
For this I'd probably just go:
ideal_response = api_response.each_with_object(Hash.new) { |o, h| h[o[:id]] = o }
Not super pretty with the multiple brackets in the block but it does the trick with just a single iteration of the api_response.