简体   繁体   中英

Ruby: Iterating Over Array Based on Following Elements

I am creating a week-by-week report of meetings using Ruby. I have a large array that contains the names of the meetings, and I have multiple instances of each element based on how many weeks they occured.

Simplified example I am starting with:

meetingsArray = ["boardInvestorsMeeting", "clientMeeting", "clientMeeting", "clientMeeting", "waterCoolerChat", "waterCoolerChat",  "waterCoolerChat", "waterCoolerChat"]

My attempt:

meetingsArray = ["boardInvestorsMeeting", "clientMeeting", "clientMeeting", "clientMeeting", "waterCoolerChat", "waterCoolerChat",  "waterCoolerChat", "waterCoolerChat"]
for m in meetingsArray[m.to_i]
  if (meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 1] && meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 2] && meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 3])
    puts "Week of the 1st: " + "#{meetingsArray[m.to_i].inject{|a| a}}"
  elsif (meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 1] && meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 2])
    puts "Week of the 8th: " + "#{meetingsArray[m.to_i].inject{|a| a}}"
  elsif (meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 1])
    puts "Week of the 15th: " + "#{meetingsArray[m.to_i].inject{|a| a}}"
  else
    puts "Week of the 22nd: " + "#{meetingsArray[m.to_i].inject{|a| a}}"
  end
end

Console error:

iMac:workspace user1$ ruby loop.rb
loop.rb:3:in `<main>': undefined method `each' for "boardInvestorsMeeting":String (NoMethodError)

Desired results:

Week of the 1st: boardInvestorsMeeting
Week of the 1st: clientMeeting
Week of the 8th: clientMeeting
Week of the 15th: clientMeeting
Week of the 1st: waterCoolerChat
Week of the 8th: waterCoolerChat
Week of the 15th: waterCoolerChat
Week of the 22nd: waterCoolerChat

Note that "clientMeeting" has "Week of the 1st:" then "Week of the 8th:" then "Week of the 15th:" in front of it because is repeated twice; while "boardInvestorsMeeting" only has "Week of the 1st:" in front of it because it isn't repeated and only appears in meetingsArray once.

Here you go!

meetingsArray = ["boardInvestorsMeeting", "clientMeeting", "clientMeeting", "clientMeeting", "waterCoolerChat", "waterCoolerChat",  "waterCoolerChat", "waterCoolerChat"]
counts = Hash.new(0)
meetingsArray.each { |name| counts[name] += 1 }

counts.each do |k,v|
    case v
    when 1
        puts "Week of the 1st: #{k}" 
    when 2
        puts "Week of the 1st: #{k}"
        puts "Week of the 8th: #{k}"
    when 3
        puts "Week of the 1st: #{k}"
        puts "Week of the 8th: #{k}"
        puts "Week of the 15th: #{k}"
    when 4
        puts "Week of the 1st: #{k}"
        puts "Week of the 8th: #{k}"
        puts "Week of the 15th: #{k}"
        puts "Week of the 22nd: #{k}"
    end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM