Get Random Friend FBML - fbml

Hey, say you have an array of friends that you've retrieved, how can you pick 1 random friend.
Thanks

Try:
$array = array_slice(array_shuffle($array_orig), 0, 1);
This will get you one friend at random.

Related

Finding the sum,highest,lowest in a Hash/Array. Ruby

I am fairly new to this so I apologize in advance of my newbieness. I have been working on a project that I want to get the sum, highest,lowest out of a hash/array. I have tried numerous times to get this right but I typically will get an error such as, fixNum cannot convert int to string and undefined method. I will attempt to fix these issues and then run into another issue so I am at a loss. For the record in my text file I have 1,Foo,22 2,Smith,30 my output looks like this {1=>["Foo",22], 2=>["Smith",30]} I would like the highest number to show 30, lowest to be 22 and total to be 52 for different outputs.
You can do as below suppose lets say a variable a = {a: [a,1],b: [b,1] } then
values = a.values.map(&:last) //Gives the last element of each array
max= a.max
min = a.min
sum = a.sum
Okay, this is very ugly and someone will probably improve upon it but it works. Assuming I understand the output you would like.
elements = h.map{ |element| element[1] }.map { |element| element[1]}
# sum
elements.sum
# highest
elements.max
# lowest
elements.min
https://repl.it/repls/AntiqueOldfashionedRom
Convert to hash and calculate min max based on values
data = "1,Foo,22 2,Smith,30"
people =
data.split(",")
.each_slice(3)
.map {|slice| [slice[0], [slice[1], slice[2]]] }
.to_h
values = people.values.map {|person| person[1] }
min = values.min
max = values.max
sum = values.sum

Extract values from an array of hashes

I need to extract values from an array of hashes:
data =
[{:diaria_media=>"103.58908136482939632545931759",
:room_night=>"1143",
:valor_periodo=>"118402.320000"},
{:diaria_media=>"307.46792079207920792079207921",
:room_night=>"101",
:valor_periodo=>"31054.260000"},
{:diaria_media=>"313.000000",
:room_night=>"9",
:valor_periodo=>"2817.000000"},
{:diaria_media=>"0.0",
:room_night=>"7",
:valor_periodo=>"0.0"},
{:diaria_media=>"4.4630434782608695652173913043",
:room_night=>"414",
:valor_periodo=>"1847.700000"},
{:diaria_media=>"150.89382627422828427853553482",
:room_night=>"1393",
:valor_periodo=>"210195.100000"},
{:diaria_media=>"221.11425992779783393501805054",
:room_night=>"554",
:valor_periodo=>"122497.300000"},
{:diaria_media=>"36.919200",
:room_night=>"25",
:valor_periodo=>"922.980000"},
{:diaria_media=>"31.967530864197530864197530864",
:room_night=>"81",
:valor_periodo=>"2589.370000"},
{:diaria_media=>"0",
:room_night=>"0",
:valor_periodo=>"0.000000"}]
I need to get all the :room night fields and add the values. What is the best way to achieve that?
first: it's not nice to ask for help and to format the question as you did.
second: the question has nothing to do with Rails or with Savon.
This is a pure Ruby question.
The solution seems simple to me.
You iterate over your array and summarize the numbers for each key :room_night
For example like this:
nights = 0
data.each do |booking|
nights += booking[:room_night].to_i
end
print "nights=#{nights}\n"
If you go functional it's even more simple:
nights = data.map{|e| e[:room_night].to_i}.reduce(:+)
and done!
As a bonus I put a executable script into Pastbin https://pastebin.com/29nMTYrK

aggregate values from specific key in an array of hashes

I'm trying to build an array of values that come from an array of hashes, at the moment my code looks like this:
ids = array_of_hashes.inject([]) do |result,instance|
result << instance[:id]
result
end
I just want to know if there's a more efficient way to do it?
You could change it to look like:
ids = hash.map { |instance| instance[:id] }
Not necessarily more efficient, but easier to read and maintain!
Good luck!
There are two easy ways for it:
1. ids = hash.collect{|h| h[:id]}
2. ids = hash.map{|h| h[:id]}
Now you would ask what is the difference in both? for explanation see this accepted answer

ruby return fixed number of elements in a certain order

This might be a dumb question.
I know sample returns random number of elements from an array.
For example,
[1,2,3].sample.times do
Is there a way to return a fixed number of elements in a certain order always?
I dont know how to do this in ruby.
EDIT:
Lets say I always want to return penalty_name, severity and name only from the second and last array here always.:
offenses = PERSON_SUMMARY[:offenses].map do |offense|
offense[:penalties].map do |penalty|
penalty.merge(name: offense[:offense_name])
end
end.flatten
=> [{:penalty_name=>"Prison", :severity=>"Medium", :name=>"Speeding"}, {:penalty_name=>"Ticket", :severity=>"Low", :name=>"Speeding"}, {:penalty_name=>"Prison", :severity=>"Medium", :name=>"Shoplifting"}, {:penalty_name=>"Fine", :severity=>"Low", :name=>"Shoplifting"}]
right now I am doing:
offenses.each do |hash|
hash.sample
I think you want something like:
[1,2,3,4,5].sample(4).sort
It will take 4 random number from the array and order it...
edit - after your comment:
[5,4,3,2,1].values_at(1,-1).sort #second element(1) and last one(-1)
=>[1, 4]
You can specify which ones you want with values_at (negative numbers count from the back.)
ar=[{:penalty_name=>"Prison", :severity=>"Medium", :name=>"Speeding"}, {:penalty_name=>"Ticket", :severity=>"Low", :name=>"Speeding"}, {:penalty_name=>"Prison", :severity=>"Medium", :name=>"Shoplifting"}, {:penalty_name=>"Fine", :severity=>"Low", :name=>"Shoplifting"}]
p ar.values_at(1,-1).map{|h|h.values}
#=> [["Ticket", "Low", "Speeding"], ["Fine", "Low", "Shoplifting"]]

How do I find a integer/max integer in an array for ruby and return the indexed position?

This is what I have so far
ages = [20,19,21,17,31,33,34]
names = [Bob, Bill, Jill, Aimee, Joe, Matt, Chris]
How do I take ages and apply a method to it to extract the largest integer out of it and learn its indexed position. The reason being I want to know which person in names is the oldest. Parallel assignment is blocking my ability to do a .sort on the array becasue it changes the position of element associted with names.
Please include code to mull over thanks,
ages.zip(names).max
names[ages.index(ages.max)]
What it does is find the maximum value in ages (ages.max), get the index of the first matching value in ages, and use it to get the corresponding person. Note that if two or more people have the same age which is the maximum, it'll only give you the name of the first one in the array.
Edit: To address your comment, I'm not sure why you need this parallel arrays structure (it'd be much easier if you just had a person object). Nevertheless, you can try something like:
indices = []
ages.each_with_index { |age, i| indices << i if age < 20 }
indices.each { |i| puts names[i] }

Resources