I have a following method
view(all_people, begin_date, end_date)
That's give me an array like this:
# for a first day (day 1)
view("*", '2009-12-01 00:00:00', '2009-12-01 24:59:59')
I would rather change timestamp strings into objects of class Time or
DateTime because that is what they are You can do proper comparison
etc. I am aware that you can also do that with your string format but
if it is a timestamp it's usually better to treat it as such.
[[name_person1, room1], [name_person2, room2], [name_person3, room2]]
# for a second day (day 2)
view("*", '2009-12-02 00:00:00', '2009-12-02 24:59:59')
[[name_person1, room1], [name_person3, room1], [name_person2, room2]]
And I need to organize theses arrays in a following structure:
[{room1 => [{day1 => [name_person1]},
{day2 => [name_person1, name_person3]}]
},
{room2 => [{day1 => [name_person2, name_person3]},
{day2 => [name_person2]}]
}]
Why so many nesting levels? Why not
{room1 => [{day1 => [name_person1]},
{day2 => [name_person1, name_person3]}]
},
{room2 => [{day1 => [name_person2, name_person3]},
{day2 => [name_person2]}]
}
Or even
{room1 => {day1 => [name_person1]},
{day2 => [name_person1, name_person3]}
},
{room2 => {day1 => [name_person2, name_person3]},
{day2 => [name_person2]}
}
Methinks you are making the structure much more complex than
necessary, especially in light of the desired output:
And finally put this array in a cvs strings
room1; day1; name_person1
room1; day2; name_person1
room1; day2; name_person3
room2; day1; name_person2
room2; day1; name_person3
room2; day2; name_person2
I tried some scripts for hours but I didn't do this job!
Too complicated for my.
See Bertram's solution. I would have done it a bit different, mainly
including sorting.
room_day_assignments = Hash.new {|h,k| h[k] = Hash.new {|a,b| a[b] = } }
days.each do |day|
v = view...
v.each do |person, room|
room_day_assignments[room][day] << person
end
end
# output
room_day_assignments.keys.sort.each do |room|
room_day = room_day_assignments[room]
room_day.keys.sort.each do |day|
room_day[day].each do |person|
printf "%s;%s;%s\n", room, day, person
end
end
end
Untested.
Cheers
robert
···
2009/12/3 Bruno Moura <brunormoura@gmail.com>:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/