Skip to content

Instantly share code, notes, and snippets.

@jkeck
Created March 10, 2010 05:02
Show Gist options
  • Save jkeck/327538 to your computer and use it in GitHub Desktop.
Save jkeck/327538 to your computer and use it in GitHub Desktop.
#this is your output buffer simulated in a hash
mybuffer = {"1"=>[{:parser_first_name=>"abc",:parser_last_name=>"xyz",:parser_bday=>"jan1"},
{:parser_first_name=>"def",:parser_last_name=>"lmn",:parser_bday=>"feb1"}],
"2"=>[{:parser_first_name=>"amy",:parser_last_name=>"morgan",:parser_bday=>"may1"},
{:parser_first_name=>"nick",:parser_last_name=>"cary",:parser_bday=>"may6"}]
}
test_hash = {}
mybuffer.each do |groupid,people|
people.each do |person|
# if there is an group with that id in the hash already
if test_hash[groupid]
# add another "person" into the list of people (or another item in the array)
test_hash[groupid] << {:fname=>person[:parser_first_name],:lname=>person[:parser_last_name],:bday=>person[:parser_bday]}
else # if there isn't a group with that id in the hash already
# create a group in the hash with a key of the group id and the contents is list of people containing one person (an array containing one hash)
test_hash[groupid] = [{:fname=>person[:parser_first_name],:lname=>person[:parser_last_name],:bday=>person[:parser_bday]}]
end
end
end
puts test_hash.inspect
puts "\n\nNow you can do stuff like this:"
test_hash.each do |gid,people|
people.each do |person|
puts "#{person[:fname]} #{person[:lname]} who's birthday is #{person[:bday]} is in group #{gid}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment