murphy@ibma0.cs.uiuc.edu (michael r murphy) (03/15/91)
I'm rather new to using Lisp and I am having a problem with strings. I would like to read in a C string and keep the escape characters as is. I have enclosed a sample of my problem. What I would like is: <cl> (setf foo "\nThis is a c string\n") "\nThis is a c string\n" I just want to get the \ in there without it being interpreted as an escape. I can then handle printing it out. Help. Script started on Fri Mar 15 09:46:15 1991 murphy|ibma0|[51]% acl Allegro CL 3.2.beta.1 [IBM RS/6000] (0/0/0) Copyright (C) 1985-1990, Franz Inc., Berkeley, CA, USA <cl> (setf foo "\nThis is a c string\n") "nThis is a c stringn" <cl> (setf foo |"\nThis is a c string\n"|) Error: Attempt to take the value of the unbound symbol |"nThis is a c stringn"| [1] <cl> :exit ; Exiting Lisp murphy|ibma0|[52]% ^D script done on Fri Mar 15 09:47:00 1991 murphy@cs.uiuc.edu OR mick@uiuc.edu
barmar@think.com (Barry Margolin) (03/16/91)
In article <27E0EDEE.49E5@ibma0.cs.uiuc.edu> murphy@ibma0.cs.uiuc.edu (michael r murphy) writes: >I would like to read in a C string and keep the escape characters >as is. I have enclosed a sample of my problem. What I would like >is: > ><cl> (setf foo "\nThis is a c string\n") > >"\nThis is a c string\n" > >I just want to get the \ in there without it being interpreted as >an escape. I can then handle printing it out. Help. You have to double the slash, i.e. (setf foo "\\nThis is a c string\\n") ><cl> (setf foo |"\nThis is a c string\n"|) >Error: Attempt to take the value of the unbound symbol |"nThis is a c stringn"| |...| is a symbol, not a string, so you have to use single-quote to prevent it from being evaluated. Also, backslash is interpreted inside vertical bars (to allow you to include a vertical bar as a character in a symbol name), so you still have to double it: (setf foo '|"\\nThis is a c string\\n"|) Note that in both cases there is only one backslash in the actual string. The doubling is only necessary during I/O. Notice: > (setq *string* "\\n") "\\n" > (length *string*) 2 > (coerce *string* 'list) (#\\ #\n) > (write-string *string*) \n "\\n" -- Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar