Jump to content

new line in character


john_

Recommended Posts

I'm just starting to learn this lisp stuff so please go easy one me.

I'm having a slight issue maybe someone can help me understand.

 

Command: (setq test (strcat "hello" "\n " "there"))

"hello\n there"

 

I read in the help documentation that \n is a new line

but it doesn't display as such with my example.

Am I misunderstanding how to use the new line character.

 

I'm just playing around learning this so it has no meaning but I was just trying to display

 

hello

there

 

Thanks

Link to comment
Share on other sites

Hi John,

 

"\n" is indeed the New line character, try running these to experiment:

 

(defun c:test ()
 (princ "\nMy Name is \nJohn")
 (princ))

 

Hope this helps :)

Link to comment
Share on other sites

Lee, does (princ) and (strcat) process control codes differently? I noticed a similar thing when I tried:

 

(strcat "\010" " " "\010")

 

That is to say, I couldn't princ it, I -had- to strcat it in order for it to work the way I wanted it to.

Link to comment
Share on other sites

Octal Codes seem to work for me -

 

for example:

 

(princ "\100Lee Mac")
(princ)

 

Returns:

 

@Lee Mac

 

But Control Characters are harder to test for, so I'm not sure.

Link to comment
Share on other sites

(strcat) doesn't evaluate the code, it just concatenates the strings.

 

(princ) evaluates the string control characters prior to displaying them

 

You can see the biggest differences between (prin1) (princ) and (print) when outputting to files

 

 [b][color=BLACK]([/color][/b]setq wf [b][color=FUCHSIA]([/color][/b]open [color=#2f4f4f]"text.txt"[/color] [color=#2f4f4f]"w"[/color][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]
 [b][color=BLACK]([/color][/b]princ [color=#2f4f4f]"\nPRINC call"[/color] wf[b][color=BLACK])[/color][/b]
 [b][color=BLACK]([/color][/b]write-line [color=#2f4f4f]" - "[/color] wf[b][color=BLACK])[/color][/b]
 [b][color=BLACK]([/color][/b]prin1 [color=#2f4f4f]"\nPRIN1 call"[/color] wf[b][color=BLACK])[/color][/b]
 [b][color=BLACK]([/color][/b]write-line [color=#2f4f4f]" - "[/color] wf[b][color=BLACK])[/color][/b]
 [b][color=BLACK]([/color][/b]print [color=#2f4f4f]"\nPRIN1 call"[/color] wf[b][color=BLACK])[/color][/b]
 [b][color=BLACK]([/color][/b]close wf[b][color=BLACK])[/color][/b]

-David

Link to comment
Share on other sites

  • 5 years later...

What I am having a hard time understanding is why the \n is needed before "My name is..." I have run

 

(defun c:test ()

(princ "\nMy Name is \nJohn")

(princ))

 

and

 

(defun c:test ()

(princ "My Name is \nJohn")

(princ))

 

without seeing any difference at all. I see several examples of this when learning LISP and it is very confusing to me. Isn't the first \n redundant? Why do we put it there?

Link to comment
Share on other sites

Using "\n" in larger routines will split lines between commands, providing similar behavior/appearance to native AutoCAD commands. You will not notice a difference in the example you posted.

 

Beeftimer, try running the following:

(defun c:test ()
   (setq e (car (entsel "\nSelect an entity : "))
         i (getpoint "Select a point : ") )
   (princ)
)

 

vs

 

(defun c:test ()
   (setq e (car (entsel "\nSelect an entity : "))
         i (getpoint "\nSelect a point : ") )
   (princ)
)

 

Do you notice the difference when progressing through the steps?

Link to comment
Share on other sites

Try alert also for messages.

 

(alert (strcat "Hello" "\nworld"))

 

This is my get a value using a dialouge. Code (ah:getval "Enter the size" 6 5) Getval is preloaded.

;; Input  Dialog box with variable title
;; By Ah June 2012
;; original concept by Alan JT
;; code (ah:getval title width limit)

(defun AH:Getval (title width limit / fo)
(setq fname "C://acadtemp//getval.dcl")
(setq fo (open fname "w"))
(write-line "ddgetval : dialog {" fo)
(write-line " : row {" fo)
(write-line ": edit_box {" fo)
(write-line (strcat "    key = "  (chr 34) "sizze" (chr 34) ";") fo)
(write-line  (strcat " label = "  (chr 34) title (chr 34) ";"  )   fo)
; these can be replaced with shorter value etc
;(write-line "     edit_width = 18;" fo) 
;(write-line "     edit_limit = 15;" fo)
(write-line width fo) 
(write-line limit fo)
(write-line "   is_enabled = true;" fo)        
(write-line "    }" fo)
(write-line "  }" fo)
(write-line "ok_cancel;}" fo)
(close fo)

(setq dcl_id (load_dialog  "c:\\acadtemp\\getval"))
(if (not (new_dialog "ddgetval" dcl_id))
(exit))
(action_tile "sizze" "(setq item  $value)(done_dialog)")
(mode_tile "sizze" 3)
(start_dialog)
; returns the value of item
)

Link to comment
Share on other sites

See if this helps :

 

(defun c:test ()
(princ "Line 1: ")
(princ "\nMy Name is \nJohn")
(princ))

 

 

(defun c:test ()
(princ "Line 2 : ")
(princ "My Name is \nJohn")
(princ))

 

 

-David

Link to comment
Share on other sites

Yes, I do see the difference very clearly with your examples. I understood the difference between the two, but my question was regarding why it was needed in the example that I posted? From what you said, I'm leaning that, yes, the first \n is, in fact, redundant and/or unnecessary in that example? Is it just standard practice to include it there so as to create good habits?

 

And thank you for your response.

 

Using "\n" in larger routines will split lines between commands, providing similar behavior/appearance to native AutoCAD commands. You will not notice a difference in the example you posted.

 

Beeftimer, try running the following:

(defun c:test ()
   (setq e (car (entsel "\nSelect an entity : "))
         i (getpoint "Select a point : ") )
   (princ)
)

vs

 

(defun c:test ()
   (setq e (car (entsel "\nSelect an entity : "))
         i (getpoint "\nSelect a point : ") )
   (princ)
)

Do you notice the difference when progressing through the steps?

Edited by Beeftimer
etiquette
Link to comment
Share on other sites

Yes, I do see the difference very clearly with your examples. I understood the difference between the two, but my question was regarding why it was needed in the example that I posted? From what you said, I'm leaning that, yes, the first \n is, in fact, redundant and/or unnecessary in that example? Is it just standard practice to include it there so as to create good habits?

And thank you for your response.

 

Exactly :thumbsup:

Link to comment
Share on other sites

my question was regarding why it was needed in the example that I posted? From what you said, I'm leaning that, yes, the first \n is, in fact, redundant and/or unnecessary in that example? Is it just standard practice to include it there so as to create good habits?

 

You can't know in advance the text which may have been previously printed to the command-line and which may obscure your printed message, hence it is usually wise to print to a new-line. For example, consider a case in which the user has evaluated your function from another program:

(defun c:test ()
(princ "My Name is \nJohn")
(princ))

[color=gray]Command:[/color] (progn (princ "\nSome random text") (c:test))
Some random textMy Name is
John

Versus:

(defun c:test ()
(princ "\nMy Name is \nJohn")
(princ))

[color=gray]Command:[/color] (progn (princ "\nSome random text") (c:test))
Some random text
My Name is
John

Link to comment
Share on other sites

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...