Thread: Ruby Snippets
View Single Post
Old Jul 9th, 2006, 3:45 PM   #31
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by Dietrich
I found this simple example:
# an array of the 50 states

states50 = [
  'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 
  'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 
  'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 
  'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 
  'Nebraska', 'Nevada', 'New_Hampshire', 'New_Jersey', 'New_Mexico',
  'New_York', 'North_Carolina', 'North_Dakota', 'Ohio', 'Oklahoma', 
  'Oregon', 'Pennsylvania', 'Rhode_Island', 'South_Carolina', 
  'South_Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 
  'Virginia', 'Washington', 'West_Virginia', 'Wisconsin', 'Wyoming'
]

puts states50.length()  # () is optional  --> 50

puts states50  # displays 50 states, one per line

def find(arr, value)
  for i in 0...arr.length
    return i if value == arr[i]
  end
end

puts find(states50, "Iowa")  # found at index --> 14
puts states50.index("Iowa") #=> 14

__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote