简体   繁体   English

Ruby:基于以下元素遍历数组

[英]Ruby: Iterating Over Array Based on Following Elements

I am creating a week-by-week report of meetings using Ruby. 我正在使用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; 请注意, "clientMeeting"前面有"Week of the 1st:"然后是"Week of the 8th:"然后是"Week of the 15th:" ,因为重复了两次; while "boardInvestorsMeeting" only has "Week of the 1st:" in front of it because it isn't repeated and only appears in meetingsArray once. "boardInvestorsMeeting"只有"Week of the 1st:"在它前面的,因为它不重复,只出现在meetingsArray一次。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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