Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have complex hash which looks like this
#hash = {1=>[], 2=>[], 3=>[], 4=>[], 5=>[], 6=>[], 7=>[], 8=>[], 9=>[], 10=>[], 11=>[], 12=>[[{"value"=>1.58, "title"=>"sun", "quantity" => 2}], [{"value"=>1.99, "title"=>"sophia", "quantity" => 5}], [{"value"=>6.30, "title"=>"roam", "quantity" => 15}], [{"value"=>3.981, "title"=>"jia, "quantity" => 4"}], 13 => [], 14 => [], 15 => []}
now I want to extract highest value along with associated title and quantity. index would 15 all the time.
for example the output should be
#hash = { value => 6.30, title => "roam", quantity => 15 }
I was searhcing some found this but did not make it work
reference link Ref
help appreciated thanks
If you're not interested in the element's index, you could flatten the values and find the maximum one:
#hash = {
1=>[], 2=>[], 3=>[], 4=>[], 5=>[], 6=>[], 7=>[], 8=>[], 9=>[],
10=>[], 11=>[], 12=>[
[{"value"=>1.58, "title"=>"sun", "quantity" => 2}],
[{"value"=>1.99, "title"=>"sophia", "quantity" => 5}],
[{"value"=>6.30, "title"=>"roam", "quantity" => 15}],
[{"value"=>3.981, "title"=>"jia", "quantity" => "4"}]
],
13 => [], 14 => [], 15 => []
}
#hash.values.flatten.max_by { |h| h["value"] }
#=> {"value"=>6.3, "title"=>"roam", "quantity"=>15}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Which two of these three expressions are equal? Why?
{ "city" => "Miami", "state" => "Florida" }
{ :city => "Miami", :state => "Florida" }
{ city: "Miami", state: "Florida" }
There is a great discussion on using a Ruby :symbol vs a String in another question here.
And here's a nice discussion about the difference between the fat arrow => syntax vs colons : in Ruby.
You can quickly check that the two hashes using :symbols are equivalent to each other, which are both different from the hash using strings:
a = {"city" => "Miami", "state" => "Florida"}
b = {:city => "Miami", :state => "Florida"}
c = {city: "Miami", state: "Florida"}
a == b
=> false
a == c
=> false
b == c
=> true
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a hash whose keys are an array of two elements like this:
logs_data =
{
[ 143184, 11467 ] => {
:finished => true,
:created_at => 2017-11-09 09:38:11 UTC
},
[ 143184, 11471 ] => {
:finished => true,
:created_at => 2017-12-20 07:21:02 UTC
}
}
I don't know how to get the value from a key. I tried this way:
logs_data[143184, 11467]
#=> ArgumentError: wrong number of arguments (given 2, expected 1)
but it failed.
2.4.2 :027 > logs_data.keys
=> [[143184, 11467], [143184, 11471]]
your key is [143184, 11467] so you have to do hash[key] like following
2.4.2 :028 > logs_data[[143184, 11467]]
=> {:finished=>true, :created_at=> 2017-11-09 09:38:11 UTC}
You need two sets of brackets logs_data[[ key ]]
In ruby you can also do logs_data.keys or logs_data.values
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have an array of hashes:
my_array = [
{
:id => 1,
:name => "Bill"
},
{
:id => 2,
:name => "Joe"
},
{
:id => 3,
:name => "Bob"
}
]
How can I get an item passing the ID, for example, given 3 as the 'id' I'd like to get the hash: {"id": 3, "nome": "Bob"}.
UPDATE:
I discovered the answer, using the method .selec as follows:
my_array.select { |a| a[:id] == 1 }
What you have is a JSON string. You will need to parse it into an array of hashes first and then use Enumerable#find to get the item:
require 'json'
test = '[
{
"id": 1,
"nome": "Bill"
},
{
"id": 2,
"nome": "Joe"
},
{
"id": 3,
"nome": "Bob"
}
]'
# Parse into an array of hashes
hashes = JSON.parse(test)
foundItem = hashes.find { |item| item["id"] == 3}
puts foundItem
Since it looks like jSON you should parse as above if it is an actual array of Hashes you can do this
arr = [{"id" => 1,"nome"=> "Bill"},{"id"=> 2,"nome"=> "Joe"},{"id"=> 3,"nome"=> "Bob"}]
arr.select{|h| h["id"] == 3}
#=>[{"id"=> 3,"nome"=> "Bob"}]
This will return an Array of all matching Hash
To get a single value you can use
arr.select{|h| h["id"] == 3}.pop
#=>{"id"=> 3,"nome"=> "Bob"}
Which will return the last matching Hash Or as #KalmanHazins stated
arr.find{|h| h["id"] == 3}
#=>{"id"=> 3,"nome"=> "Bob"}
Which will return the first Hash that matches based on current order
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to remove partial duplicate value from an Array.
['John', 'Johnson', 'Mike', 'Tim', 'Timberland']
I want remove partial duplicate value.
in this case, I want keep longer string value.
['Johnson', 'Mike', 'Timberland']
Any good idea?
This is how I would do:
ary = ['John', 'Johnson', 'Mike', 'Tim', 'Timberland']
ary.select {|e| ary.grep(Regexp.new(e)).size == 1 }
# => ["Johnson", "Mike", "Timberland"]
Just do the following, in case when part is resided at the beginning of a word only:
array = ['John', 'Johnson', 'Mike', 'Tim', 'Brakatim', 'Weltimwel']
# => ["John", "Johnson", "Mike", "Tim", "Brakatim", "Weltimwel"]
array.reject {| v | " #{array.join( ' ' )} " =~ /\W#{v}\w/i }
# => ["Johnson", "Mike", "Tim", "Brakatim", "Weltimwel"]
Or in case when part is resided at the beginning of a word, and at the end or middle of it:
array = ['John', 'Johnson', 'Mike', 'Tim', 'Timberland', 'Brakatim', 'Weltimwel']
# => ["John", "Johnson", "Mike", "Tim", "Timberland", "Brakatim", "Weltimwel"]
array.reject {| v | " #{array.join( ' ' )} " =~ /\W#{v}\w|\w#{v}\W|\w#{v}\w/i }
# => ["Johnson", "Mike", "Timberland", "Brakatim", "Weltimwel"]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this array of arrays:
[["abc", "123"], ["cde", "456"], ["cde", "674"]]
And I want this array of arrays arranged in this way:
{ "name": "test", "children": [ {"name": "abc", "children": [ {"name": "123"} ]}, {"name": "cde", "children": [ { "name": "456"},{"name": "674"} ]}]}
How can I make this transformation in ruby language?
Thanks in advance.
Try this
require 'json'
src_arr= [["abc", "123"], ["cde", "456"], ["cde", "674"]]
tmp = {} # to collect all common node first
src_arr.each do |arr|
if node = tmp[arr.first] # check if node exists
node['children'] << {'name' => arr.last} # append of exists
else
# add node if does not exists
tmp[arr.first] = {'name' => arr.first,'children' => [{'name' => arr.last}]}
end
end
tree = {'name' => 'test','children' => tmp.values}
puts tree
#=> {"name"=>"test", "children"=>[{"name"=>"abc", "children"=>[{"name"=>"123"}]}, {"name"=>"cde", "children"=>[{"name"=>"456"}, {"name"=>"674"}]}]}
puts JSON.generate(tree)
#=> {"name":"test","children":[{"name":"abc","children":[{"name":"123"}]},{"name":"cde","children":[{"name":"456"},{"name":"674"}]}]}