Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Parsing an text file
Johnathan Wagner
4/1/2007 12:43:00 AM
Hi,
I cannot find a way to easily parse a text file. I am just starting to
learn ruby so please bear with me.
My text file looks like this:
0 8 2 9 3 0 0 4
9 2 8 3 9 3 0 2
2 3 4 9 8 8 9 0
Basically just numbers and spaces.
So far, I know how to read the file and put each line into an array.
def readfile(file)
arr = IO.readlines(file)
parsing(arr)
end
def parsing(arr)
string1 = arr[0].to_s
string2 = arr[1].to_s
string3 = arr[2].to_s
ans.each_byte {|b|p b.chr}
end
What I wanted to do is, for example, want to find all 8's and change it
to 5's.
I am stuck on how I can use the string and search each char's to find
the 8's and then change it. Also I would like to know if there is a for
loop or something to go through each array and store it into a new
string because there might be more than 3 lines. I have search through
documentations and trying to find examples but there doesn't seem to be
much help.
Any help, especially examples, would be appreciate. Thanks in advance.
--
Posted via
http://www.ruby-...
.
3 Answers
Drew Olson
4/1/2007 12:48:00 AM
0
Johnathan Wagner wrote:
> What I wanted to do is, for example, want to find all 8's and change it
> to 5's.
> I am stuck on how I can use the string and search each char's to find
> the 8's and then change it. Also I would like to know if there is a for
> loop or something to go through each array and store it into a new
> string because there might be more than 3 lines. I have search through
> documentations and trying to find examples but there doesn't seem to be
> much help.
>
> Any help, especially examples, would be appreciate. Thanks in advance.
The code below reads through your file, replaces 8's with 5's and then
writes out the lines to a new file:
File.open("newfile,txt","w") do |out|
File.open("myfile.txt").each do |line|
out << line.gsub("8","5")
end
end
--
Posted via
http://www.ruby-...
.
Johnathan Wagner
4/1/2007 1:02:00 AM
0
Thanks for your fast answer.
One last question, if you can help me.
How can I compare each characters in a string?
Drew Olson wrote:
> Johnathan Wagner wrote:
>> What I wanted to do is, for example, want to find all 8's and change it
>> to 5's.
>> I am stuck on how I can use the string and search each char's to find
>> the 8's and then change it. Also I would like to know if there is a for
>> loop or something to go through each array and store it into a new
>> string because there might be more than 3 lines. I have search through
>> documentations and trying to find examples but there doesn't seem to be
>> much help.
>>
>> Any help, especially examples, would be appreciate. Thanks in advance.
>
> The code below reads through your file, replaces 8's with 5's and then
> writes out the lines to a new file:
>
> File.open("newfile,txt","w") do |out|
> File.open("myfile.txt").each do |line|
> out << line.gsub("8","5")
> end
> end
--
Posted via
http://www.ruby-...
.
Drew Olson
4/1/2007 1:08:00 AM
0
Johnathan Wagner wrote:
> Thanks for your fast answer.
>
> One last question, if you can help me.
>
> How can I compare each characters in a string?
You can compare each character individually by using the each_byte
method, however that gives you the ASCII representation of the
character. In your case I would split each line by spaces to get each
number as a substring like so:
File.open("myfile.txt").each do |line|
line.split(" ").each do |num|
# do your work here
end
end
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Parsing an text file
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password