Hi -- On Sat, 31 May 2008, Zoe Phoenix wrote: > Siep Korteling wrote: >> Zoe Phoenix wrote: >>> I have a program that someone on this forum helped me fix before that >>> took a list of cities formatted like: >>> >>> New York | Chicago | Boston | >>> >>> and formatted them like this, along with a phrase added after each one: >>> >>> New York >>> Chicago >>> Boston >>> etc. >>> >>> The code looks like this: >>> >>> main = 0 >>> >>> >>> full= File.open("state.txt") >>> phrase=[", New Jersey"] >>> count=0 >>> inside = [] >>> full.each do |line| >>> first=[] >>> first=line.split(/\|/) >>> first.each do |single| >>> sub=single.strip! >>> main = (sub).to_s + (phrase).to_s >>> >>> inside << main >>> >>> newfile=File.new("state2.txt", "w") >>> newfile.puts inside >>> newfile.close >>> >>> count+=1 >>> end >>> end >>> >>> >> >> The method split splits up a string and it will put the parts in an >> array. >> If you don't specify what to split on, it will split on newlines. Not >> what you want. How to make clear that you want to split on " "? >> Just say split(" ") . >> (split will also work with a regular expression, like in your code. It's >> faster and far more powerfull, but completely unreadable if you are not >> familiar with it. In your code split("|") works.) >> >> I have not tried, but it looks as if your code writes a new file for >> each line it reads. Each time the same file. First time 1 line, second >> time 2 lines, etc. You could consider taking this bit: >> >> newfile=File.new("state2.txt", "w") >> newfile.puts inside >> newfile.close >> >> out of the loop. >> >> hth, >> >> Siep > > > > Well, it isn't writing a new file for each line... It's writing a new file for each element in the input. You're writing the same file over and over again, a little bigger each time, instead of gathering all the input and writing it all at once (or writing it incrementally to a file that you keep open). > but, it's not listing > the cities like I want, all it's doing is putting the phrase on a new > line for the same number of cities there are. So, I get, say ", > Alabama" a bunch of times instead of "Montgomery, Alabama", "Birmingham, > Alabama", etc. > > I want it to take this: > > Alabaster Albertville Alexander City Andalusia Anniston Arab Ardmore > Athens Atmore Attalla Auburn > > And turn it into this: > > Alabaster, Alabama > Albertville, Alabama > Alexander City, Alabama > Andalusia, Alabama > etc. > > What I'm getting when I run the program is, > > , Alabama > , Alabama > , Alabama > etc. > > > I know I'll run into a problem with some of the cities having two words > in them, like Alexander City, but fixing those manually isn't a problem. Try this: phrase = ", Alabama" File.open("state.txt") do |infile| File.open("state2.txt", "w") do |outfile| infile.each do |line| cities = line.split(" ") cities.each do |single| outfile.puts(single.strip + phrase) end end end end David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin See http://www.rubypal.com for details and updates!