Problem: Newbie with a question on file arrays and file insertion


(Kurt Euler) #1

Ruby pros:

I am a Ruby newbie, and would appreciate help with the following to jump-start a project.

1)- How can I read the contents of a text file into a two-dimentional array, such that one dimension are the rows of text and the other dimension are fields that occur on each line as delimited by some character.

 Note: A tutorial I'm using shows that I can use the readlines method 
 to create a one-dimensional array, with each line from the source file
 an element. It seems like I ought to be able to use the same or
 similar method to include the second dimension (delimited fields in 
 each row).

2)- Say I have text files FileA and FileB. FileA contains a text tag in the somewhere in it called, say, “”. How can I:

 - locate the tag <foobar> in FileA and
 - replace <foobar> with the entire contents of FileB.

 In short, I want to insert FileB in FileA where the <foobar> tag is,
 then delete the tag.

Thanks!

-Kurt Euler


(Stefan Schmiedl) #2

Ruby pros:

I am a Ruby newbie, and would appreciate help with the following to jump-start a project.

1)- How can I read the contents of a text file into a two-dimentional array, such that one dimension are the rows of text and the other dimension are fields that occur on each line as delimited by some character.

 Note: A tutorial I'm using shows that I can use the readlines method 
 to create a one-dimensional array, with each line from the source file
 an element. It seems like I ought to be able to use the same or
 similar method to include the second dimension (delimited fields in 
 each row).

File(“whatever”).readlines.collect { |line|
line.split “separator”
}

comes to mind here.

2)- Say I have text files FileA and FileB. FileA contains a text tag in the somewhere in it called, say, “”. How can I:

 - locate the tag <foobar> in FileA and
 - replace <foobar> with the entire contents of FileB.

 In short, I want to insert FileB in FileA where the <foobar> tag is,
 then delete the tag.

if the files are not too large, you could read them as a whole
and then just do string substitution.

a = File(“a”).read
b = File(“b”).read

a.gsub(//, b)

Be warned: this is written on a sunday morning before breakfast :slight_smile:

s.

···

On Sun, 02 Jun 2002 07:36:57 GMT, Kurt Euler keuler@portal.com wrote:


(Nobuyoshi Nakada) #3

Hi,

2)- Say I have text files FileA and FileB. FileA contains a
text tag in the somewhere in it called, say,
"". How can I:

 - locate the tag <foobar> in FileA and
 - replace <foobar> with the entire contents of FileB.

 In short, I want to insert FileB in FileA where the <foobar> tag is,
 then delete the tag.

if the files are not too large, you could read them as a whole
and then just do string substitution.

a = File(“a”).read
b = File(“b”).read

a.gsub(//, b)

If “a” is large:

require 'tempfile’
require ‘ftools’

t = Tempfile.new(“foobar”)
b = nil
IO.foreach(“a”) do |line|
t.print(line.gsub(//) do
b ||= File.open(“b”) {|b| b.read}
end)
end
File.copy(t.path, “a”)

Be warned: this is written on a sunday morning before breakfast :slight_smile:

Too, except that it’s an evening here. :slight_smile:

···

At Sun, 2 Jun 2002 17:09:07 +0900, Stefan Schmiedl wrote:


Nobu Nakada