[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

splitting help needed

Zoe Phoenix

5/30/2008 11:15:00 PM

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




I tried to modify it so that it would separate not on the '|' character,
but on a single space (such as in a list of cities like "New York
Chicago Boston", etc. without the '|' above) and then enter the phrase
after it.

I can't seem to get it to put each city in the file on a new line and
then add the phrase that I want after it like it did before. The only
real difference in what I want now and what I had before was the '|'
character. Can someone help me fix this?
--
Posted via http://www.ruby-....

19 Answers

Siep Korteling

5/30/2008 11:42:00 PM

0

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-....

Siep Korteling

5/30/2008 11:57:00 PM

0

Zoe Phoenix wrote:

>
>
> I tried to modify it so that it would separate not on the '|' character,
> but on a single space (such as in a list of cities like "New York
> Chicago Boston", etc. without the '|' above) and then enter the phrase
> after it.
>
Oh. Well, you will end up with the cities New, York, Chicago, Boston.

Sorry about that.

Siep
--
Posted via http://www.ruby-....

Zoe Phoenix

5/31/2008 12:02:00 AM

0

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-....

David A. Black

5/31/2008 1:06:00 AM

0

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.r... for details and updates!

7stud --

5/31/2008 1:57:00 AM

0

Siep Korteling wrote:
> 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(" ") .
>

str = "hello world goodbye"
arr = str.split()
p arr

--output:--
["hello", "world", "goodbye"]


> (split will also work with a regular expression, like in your code. It's
> faster

Wrong.

> and far more powerfull, but completely unreadable if you are not
> familiar with it. In your code split("|") works.)
>

Ahh, but the governing principle in the Ruby community is to make a Ruby
script look as much like a Perl script as possible--efficiency be
damned. So who in their right mind would pass up a chance to use a
hieroglyphic regex like: /\|/ in their code. That's art.
--
Posted via http://www.ruby-....

Zoe Phoenix

5/31/2008 3:16:00 AM

0

Ohhh, I see... thank you so much! :-D
--
Posted via http://www.ruby-....

Tristin Davis

5/31/2008 3:56:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

No need to waste space being facetious.


On Fri, May 30, 2008 at 10:16 PM, Zoe Phoenix <dark_sgtphoenix@hotmail.com>
wrote:

> Ohhh, I see... thank you so much! :-D
> --
> Posted via http://www.ruby-....
>
>

David A. Black

5/31/2008 10:08:00 AM

0

Hi --

On Sat, 31 May 2008, 7stud -- wrote:

> Siep Korteling wrote:
>> 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(" ") .
>>
>
> str = "hello world goodbye"
> arr = str.split()
> p arr
>
> --output:--
> ["hello", "world", "goodbye"]
>
>
>> (split will also work with a regular expression, like in your code. It's
>> faster
>
> Wrong.

My benchmarks suggest that it's a little faster (about 10%).


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.r... for details and updates!

David A. Black

5/31/2008 10:08:00 AM

0

Hi --

On Sat, 31 May 2008, Siep Korteling wrote:

> 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(" ") .

Actually without an argument it will split on any amount of
whitespace. I think you're thinking of how strings enumerate, which is
(by default) as lines:

"abc\ndef".to_a # ["abc\n", "def"]


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.r... for details and updates!

Dave Bass

5/31/2008 11:34:00 AM

0

7stud -- wrote:
> Ahh, but the governing principle in the Ruby community is to make a Ruby
> script look as much like a Perl script as possible--efficiency be
> damned. So who in their right mind would pass up a chance to use a
> hieroglyphic regex like: /\|/ in their code. That's art.

Larry Wall (Perl supremo) has called this sort of thing LTS: leaning
toothpick syndrome. But Perl's syntax allows you to write a regexp as
m(\|), which is a bit clearer. Or you can use the quotemeta() function
to add a backslash for you.

But this is Ruby, not Perl! :-D

Coming from 10 years of Perl coding, I wish Ruby were *less* Perl-like,
as it can get confusing, especially when you're working in both
languages at the same time.
--
Posted via http://www.ruby-....