print an array within an array if an array value equals x Ruby -


i'm new ruby , i'm trying practice skills bit writing simple terminal program finds hiking spot in vancouver. (while may not best way write program in end, i'm trying test array skills program.)

what i've done put 5 hiking spots in 2 dimensional array. each array includes name, location, difficulty, , length of hike. want user enter preferences , print out matching hikes.

e.g. if user enters 6km, want print out hike names arrays <= 6km.

so want print following code when [0] <= 6 in "#{hikes[i][0]}"

    hikes.each_index{|i| puts "#{hikes[i][0]} " } 

thanks help!

here's code far:

    #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 weekend! should go hiking?\n\n"     puts "here list of available hikes."     puts "___________________________"      hikes.each_index{|i| puts "#{hikes[i][0]} " }      puts "\n"     puts "how many kilometers want hike?"     kilometers = $stdin.gets.chomp      puts "\n"     puts "how hard want hike be, easy or intermediate?"     difficulty = $stdin.gets.chomp      puts "\n"     puts "what area of town want hike in:"     puts "tri cities"     puts "howe sound"     puts "surrey"     puts "whistler"     area_of_town = $stdin.gets.chomp      puts "\n"     puts "ok searching awesome views #{kilometers}km, of #{difficulty} difficulty, , in #{area_of_town}."      hikes.each_index{|i| puts "#{hikes[i][2]}"} 

maybe

hikes.select {|arr| arr[3] <= 5}.each{|arr| puts arr[0]} 

but suggest using hash can use name attribute you're looking for.

hikes = [{'location'=>'admirality', 'difficulty'=>5}, {'location'=>'admirality', 'difficulty'=>10}, {'location'=>'admirality', 'difficulty'=>15}]  hikes.select {|hsh| hsh["difficulty"] <= 10} 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -