Author Topic: autolisp question  (Read 2250 times)

0 Members and 1 Guest are viewing this topic.

lc_shi

  • Guest
autolisp question
« on: October 17, 2004, 08:55:33 PM »
i am a lisp biginner. I'm puzzled by the difference between prinl\princ\print. pls teach me.

SMadsen

  • Guest
autolisp question
« Reply #1 on: October 18, 2004, 05:41:51 AM »
The main difference is between PRINC and PRIN1/PRINT in that PRINC interprets control characters into their ASCII equivalents while PRIN1 and PRINT print the control characters (expand the control characters):

Code: [Select]
(setq i 0 a 10)
(while (< i a)
  (princ (chr i))
  (setq i (1+ i))
)
       10

(setq i 0 a 10)
(while (< i a)
  (prin1 (chr i))
  (setq i (1+ i))
)
"""\001""\002""\003""\004""\005""\006""\007""\010""\t"10

When you deal with 'normal' printable characters, the difference is merely shown by printing a string with double quotes:

Command: (princ "abc")(princ)
abc

Command: (prin1 "abc")(prin1)
"abc"

PRIN1 and PRINT work in much the same way, except PRINT adds a newline to the beginning and a space to the end when it prints (only to the output, not to the return value):

Code: [Select]
(setq i 0 a 10)
(while (< i a)
  (print (chr i))
  (setq i (1+ i))
)

""
"\001"
"\002"
"\003"
"\004"
"\005"
"\006"
"\007"
"\010"
"\t" 10


Notice that each output is placed on a new line (also the empty line between the last parenthesis and the output is a newline character). One might think that PRINT is more appropriate than PRINC when outputting prompts in a program because it includes a newline character - thus avoiding an explicit "\n" character - but it's only really useful for output to files or to build strings for programming purposes where control characters need to be expanded. This example using PRINT looks pretty weird when output to the text screen:

Code: [Select]

(defun C:TEST ()
  (print "Select lines")
  (if (setq sset (ssget '((0 . "LINE"))))
    (print (strcat (itoa (sslength sset)) " line(s) selected"))
  )
  (princ)
)


Command: test
"Select lines"
Select objects: 1 found
Select objects:
"1 line(s) selected"


The same happens with PRIN1. Replacing PRINT above with PRIN1 will look like this:

Command: test "Select lines"
Select objects: 1 found
Select objects:  "1 line(s) selected"

lc_shi

  • Guest
autolisp question
« Reply #2 on: October 18, 2004, 08:57:12 PM »
it's great. thank you!