No output in my file `out.txt`,after finishing the script

Hi,

I wrote the below code:

need_lines = File.readlines("C:/Documents and Settings/rakshiar/My
Documents/Downloads/prod_bck_up.xml")[1000..1012]

File.open("C:/Documents and Settings/rakshiar/My
Documents/Downloads/out.txt", 'w') do |f|
f.write(need_lines)
end

But when the code has been finished,I am not getting any output in the
file `out.txt`.

Please help me,what wrong I did.

···

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

Given the brevity of the code I have to ask. What have you done to find out
what the problem is yourself?

(Perhaps "puts need_lines.size" at some point might be a good idea)

···

On 30 August 2013 10:10, Love U Ruby <lists@ruby-forum.com> wrote:

Hi,

I wrote the below code:

need_lines = File.readlines("C:/Documents and Settings/rakshiar/My
Documents/Downloads/prod_bck_up.xml")[1000..1012]

File.open("C:/Documents and Settings/rakshiar/My
Documents/Downloads/out.txt", 'w') do |f|
f.write(need_lines)
end

But when the code has been finished,I am not getting any output in the
file `out.txt`.

Please help me,what wrong I did.

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

Peter Hickman wrote in post #1120062:

Given the brevity of the code I have to ask. What have you done to find
out
what the problem is yourself?

my out.txt contains as below:

[" <body>\n", " <p>Hello World!</p>\n", " <p>Goodbye!</p>\n"]

But I want it to be as :

  <body>
    <p>Hello World!</p>
    <p>Goodbye!</p>

Here is my code:-

need_lines = File.readlines("C:/Documents and Settings/rakshiar/My
Documents/Downloads/Sample.xml")[1..3]

File.open("C:/Documents and Settings/rakshiar/My
Documents/Downloads/out.txt", 'w') do |f|
f.write(need_lines)
end
puts need_lines

···

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

Thanks I am done:-

need_lines = File.readlines("C:/Documents and Settings/rakshiar/My
Documents/Downloads/Sample.xml")[1..3]

File.open("C:/Documents and Settings/rakshiar/My
Documents/Downloads/out.txt", 'w') do |f|
f.write(need_lines.map(&:lstrip).join)
end

Exact output:-

<body>
<p>Hello World!</p>
<p>Goodbye!</p>

But I am open to get better suggestions from you people..

···

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

I don't know what your file looks like, and that "[328855..336623]" bit
looks dodgy to me, but I can suggest an alternative:

File.write("C:/Documents and Settings/My Documents/Downloads/out.xml",
need_lines.join.gsub( /^\s+/, '' ) )

It should be faster since it's just editing one String, rather than
mapping an Array. Your final output is a single String anyway, so
there's no need for the Array.

It might need a bit of tweaking ( maybe /^[\t ]+/ ) if you need
completely blank lines in your output.

···

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

Write is expecting a string you gave it an array so it converted the array
into a string. It did what you asked of it.

File.open("C:/Documents and Settings/rakshiar/My
Documents/Downloads/out.txt", 'w') do |f|
  need_lines.each do |line|
    f.write(line)
  end
end

Is what you could do in this situation.

Honestly given the shortness of the code you should have been able to debug
this yourself. You have been on this list too long to use being new to Ruby
as an excuse. Put some effort into this please.

Peter Hickman wrote in post #1120065:

Write is expecting a string you gave it an array so it converted the
array
into a string. It did what you asked of it.

File.open("C:/Documents and Settings/rakshiar/My
Documents/Downloads/out.txt", 'w') do |f|
  need_lines.each do |line|
    f.write(line)
  end
end

Yes you are right! I have fixed my code. But the above one will copy all
the lines,where I want to select some lines as per the different
scenarios. Accordingly I wrote a code. If any better advice above my
code,you have,please advice. I am always open to learn ... :slight_smile:

···

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

Peter Hickman wrote in post #1120065:

Honestly given the shortness of the code you should have been able to
debug
this yourself. You have been on this list too long to use being new to
Ruby
as an excuse. Put some effort into this please.

Although my code is giving exactly what I want.. But taking too much
time means 2 min for the blow scenarios:

# this code will help to extract the validation as needed

need_lines = File.readlines("C:/Documents and Settings/My
Documents/Downloads/prod.xml")[328855..336623]

File.open("C:/Documents and Settings/My Documents/Downloads/out.xml",
'w') do |f|
f.write(need_lines.map(&:lstrip).join)
end
puts "done"

Any suggestion to make it speedy ?

···

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

You are reading a large file into memory. Ruby will have to build an array
to store at least 336,623 lines of text. One line at a time. This takes
time.

The easiest way to make something fast is not to do anything unnecessary.
Like loading the contents of of a file into memory just so you can write
out a fraction of them.

Peter Hickman wrote in post #1120073:

You are reading a large file into memory. Ruby will have to build an
array
to store at least 336,623 lines of text. One line at a time. This takes
time.

The easiest way to make something fast is not to do anything
unnecessary.
Like loading the contents of of a file into memory just so you can write
out a fraction of them.

The source prod.xml has numerous number of lines of xml. I need to
extract part of it,put it in another file. Then I will do some change in
the xml,as the requirement came to me. I am doing this via script,as
manual selection need too much time and Its error prone. I am basically
selecting all the content inside of a particular <form>..</form>.But the
script is taking time,seems I am taking the content manually. But I need
each xml code to be in a single line,exactly the same as the source,so
that I can perform in my editor Ctrl+F and CTRL+H...

Could you codify your suggestion, @Peter?

···

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