Read data line by line

could anybody please give me an example of how to read data from a text
file line by line so that manipulations can be made on each and
everyline and copy those to other file?

On 4/7/07, Krishna V. [email protected] wrote:

could anybody please give me an example of how to read data from a text
file line by line so that manipulations can be made on each and
everyline and copy those to other file?


Posted via http://www.ruby-forum.com/.

http://www.ruby-forum.com/topic/103371#new

Harry

http://www.kakueki.com/ruby/list.html
Japanese Ruby List Subjects in English

On Apr 6, 2007, at 11:51 PM, Krishna V. wrote:

could anybody please give me an example of how to read data from a
text
file line by line so that manipulations can be made on each and
everyline and copy those to other file?

File.open(“output.txt”, “w”) do |output|
File.foreach(“input.txt”) do |line|
# change line here…
output << line
end
end

Hope that helps.

James Edward G. II

James Edward G. II wrote:

James Edward G. II

As well as something like this

lines=File.readlines(“filename”)
lines.collect!{|l| do something with l and give it back}
File.open(“output”,“w”){|f| f<<lines.join("\n")}

Pedantic, I know :slight_smile:
V.-

Thanks a lot James

On Apr 9, 2007, at 6:55 AM, Vassilis R. wrote:

Pedantic, I know :slight_smile:

The problem with your solution is that it slurps all the data into
memory at once. For large data sets, this may not be an option.

My solution only reads one line at a time and thus is a lot more
memory friendly.

James Edward G. II