0

i would like to escape the backslash in swipl when using string_concat.

Scenario 1-

string_concat('stack', ' overflow', Result).
Result = "stack overflow" 

Scenario 2-

string_concat('stack', ' \=overflow', Result).
Syntax error: Undefined character escape in quoted atom or string: `\='

Scenario 3-

string_concat('stack', ' \\=overflow', Result).
Result = "stack \\=overflow" 

Now, scenario1 behaves as expected. I am faced with a situation explained in scenario2, where in i need to concatenate a string that contains a backslash and equalto. ie, string1 has just text, string2 has text with \= also inside it, and my result should be string1 and string2 concatenated. However, using just \= as in scenario2 results in an error asking me to escape the sequence. Now, escaping as in scenario3 gives me two backslashes and the equal sign. I do not want two backslashes.

My output should be exactly stack \=overflow. Is there some escape sequence or method that I am missing here?

Thanks!

user1644208
  • 105
  • 5
  • 12

1 Answers1

1

Double backslash are there just for display:

?- string_concat(stack, ' \\=overflow', X), writeln(X).
stack \=overflow
X = "stack \\=overflow".

but you can change the behaviour using this flag:

?- set_prolog_flag(character_escapes,false).
true.

?- writeln('stack \=overflow').
stack \=overflow
true.

with default value true I get the error you report:

?- set_prolog_flag(character_escapes,true).
true.

?- writeln('stack \=overflow').
ERROR: Syntax error: Undefined character escape in quoted atom or string: `\='
ERROR: writeln('stack \
ERROR: ** here **
ERROR: =overflow') . 

Anyway, I think you should reset its value to default when done, or double check your SW for unwanted side effects on literals.

HTH

CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • thanks for the flag help. however, i see that when using string_concat, I still get the 2 backslashes. See code: set_prolog_flag(character_escapes, false), string_concat('stack', '\=overflow', X), writeln(X). Gives output as: stack\=overflow X = "stack\\=overflow" Since I need to use X in other places in the program, it shouldn;t have the \\=, instead it should only have \=. Any suggestion on this? Thanks. – user1644208 Sep 16 '12 at 09:00