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... 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.
-- 
Posted via http://www.ruby-forum.com/.