[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

read data line by line

Krishna Vutukuru

4/7/2007 4:51:00 AM

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

4 Answers

Harry

4/7/2007 5:03:00 AM

0

On 4/7/07, Krishna Vutukuru <krishna.vutukuru@consultrcg.com> 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-....
>
>

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

Harry

--

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

James Gray

4/7/2007 3:27:00 PM

0

On Apr 6, 2007, at 11:51 PM, Krishna Vutukuru 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 Gray II

Vassilis Rizopoulos

4/9/2007 11:55:00 AM

0

James Edward Gray II wrote:
> On Apr 6, 2007, at 11:51 PM, Krishna Vutukuru 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 Gray 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 :)
V.-
--
http://www.braveworl...


James Gray

4/9/2007 12:06:00 PM

0

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

> James Edward Gray II wrote:
>> On Apr 6, 2007, at 11:51 PM, Krishna Vutukuru 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 Gray 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 :)

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 Gray II