I am trying to write a basic script to implement "silent comments"
In the example anything from "input.txt" that is enclosed between "--/"
and "/--" will not be output to "output.text" It appears that the
problem I am having is that the opening string is indexed from beginning
of file, whereas the closing string is indexed from beginning of line.
So I would like to figure out why.
Also when using Ruby1.9 there is an error message about using "each"
with a string that I need to find a workaround to.
any help is greatly appreciated. thanks in advance.
here is the code:
I am trying to write a basic script to implement "silent comments"
In the example anything from "input.txt" that is enclosed between "--/"
and "/--" will not be output to "output.text" It appears that the
problem I am having is that the opening string is indexed from beginning
of file, whereas the closing string is indexed from beginning of line.
So I would like to figure out why.
Also when using Ruby1.9 there is an error message about using "each"
with a string that I need to find a workaround to.
any help is greatly appreciated. thanks in advance.
here is the code:
infile = File.read("input.txt")
File.open("output.txt", "w") do |outfile|
outfile.puts infile.split(%r{\n?--/.*?/--}m).join
end
Two changes:
- I added an optional \n at the beggining of the matched area to cover
that case, although there might be other corner cases.
- I joined the array before writing it, because puts with an array
prints a newline between each element.
See if this works for you and test any other edge case regarding
carriage returns. You can print what the split returns to see what you
get in each case:
infile = File.read("input.txt")
File.open("output.txt", "w") do |outfile|
splitted = infile.split(%r{\n?--/.*?/--}m)
puts splitted.inspect
outfile.puts splitted.join
end
Jesus.
···
On Thu, Mar 25, 2010 at 9:09 PM, Jesse B. <jessebos@aol.com> wrote:
Thank you Jesus,
Your solution is much more elegant.
However it leaves a blank line where my string was. any idea how to
close that gap?
as you can see, there is the \n after opening text. The three \n at the
end were present in the input.txt, so no problem.
thank you for your time.
Jesús Gabriel y Galán wrote:
···
On Thu, Mar 25, 2010 at 9:09 PM, Jesse B. <jessebos@aol.com> wrote:
thanks again for all of your help!
infile = File.read("input.txt")
File.open("output.txt", "w") do |outfile|
outfile.puts infile.split(%r{\n?--/.*?/--}m).join
end
Two changes:
- I added an optional \n at the beggining of the matched area to cover
that case, although there might be other corner cases.
- I joined the array before writing it, because puts with an array
prints a newline between each element.
See if this works for you and test any other edge case regarding
carriage returns. You can print what the split returns to see what you
get in each case:
infile = File.read("input.txt")
File.open("output.txt", "w") do |outfile|
splitted = infile.split(%r{\n?--/.*?/--}m)
puts splitted.inspect
outfile.puts splitted.join
end
File.open "output.txt", "w" do |out|
out.write content
end
Kind regards
robert
···
2010/3/25 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:
On Thu, Mar 25, 2010 at 9:09 PM, Jesse B. <jessebos@aol.com> wrote:
Thank you Jesus,
Your solution is much more elegant.
However it leaves a blank line where my string was. any idea how to
close that gap?
i.e.
opening text
--/text to delete/--
closing text
now renders as:
opening text
closing text
and what I am going for is:
opening text
closing text
thanks again for all of your help!
infile = File.read("input.txt")
File.open("output.txt", "w") do |outfile|
outfile.puts infile.split(%r{\n?--/.*?/--}m).join
end
Two changes:
- I added an optional \n at the beggining of the matched area to cover
that case, although there might be other corner cases.
- I joined the array before writing it, because puts with an array
prints a newline between each element.
Thank You Robert,
I tried running this and unfortunately it shows the line break (newline)
with or without the leading space before the "comment".
So I think Jesus is unto something with the "joined the array before
writing it because puts with an array prints a newline between each
element."
Now I just need to figure out how to have it not do the newline when
there are spaces before the "comment"
thanks for your time.
Robert Klemme wrote:
···
2010/3/25 Jes�s Gabriel y Gal�n <jgabrielygalan@gmail.com>:
closing text
closing text
- I added an optional \n at the beggining of the matched area to cover
that case, although there might be other corner cases.
- I joined the array before writing it, because puts with an array
prints a newline between each element.
If you slurp in the whole file anyway then you can do
I think there's no solution to that. I mean, the requirement is to
remove what's inside the comments, and that leaves a line with a
single space. Both my solution and Robert's do exactly that (I think
Robert's solution is the better one).
What you are asking now is for an additional condition, that it might
be that if after removing the comments there's an empty line (a line
with only spaces), that should be removed too.
content = File.read "input.txt"
content.gsub! %r{--/.*?/--}m, ''
content.gsub! %r{\A\s+\z}, '' # untested and i think you might to
remove a \n in this case too, give it a try.
File.open "output.txt", "w" do |out|
out.write content
end
Or maybe the condition is to remove all the whitespace and \n that
surround the comments.
content = File.read "input.txt"
content.gsub! %r{[\n\s]*--/.*?/--[\n\s]*}m, '' #untested also, but
you get the idea.
File.open "output.txt", "w" do |out|
out.write content
end
Hope this helps,
Jesus.
···
On Fri, Mar 26, 2010 at 5:17 PM, Jesse B. <jessebos@aol.com> wrote:
Thank You Robert,
I tried running this and unfortunately it shows the line break (newline)
with or without the leading space before the "comment".
So I think Jesus is unto something with the "joined the array before
writing it because puts with an array prints a newline between each
element."
Now I just need to figure out how to have it not do the newline when
there are spaces before the "comment"
thanks for your time.
File.open "output.txt", "w" do |out|
out.write content
end
I used this for testing:
robert@fussel:~$ cat input.txt
1. before after
X1.1
before --/ comment
comment /-- after
X1.2
2. before
X2.1
before --/ comment
comment /--
X2.2
3. after
X3.1
--/ comment
comment /-- after
X3.2
4. -
X4.1
--/ comment
comment /--
X4.2
5. end
robert@fussel:~$
Cheers
robert
···
On 03/26/2010 09:48 PM, Jesús Gabriel y Galán wrote:
On Fri, Mar 26, 2010 at 5:17 PM, Jesse B. <jessebos@aol.com> wrote:
Thank You Robert,
I tried running this and unfortunately it shows the line break (newline)
with or without the leading space before the "comment".
So I think Jesus is unto something with the "joined the array before
writing it because puts with an array prints a newline between each
element."
Now I just need to figure out how to have it not do the newline when
there are spaces before the "comment"
thanks for your time.
I think there's no solution to that. I mean, the requirement is to
remove what's inside the comments, and that leaves a line with a
single space. Both my solution and Robert's do exactly that (I think
Robert's solution is the better one).
What you are asking now is for an additional condition, that it might
be that if after removing the comments there's an empty line (a line
with only spaces), that should be removed too.
content = File.read "input.txt"
content.gsub! %r{--/.*?/--}m, ''
content.gsub! %r{\A\s+\z}, '' # untested and i think you might to
remove a \n in this case too, give it a try.
File.open "output.txt", "w" do |out|
out.write content
end
Or maybe the condition is to remove all the whitespace and \n that
surround the comments.
content = File.read "input.txt"
content.gsub! %r{[\n\s]*--/.*?/--[\n\s]*}m, '' #untested also, but
you get the idea.
File.open "output.txt", "w" do |out|
out.write content
end