Forum: Ruby Parsing an text file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Johnathan W. (Guest)
on 2007-04-01 04:43
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.
Drew O. (Guest)
on 2007-04-01 04:47
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
Johnathan W. (Guest)
on 2007-04-01 05:02
Thanks for your fast answer.

One last question, if you can help me.

How can I compare each characters in a string?

Drew O. 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
Drew O. (Guest)
on 2007-04-01 05:08
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
This topic is locked and can not be replied to.