[英]print an array within an array if an array value equals x Ruby
我是Ruby的新手,我试图通过编写一个简单的终端程序来练习一些技能,该程序可以在温哥华找到一个远足地点。 (虽然这可能不是最终编写程序的最佳方法,但我正在尝试使用此程序测试数组技能。)
我所做的是将5个远足点排列成二维阵列。 每个数组都包括远足的名称,位置,难度和持续时间。 我希望用户输入自己的偏好,然后打印出匹配的涨幅。
例如,如果用户输入6km,我想从<= 6km的数组中打印出远足名称。
因此,我只想打印以下代码,但前提是“#{hikes [i] [0]}”中的[0] <= 6
hikes.each_index{|i| puts "#{hikes[i][0]} " }
谢谢您的帮助!
到目前为止,这是我的代码:
#Hiking Array: [Hike Name, Location, Difficulty, Length(km)]
hikes =
[["Admiralty Point", "Tri Cities", "Easy", 5],
["Habrich Ridge Trail", "Howe Sound", "Intermediate", 7],
["Aldergrove Regional Park", "Surrey", "Easy", 5],
["Alice Lake", "Howe Sound", "Easy", 6],
["Ancient Cedars Trail", "Whistler", "Intermediate", 5]]
puts "It's the weekend! Where should we go hiking?\n\n"
puts "Here is the list of available hikes."
puts "___________________________"
hikes.each_index{|i| puts "#{hikes[i][0]} " }
puts "\n"
puts "How many kilometers do we want to hike?"
kilometers = $stdin.gets.chomp
puts "\n"
puts "How hard do we want the hike to be, Easy or Intermediate?"
difficulty = $stdin.gets.chomp
puts "\n"
puts "What area of town do we want to hike in:"
puts "Tri Cities"
puts "Howe Sound"
puts "Surrey"
puts "Whistler"
area_of_town = $stdin.gets.chomp
puts "\n"
puts "Ok searching for awesome views that are #{kilometers}km, of #{difficulty} difficulty, and are in #{area_of_town}."
hikes.each_index{|i| puts "#{hikes[i][2]}"}
也许
hikes.select {|arr| arr[3] <= 5}.each{|arr| puts arr[0]}
但我建议您使用哈希,以便您可以使用名称来寻找所需的属性。
hikes = [{'location'=>'Admirality', 'difficulty'=>5}, {'location'=>'Admirality', 'difficulty'=>10}, {'location'=>'Admirality', 'difficulty'=>15}]
hikes.select {|hsh| hsh["difficulty"] <= 10}
如果嵌套数组与所有3个条件都匹配,则只需使用“和”运算符&&
从嵌套数组中打印名称,例如:
hikes.each do |h|
puts h[0] if ( h[3] <= kilometers &&
h[2].downcase == difficulty.downcase &&
h[1].downcase == area_of_town.downcase )
end
downcase
,以防万一用户输入与数组字符串不匹配(例如"Easy"
vs "easy"
) puts h.join(', ')
puts h[0]
更改为puts h.join(', ')
。 您可以考虑按以下步骤进行操作(在通过消除某些加长属性进行简化之后):
hikes =
[["Admiralty Point", 5],
["Habrich Ridge Trail", 7],
["Aldergrove Regional Park", 5],
["Alice Lake", "Howe Sound", 6],
["Ancient Cedars Trail", 5]]
HIKES_BY_DISTANCE =
hikes.each_with_object(Hash.new {|h,k| h[k] = []}) {|a,h| h[a.last] << a}
#=> {5=>[["Admiralty Point", 5],
# ["Aldergrove Regional Park", 5],
# ["Ancient Cedars Trail", 5]],
# 7=>[["Habrich Ridge Trail", 7]],
# 6=>[["Alice Lake", "Howe Sound", 6]]}
def select_hikes(length_range)
HIKES_BY_DISTANCE.each_with_object([]) do |(k,v),a|
a.concat(v) if length_range.cover?(k)
end
end
select_hikes(4..5)
#=> [["Admiralty Point", 5],
# ["Aldergrove Regional Park", 5],
# ["Ancient Cedars Trail", 5]]
select_hikes(2..6)
#=> [["Admiralty Point", 5],
# ["Aldergrove Regional Park", 5],
# ["Ancient Cedars Trail", 5],
# ["Alice Lake", "Howe Sound", 6]]
select_hikes(4..7)
#=> [["Admiralty Point", 5],
# ["Aldergrove Regional Park", 5],
# ["Ancient Cedars Trail", 5],
# ["Habrich Ridge Trail", 7],
# ["Alice Lake", "Howe Sound", 6]]
select_hikes(6..9)
#=> [["Habrich Ridge Trail", 7],
# ["Alice Lake", "Howe Sound", 6]]
select_hikes(7..7)
#=> [["Habrich Ridge Trail", 7]]
select_hikes(1..2)
#=> []
select_hikes(8..9)
#=> []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.