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