0

I'm having an issue trying to determine how to create an array I need.

I have an array that looks something like this:

["2013-10-01","2013-10-02","2013-10-07"]

So there's 7 days, 10-01 being sunday and 10-07 being monday. I want to dump each date into it's respective array, but if the date doesn't exist (10-03) I want to place an empty element in the tuesday array.

Code Ex.

date_array = ["2013-10-01","2013-10-02","2013-10-07"]
sunday_array = []
monday_array = []
tuesday_array = []
wednesday_array = []
thursday_array = []
friday_array = []
saturday_array = []
sunday_array = []

date_array.each do |date|
  if date.include? (Date.parse(Time.now.to_s) - 39).strftime("%Y-%m-%d")
    sunday_array << date
  elsif date.include? (Date.parse(Time.now.to_s) - 38).strftime("%Y-%m-%d")
    monday_array << date
  elsif date.include? (Date.parse(Time.now.to_s) - 37).strftime("%Y-%m-%d")
    tuesday_array << date
  #etc, etc.
  end
end

This would work, but it doesn't create the empty values I need. At the end, each array should have the exact same number of elements, even if thursday_array has 100% empty values.

3
  • Why you wrote such complex code? :) Commented Nov 9, 2013 at 15:46
  • 1
    +1 May be you say the whole task to us and we propose better solution? Commented Nov 9, 2013 at 15:50
  • @alex Yes.. I agree with you.. Commented Nov 9, 2013 at 15:52

2 Answers 2

1

This program will do as you ask. For each member of staff it creates a seven-element array of false values, then the day of week corresponding to each element of the details array is set to true. At the end, days not worked still contain false.

Note that Time#wday returns 0 for Sunday and 6 for Saturday, so each member of staff's array goes from Sunday to Saturday.

As you can see from the output, every member of staff in your sample data worked from Monday to Friday except Ramielle Ford who also took Friday off.

The pp module has been included only to prove the pp command to lay out the data nicely. You will not need it.

require 'date'
require 'pp'

