AI Zone Admin Forum Add your forum

CHATBOT MEETUP CHATBOT MEETUP CHATBOT MEETUP CHATBOT CHATBOT MEETUP CHATBOT MEETUP CHATBOT MEETUP CHATBOT MEETUP

On June 28th, 2017 we're organizing our next Chatbot Meetup in Amsterdam, The Netherlands. Feel free to join!'

New line
 
 

Hi,

if I have a json string with special characters like ^’\n’
they get correctly printed when just outputted to the user, but wrong when printed to a file with ^log()

Has anyone had the same problem?
Is there any workaround?

 

 
  [ # 1 ]

I have an example to make my problem clearer:

$$test = ^'line \n another line '
^log(FILE TestLog.txt $$test

leads to

line
another line

but should look like

line \n another line

Using an active string directly however is working:

^log(FILE TestLog.txt ^'line \n another line '

But that doesn’t solve my problem..

 

 

 
  [ # 2 ]

Tobias, depending on how you are reading the “TestLog.txt” file, the \n might be automatically converted in your editor even though it is correct in the file.


This might be what you want though:

var data = ‘{"count" : 1, "stack" : "line\\nanother lne"}’;

(You need to escape the “\” in your string (turning it into a double-”\”), otherwise it will become a newline in the JSON source, not the JSON data.)

 

 
  [ # 3 ]
Merlin - Jan 17, 2017:

Tobias, depending on how you are reading the “TestLog.txt” file, the \n might be automatically converted in your editor even though it is correct in the file.

No, it’s not correct in the file. There is a newline char, there should be the chars ‘\’ and ‘n’

As I said, it’s working if I don’t use a variable but parse the active json string direct to ^log

Merlin - Jan 17, 2017:

This might be what you want though:

var data = ‘{"count" : 1, "stack" : "line\\nanother lne"}’;

(You need to escape the “\” in your string (turning it into a double-”\”), otherwise it will become a newline in the JSON source, not the JSON data.)

I forgot to mention that I already tested this without success :/

 

 
  [ # 4 ]

Try to do a uri escape on the input.

 

 

 
  [ # 5 ]
Merlin - Jan 17, 2017:

Try to do a uri escape on the input.

How do I do that in Chatscript?

 

 
  [ # 6 ]

So on the original post, I don’t think I see the problem yet. This code works for me
topic: ~INTRODUCTIONS keep repeat (~emogoodbye ~emohello ~emohowzit name here )
#!x matches every time on startup of a new conversation
t: Welcome to?
u: ( test ) $_x = ^jsoncreate(object)
$_x.test = ^‘Here I am.\nWhere are you’
  $_x.test
^log(FILE TMP/x.txt $_x.test)

 

 
  [ # 7 ]
Bruce Wilcox - Jan 26, 2017:

So on the original post, I don’t think I see the problem yet. This code works for me
topic: ~INTRODUCTIONS keep repeat (~emogoodbye ~emohello ~emohowzit name here )
#!x matches every time on startup of a new conversation
t: Welcome to?
u: ( test ) $_x = ^jsoncreate(object)
$_x.test = ^‘Here I am.\nWhere are you’
  $_x.test
^log(FILE TMP/x.txt $_x.test)

Is there a \n- char in your textfile?
Or are there two chars: ‘\’ and ‘n’

Cause as far as I can see i need the latter.

with

^log(FILE TMP/x.txt $_x.test

I get the single \n char


and with

^log(FILE  TMP/x.txt ^'Here I am.\nWhere are you'

I get the two chars like I am trying to achieve

 

 

 
  [ # 8 ]

Sorry, not good with wikiformat.  My text had a backslash and an n in it.

 

 
  [ # 9 ]

I tested with the newest version of Chatscript, maybe we are having a mistunderstanding here.
Can you upload your textfile?

 

 
  [ # 10 ]

Problem still exists.

I have tested your example code once again and it prints a file with two lines in it.
What I need is having a file with one line and a \n in it.

To make this clearer, I need a valid newline for sending json to another program.
Right now the newline char isn’t part of the json string, but of the json source.

 

 
  [ # 11 ]

Here is the error message I get from my other program which tries to parse the json:

JsonMappingException: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value

 

 
  [ # 12 ]

the problem here is that “\n” is interpreted as a NEWLINE, but you need a literal “\” followed by the “n” character, yes? Is there any way for the other program to be configured such that it can convert some other symbol (”\ n”, for example - note the extra space) into the character string it currently accepts? Or possibly could it be configured to accept a different escape sequence other than “\n”? Given that \n has been recognised by virtually every program that manipulates text as a NEWLINE character, it’s odd that the program you’re using expects it to be there as straight text. this strikes me as wrong, somehow. raspberry

 

 
  [ # 13 ]

I do have the ability to tell my other program to accept this.
But for me what chatscript is doing is wrong, so I would rather like to fix that problem instead of working around it.

 

 
  [ # 14 ]

Myself being a web developer and working extensively with PHP, in addition to a number of Integrated Development Environments (IDE for short), I see the treatment of \n as a NEWLINE character to be a natural thing, so I would expect the way ChatScript handles it as being a non-issue. But this is just my opinion on the matter. If it’s wrong for you, then it’s wrong for you. I guess it boils down to how Bruce feels about it. cheese

For now I suggest the “workaround”, and see what Bruce wants to do with it. If he changes the behaviour, great! If not, then at least you have a way to deal with it.

 

 
  login or register to react