Jump to content

Print a variable's value to the command line without repetition


Recommended Posts

Posted

Hi there,

 

I'm trying to figure out how to print a variable's value to the command line without having it repeat.

 

For instance, say the variable MyVar holds this string: 12 x 5 which I would like to print to the command line.

 

When I try this: (Princ MyVar), the information prints twice: 12 x 5 "12 x 5"

I've been playing around with the control codes but I haven't been able to avoid the return variable.

 

In my murky understanding, I know that this has something to do with the way lisp evaluates expressions.

 

Is there a simple way around this?

 

Thanks for any advice

 

AJS

Posted

use the (princ) at the end

also lookup prin1,prompt and print

Posted

Correct me if I am wrong, but I believe the reason for the repetition is that the action of using "princ" returns the value of the variable or string supplied to it, but also princ itself will print the supplied variable to the command line.

 

When you follow (princ [string] ) with another (princ), the last return is a repeated empty string, as no value is assigned to princ and hence no value is printed.

 

Therefore this suppresses the return of the first princ and only the value assigned to the first princ is printed.

 

:unsure: *Hoping this info is correct* :unsure:

 

Cheers

 

Lee

Posted

JohnM beat me to it, but you might want a further explanation. Dammit Lee, I've been beaten twice!

 

ajs, lpsiefert's solution will work fine, and here's why. The default behavior of an Autolisp program is to display the value of the last function called. The usual method to avoid the unwanted display is to close the program with a (princ) without argument. If you run this program :

 

(defun c:tmv (/ myvar1 myvar2)

(setq myvar1 "12 x 5")

(setq myvar2 "5 x 12")

(princ myvar1)

(terpri)

(princ myvar2)

)

 

it displays :

 

12 x 5

5 x 12

"5 x 12"

 

If you add (princ) as in :

 

(defun c:tmv (/ myvar1 myvar2)

(setq myvar1 "12 x 5")

(setq myvar2 "5 x 12")

(princ myvar1)

(terpri)

(princ myvar2)

(princ)

)

 

it displays :

 

12 x 5

5 x 12

 

I hope this helps.

Posted

As quoted from ACAD:

 

When entered from VLISP, the prompt function displays a message (a string) in the AutoCAD Command window and returns nil to the VLISP Console window.

 

The princ, prin1, and print functions all display an expression (not necessarily a string) in the AutoCAD Command window and return the expression to the VLISP Console window.

 

Optionally, these functions can send output to a file. The differences are as follows:

  • princ displays strings without the enclosing quotation marks.

  • prin1 displays strings enclosed in quotation marks.

  • print displays strings enclosed in quotation marks but places a blank line before the expression and a space afterward.

The following examples demonstrate the differences between the four basic output functions and how they handle the same string of text.

 

If you enter the examples from VLISP, the text following prints is what you see at the AutoCAD Command prompt; text following returns appears within the VLISP Console window or within an application.

 

See the following section for an explanation of the control characters used in the example.

 

(setq str "The \"allowable\" tolerance is \261 \274\"")

 

 

(prompt str) prints The "allowable" tolerance is VLALBASb.gif 1/4"

 

and returns nil

 

(princ str) prints The "allowable" tolerance is VLALBASb.gif 1/4"

 

and returns "The \"allowable\" tolerance is VLALBASb.gif 1/4\""

 

(prin1 str) prints "The \"allowable\" tolerance is VLALBASb.gif 1/4""

 

and returns "The \"allowable\" tolerance is VLALBASb.gif 1/4\""

 

(print str) prints "The \"allowable\" tolerance is VLALBASb.gif 1/4""

 

and returns "The \"allowable\" tolerance is VLALBASb.gif 1/4\""

 

Note that the write-char and write-line functions can also display output to a Command window. Refer to the AutoLISP Reference for information on these functions.

Posted

the long version

 

 
(DEFUN C:PV ()
  (setq acaddoc (vla-get-activedocument (vlax-get-acad-object)))
  (setq util (vla-get-utility acaddoc))
(vla-prompt util "\nabc")
  (princ)
  )

Posted

Thanks for the solution!

 

On a personal note what I would like to say is that there's something about learning Lisp that really meshes with the way that I internally organize information.

 

I'm somewhat competent in Crystal Reports but the knowledge that I gain in Lisp seems to move the ball forward in a manner that imparts information better than any other programming language that I've learned.

 

So I guess what I'm trying to say is that the advice that is generously provided to me on this forum is not only practical but a genuine pleasure.

 

Thanks so much,

 

AJS

Posted

No problem AJS - normally I learn something new too when posting information :)

 

Cheers

 

Lee

Posted

Organization is the key to drawing and programming as well as work and life in general.

Lisp stands for list processing and it executes line per line so there is no jumping around, point A to point B. if you train your self to think like that you will be able to figure things out more quickly and correctly and become more successful. I can’t tell you how many draftsmen and programmers I have dealt with that are so un-organized that they spend more time chasing their tails than getting the job done.

Posted
Organization is the key to drawing and programming as well as work and life in general.

Lisp stands for list processing and it executes line per line so there is no jumping around, point A to point B. if you train your self to think like that you will be able to figure things out more quickly and correctly and become more successful. I can’t tell you how many draftsmen and programmers I have dealt with that are so un-organized that they spend more time chasing their tails than getting the job done.

 

Very well put John,

 

Having good practice with LISP programming urges your to think a lot more logically and break things down to the rules they are built on. - This not only helps in other tasks, such as those involving Mathematics, but also, as John says, organisational skills are improved greatly as your mind sets things out in a logical manner.

 

Also, because programming is purely a problem-solving skill, it helps you think clearer and sometimes more laterally to solve everyday problems :)

 

Just adding my $0.02. :)

 

Lee

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...