table = eval(<<__END__JSON__)
{"success"=>true, "data"=>[
    {"id"=>133572, "memberId"=>"103001862-07", "firstName"=>"Anissa", "lastName"=>"Martin", "dateOfBirth"=>"2002-11-09", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"08:19:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"08:15:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:40:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"08:26:00", "checkOutTime"=>"08:00:00"}]},
    {"id"=>133573, "memberId"=>"103001862-08", "firstName"=>"Allyson", "lastName"=>"Martin", "dateOfBirth"=>"2005-02-16", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"08:19:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"08:15:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:40:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"08:26:00", "checkOutTime"=>"08:00:00"}]},
    {"id"=>135692, "memberId"=>"103008263-05", "firstName"=>"Emmya", "lastName"=>"Burrell", "dateOfBirth"=>"2003-01-17", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"16:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"16:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"16:40:00"}]},
    {"id"=>138343, "memberId"=>"103015748-03", "firstName"=>"Abigail", "lastName"=>"Young", "dateOfBirth"=>"2003-08-09", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"07:40:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"07:35:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:36:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>1, "checkInTime"=>"07:40:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:30:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>1, "checkInTime"=>"07:30:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"07:56:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:26:00"}]},
    {"id"=>139451, "memberId"=>"103016684-03", "firstName"=>"William", "lastName"=>"Brown", "dateOfBirth"=>"2004-02-06", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}]},
    {"id"=>8748758, "memberId"=>"2162539-05", "firstName"=>"Ramielle", "lastName"=>"Ford", "dateOfBirth"=>"2009-08-06", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}]},
    {"id"=>8795043, "memberId"=>"2163877-04", "firstName"=>"Christopher", "lastName"=>"Lampkins", "dateOfBirth"=>"2006-11-26", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:23:00"}]}]}
__END__JSON__

cards = {}
table['data'].each do |staff|
  name = staff.values_at(*%w/ firstName lastName /).join(' ')
  cards[name] = Array.new(7, false)
  staff['details'].each do |clock|
    wday = Date.parse(clock['date']).wday
    cards[name][wday] = true
  end
end

pp cards

output

{"Anissa Martin"=>[false, true, true, true, true, true, false],
 "Allyson Martin"=>[false, true, true, true, true, true, false],
 "Emmya Burrell"=>[false, true, true, true, true, true, false],
 "Abigail Young"=>[false, true, true, true, true, true, false],
 "William Brown"=>[false, true, true, true, true, true, false],
 "Ramielle Ford"=>[false, false, true, true, true, false, false],
 "Christopher Lampkins"=>[false, true, true, true, true, true, false]}

Update

Luigi suggest using group_by. This alternative version is written that way and I think it is less readable, but you may prefer it.

cards = {}
table['data'].each do |staff|
  name = staff.values_at(*%w/ firstName lastName /).join(' ')
  cards[name] = Array.new(7, false)
  staff['details'].group_by { |clock| Date.parse(clock['date']).wday }.each_key do |wday|
    cards[name][wday] = true;
  end
end

Update 2

This final alternative seems to do what you most recently say you want. If your requirement changes again then please open a new question, explain your requirement comprehensively, and give a good example of actual data. I spent several minutes looking for a bug that isn't there: Anissa Martin really does clock in at 8:26 and out at 8:00.

This iteration isn't so far from my original, and if you have enough knowledge to stand a chance of writing this for yourself then it is hard to believe you couldn't have come up with the same thing.

cards = {}
table['data'].each do |staff|
  name = staff.values_at(*%w/ firstName lastName /).join(' ')
  cards[name] = Array.new(14, nil)
  staff['details'].each do |clock|
    wday = DateTime.parse(clock['date']).wday
    cards[name][wday + wday, 2] = clock.values_at(*%w/ checkInTime checkOutTime /)
  end
end

output

{"Anissa Martin"=>
  [nil,
   nil,
   "08:26:00",
   "08:00:00",
   "08:19:00",
   "08:00:00",
   "08:15:00",
   "08:00:00",
   "15:30:00",
   "17:40:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil],
 "Allyson Martin"=>
  [nil,
   nil,
   "08:26:00",
   "08:00:00",
   "08:19:00",
   "08:00:00",
   "08:15:00",
   "08:00:00",
   "15:30:00",
   "17:40:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil],
 "Emmya Burrell"=>
  [nil,
   nil,
   "15:30:00",
   "16:40:00",
   "15:30:00",
   "16:00:00",
   "15:30:00",
   "16:00:00",
   "07:00:00",
   "08:00:00",
   "07:00:00",
   "08:00:00",
   nil,
   nil],
 "Abigail Young"=>
  [nil,
   nil,
   "15:30:00",
   "17:26:00",
   "15:30:00",
   "17:00:00",
   "15:30:00",
   "17:36:00",
   "15:30:00",
   "17:30:00",
   "07:30:00",
   "08:00:00",
   nil,
   nil],
 "William Brown"=>
  [nil,
   nil,
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil],
 "Ramielle Ford"=>
  [nil,
   nil,
   nil,
   nil,
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil,
   nil,
   nil],
 "Christopher Lampkins"=>
  [nil,
   nil,
   "15:30:00",
   "17:23:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil]}
Sign up to request clarification or add additional context in comments.

6 Comments

This is definitely closer, and thanks for the tips. However, what I need is an array of 14 values with the actual times (checkin,checkout,checkin,checkout, etc.) that correspond to each day. How can I pull in that data?
To clarify my above comment, I need a hash like you have above with the values being 14 element arrays of the checkin/checkout times, in comparison to the 7 element array including true/false.
@Luigi: Well this is new. I suggest you need an array of seven pairs of times, each pair being the in and out times for the period. It also seems like you haven't thought this through, as some employees log in and out twice in one day. William Brown, for instance, logs out twice every day. How do you want this data represented?
@Luigi, Also you need to decide whether you want the report to run for the preceding seven days (in this case Tuesday through to Monday) or if the columns will always be Sunday to Saturday as you say.
Valid concern - My apologies for this data being in that format, I would actually in this case always choose the later time. In the end this data will only include the later time, so no one will have checked in/out more than once in a day. The data will always run on Saturday evening for Sunday to Saturday, so the columns will remain static.
|
0

Try to do following: array.group_by{|e| Date.parse(e).wday }

http://www.ruby-doc.org/core-2.0.0/Time.html#method-i-wday

1 Comment

I think you mean to use the Date class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.