From: Siep Korteling Date: 2008-05-31T08:41:48+09:00 Subject: Re: splitting help needed 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 -- Posted via http://www.ruby-forum.com/.