TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: mailmaverick on December 06, 2015, 07:50:14 AM

Title: Difference Between PRINT, PRINC & PRIN1
Post by: mailmaverick on December 06, 2015, 07:50:14 AM
Hi All,

This post is for information rather than a query. I had been using Autolisp since two years now but was always confused between PRINT, PRINC & PRIN1. Today I Googled it and found following explanation from IRNEB at following link :

http://forums.augi.com/showthread.php?111705-Difference-Between-PRINT-PRINC-amp-PRIN1 (http://forums.augi.com/showthread.php?111705-Difference-Between-PRINT-PRINC-amp-PRIN1)

It says :

Quote

They all 3 send a string to either the command line area (note it doesn't send a command, just a message). Or (if specified) to a file.
The princ function sends a string after formatting any contol characters. E.g. (princ "\"this is quoted\"") will show as "this is quoted" on the command line.
The prin1 function does the same without formatting. E.g. (prin1 "\"this is quoted\"") will show as "\"this is quoted\"". This is usually used when sending to a file so you can later use the read function to get the same value. Also say you have a list of strings, it'll send the text to the file so a later read would produce the same list of strings - princ will ommit the quotes and the read would fail.
The print function does the same as the prin1, but prefixes the string with a newline "\n" (which is formatted). This so that you can send a control code string to a new line in a file in one instruction, otherwise you'd have to do a princ for the new line, then a prin1 for the data.
There's also the prompt function which does exactly the same as princ with 2 caveats:
It can't send to a file (only the command line)
It has to have some text to send to the command line. The princ can be used without any string - this is usually wanted when you end a defun with (princ) so nothing is displayed on the command line. Otherwise the last line of the defun will basically issue a prin1 with its value.
I tend to ignore prompt and use princ instead. But the other 2 have their uses and I've used them many a time.

This has helped me a lot in my understanding. So I thought I must post it here so that many others like me should get befitted from it.

Thanks to IRNEB for such simple explanation.

Title: Re: Difference Between PRINT, PRINC & PRIN1
Post by: Lee Mac on December 06, 2015, 12:32:47 PM
I would also add that the argument supplied to the prin* functions needn't strictly be a string - you can use such functions with any AutoLISP data type.

This may also be helpful for your understanding: About Displaying Messages (http://help.autodesk.com/view/ACD/2016/ENU/?guid=GUID-58AD9863-BB35-4982-9382-581FE8A5022C)
Title: Re: Difference Between PRINT, PRINC & PRIN1
Post by: roy_043 on December 07, 2015, 04:16:40 AM
It is worth mentioning that (prompt) returns nil. This can be useful.
See example below. I prefer solution 1. It is easier to read and maintain.

Solution 1:
Code - Auto/Visual Lisp: [Select]
  1.   (and
  2.     (or
  3.       (numberp var1)
  4.       (prompt "\nError: var1 is not a number ")
  5.     )
  6.     (or
  7.       (= 'str (type var2))
  8.       (prompt "\nError: var2 is not a string ")
  9.     )
  10.   )
  11.   (progn
  12.     ...
  13.   )
  14. )
  15.  

Solution 2:
Code - Auto/Visual Lisp: [Select]
  1. (if (numberp var1)
  2.   (if (= 'str (type var2))
  3.     (progn
  4.       ...
  5.     )
  6.     (princ "\nError: var2 is not a string ")
  7.   )
  8.   (princ "\nError: var1 is not a number ")
  9. )
Title: Re: Difference Between PRINT, PRINC & PRIN1
Post by: Lee Mac on December 07, 2015, 04:34:51 AM
See example below. I prefer solution 1. It is easier to read and maintain.

Maybe easier to maintain, but since the code relies on one having the knowledge that the prompt function returns nil, I disagree that it is easier to read (especially if viewed from a pseudocode perspective, for example by someone not versed in LISP).

I personally prefer the following structure for this form of checking:

Code - Auto/Visual Lisp: [Select]
  1.     (   (not (numberp var1))
  2.         (princ "\nError: var1 is not a number.")
  3.     )
  4.     (   (/= 'str (type var2))
  5.         (princ "\nError: var2 is not a string.")
  6.     )
  7.     (   ...   )
  8. )
Title: Re: Difference Between PRINT, PRINC & PRIN1
Post by: roy_043 on December 07, 2015, 06:11:03 AM
A (cond) based solution can indeed replace 'Solution 1'. I do use (cond) of course, but when validating user input I typically use a structure as demonstrated in 'Solution 1'. I may revise that habit though. :-)