I would most certainly use a regular expression solution for a task like this.
There is little gain to get from split and join here
str = "SEARCH_TERM(S) CITY,STATE"
str.scan(/^(.*)\s+([^\s,]+
puts a[0]
puts a[1]
puts a[2]
end
of course you can save the different regex results in a variable if you need to
and if you make it an array, you can easily work on an entire csv file like this
cheers
Geert
Main Topics
Browse All Topics





by: wesgarrisonPosted on 2007-12-23 at 14:21:58ID: 20522771
If your only delimiter between the search terms and the city is a space, you're going to have issues with any city that has a space in it:
')
re/classes /Array.htm l#M002192
Term1 Term2 Kansas City, KS
... would make "City" the city and "Kansas" an additional search term.
That said, just call split() on it again with a space, which will return an array of words:
search_terms_state = search_parts.first.split('
city = search_terms_state.pop
# pop actually removes the last element and returns it, so only the terms are left
RubyDoc for Array::pop()
http://www.ruby-doc.org/co
Is that what you're looking for? If not, lemme know and we'll get it straightened out.