Page 1 of 1

Double quotes inside string

Posted: Fri Oct 17, 2008 1:11 am
by heras1985
Hi, my problem is the next
I recieve an string like this:
"<OMA>
<OMS cd="Computing" name="Homology"/>
<OMA>
<OMS cd="simplicial-group" name="Loop-Space"/>
<OMA>
<OMS cd="Simplicial-Set" name="sphere"/>
<OMI>4</OMI>
</OMA>
<OMI>2</OMI>
</OMA>
<OMI>2</OMI>
</OMA>"
so I have a problem, because if i try to read this string in Common Lisp i get
<OMA>
<OMS cd=
Exists some way to solve this problem, i.e, if i read it i would like to get
<OMA>
<OMS cd="Computing" name="Homology"/>
<OMA>
<OMS cd="simplicial-group" name="Loop-Space"/>
<OMA>
<OMS cd="Simplicial-Set" name="sphere"/>
<OMI>4</OMI>
</OMA>
<OMI>2</OMI>
</OMA>
<OMI>2</OMI>
</OMA>

Re: Double quotes inside string

Posted: Fri Oct 17, 2008 3:59 am
by lnostdal
it'd be easier if you posted a complete snippet of code which shows the problem

..some code that works as expected here:

Code: Select all

CL-USER> (with-input-from-string (stream "<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>")
           (read-line stream))

"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
T
CL-USER> (princ *)
<OMA><OMS cd="Computing" name="Homology"/></OMA>
"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
CL-USER> (prin1 *)
"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
CL-USER>

Re: Double quotes inside string

Posted: Fri Oct 17, 2008 4:03 am
by john.byrne
I believe all you need to do here is escape the quotes that are part of your string, with the backslash character ('\')

So, wherever you have something like this:

<OMS cd="Computing" name="Homology"/>

...change it to this:

<OMS cd=\"Computing\" name=\"Homology\"/>

This is common to most programming languages.

Re: Double quotes inside string

Posted: Fri Oct 17, 2008 4:08 am
by lnostdal
just to make sure there is no misunderstanding; one do not have to escape an actual data-source or the file content, if reading from a file:

Code: Select all

lnostdal@blackbox:~$ nano -w test.txt
lnostdal@blackbox:~$ cat test.txt
<OMA><OMS cd="Computing" name="Homology"/></OMA>
lnostdal@blackbox:~$ 

Code: Select all

CL-USER> (with-open-file (stream "/home/lnostdal/test.txt")
           (read-line stream))
"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
NIL
CL-USER> (princ *)
<OMA><OMS cd="Computing" name="Homology"/></OMA>
"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
CL-USER> 

Re: Double quotes inside string

Posted: Fri Oct 17, 2008 10:15 am
by smithzv
just to make sure there is no misunderstanding; one do not have to escape an actual data-source or the file content, if reading from a file:
Right, this is because you do not want to use the reader when you just want the string data out of it, you just want read-line (or maybe read-sequence in some cases). The reader does a lot of stuff like parsing the input and converting it into Lisp data, which includes trying to understand double quotes.

And if it is the case that you actually are working with lots of string literals in your code with double quotes in it, a library like cl-interpol (http://www.weitz.de/cl-interpol/) might help (for some well behaved input). It will certainly escape your quotes. Or you could define something like your own text block reader macro similar to the Python """ facility or a "here document".

Zach S