question to write \4 into file

C

Cheyne Li

Hi there,

I have problem with writng "\4" into a file, for example, I want to
write a string "aaa\4024_folder\mm" into a file test.cfg. But once the
string wrote into file, it is "aaa024_folder\mm". So, i do this,

string="aaa\4024_folder\mm"
string=string.sub(/\\4/,"\\4") the result is the same.

I tried string=string.sub(/\\4/,"\\4"), the result is showed a square

Any one has any idea to solve the problem? Thank you in advance.
 
7

7stud --

Cheyne said:
Hi there,

I have problem with writng "\4" into a file, for example, I want to
write a string "aaa\4024_folder\mm" into a file test.cfg. But once the
string wrote into file, it is "aaa024_folder\mm". So, i do this,

string="aaa\4024_folder\mm"
string=string.sub(/\\4/,"\\4") the result is the same.

I tried string=string.sub(/\\4/,"\\4"), the result is showed a square

Any one has any idea to solve the problem? Thank you in advance.

Use single quotes around your string?
 
7

7stud --

7stud said:
Use single quotes around your string?

Otherwise, keeping adding slashes to your match while trying to replace
it with "HELLO". Don't be surprised if it takes 9 slashes to get the
match. Once you get the match, then use your desired replacement and
keep adding slashes to it until it works.
 
R

Rob Biedenharn

I tried '\4' '\\4' not luck at all
--


as a literal string, you need to escape the \

string = "aaa\\4024_folder\\mm"
File.open('test.cfg', 'a') {|f| f.puts string }

This appends the string to the test.cfg file.

://tmp $ irb
irb> string = "aaa\\4024_folder\\mm"
=> "aaa\\4024_folder\\mm"
irb> File.open('test.cfg', 'a') {|f| f.puts string }
=> nil
irb> exit
://tmp $ cat test.cfg
aaa\4024_folder\mm


-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
B

Brian Candler

Cheyne said:
Hi there,

I have problem with writng "\4" into a file, for example, I want to
write a string "aaa\4024_folder\mm" into a file test.cfg. But once the
string wrote into file, it is "aaa024_folder\mm".

That's because \4 inside a double-quoted string is a single character,
with code 4.

If you want a backslash inside a double-quoted string, put two
backslashes.

irb(main):001:0> str = "aaa\\4024_folder\\mm"
=> "aaa\\4024_folder\\mm"
irb(main):002:0> puts str
aaa\4024_folder\mm
=> nil

If you are building filenames, note that even under Windows you can use
forward-slashes inside Ruby.

Inside a gsub replacement string it can be even more confusing, since a
backslash followed by 4 means "replace this with the fourth capture in
the match". So to replace x with \4, you need to do this

irb(main):003:0> str = "wxyz"
=> "wxyz"
irb(main):004:0> res = str.gsub(/x/, "\\\\4")
=> "w\\4yz"
irb(main):005:0> puts res
w\4yz
=> nil

Or use the block form of gsub, which doesn't treat backslash-N
specially.

irb(main):007:0> res = str.gsub(/x/) { "\\4" }
=> "w\\4yz"
irb(main):008:0> puts res
w\4yz
=> nil
 
C

Cheyne Li

7stud said:
Otherwise, keeping adding slashes to your match while trying to replace
it with "HELLO". Don't be surprised if it takes 9 slashes to get the
match. Once you get the match, then use your desired replacement and
keep adding slashes to it until it works.

Thanks. After puting 8 slashes, the path is correctely written in the
file...speachless...
 
B

Brian Candler

Cheyne said:
Thanks. After puting 8 slashes, the path is correctely written in the
file...speachless...

Then there is something else which is doing a third level of dequoting.
I can't say what this is, since you don't show your code.

Perhaps you are passing the string to a shell, e.g. using backticks,
%x{...}, system(), IO.popen or similar. In this case, the shell also
treats backslash sequences specially.

irb(main):001:0> str = "\\\\"
=> "\\\\"
irb(main):002:0> puts str
\\
=> nil
irb(main):003:0> system("echo #{str}")
\
=> true

Here, str is actually two characters (backslash followed by backslash).
But the shell treats this as a single backslash-escaped character.

You can avoid this problem by not getting the shell involved. If you
pass multiple arguments to system() then it invokes the target program
with the arguments exactly as given, without invoking the shell at all.

irb(main):004:0> system("echo", str)
\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,939
Members
47,484
Latest member
JackRichard

Latest Threads

Top