Author Topic: Storing variables  (Read 14887 times)

0 Members and 1 Guest are viewing this topic.

cadman6735

  • Bull Frog
  • Posts: 255
  • Life is all about the attitude.
Re: Storing variables
« Reply #15 on: March 31, 2011, 10:33:54 AM »

Why does the 'P' remaine for a return?  If I put double space vs a single space the 'P' disapears


Quote
(defun c:exdata ( / )

(setq lastent (entget (car (entsel))))

(regapp "SHEETDATA")
                                           
(setq
  exdata '((-3 ("SHEETDATA" (1000 . "SheetTitle"))))
)
 
(setq newent
  (append lastent exdata)
)

(entmod newent)

(princ)
)


(defun c:test ( / )

  (vl-load-com)

  (setq
    acadObject         (vlax-get-acad-object)
    acadActiveDocument      (vla-get-ActiveDocument acadObject)
    acadPaperSpace      (vla-get-PaperSpace acadActiveDocument)
    acadLayout         (vla-get-Layout acadPaperSpace)

  )

  (setq ss (ssget "_x" '((0 . "MTEXT" ) (-3 ("SHEETDATA")))))

  (setq
    ssEnt   (ssname ss 0)
    sheetTitle   (cdr (assoc 1 (entget ssEnt)))
  )

  (setq sheetTitle (vl-string-translate "\\P" " " sheetTitle))     <---  If I put a double space here the 'P' disapears 
  (setq sheetTitle (vl-string-translate "\\pxqc;" ""  sheetTitle))

    (print sheetTitle)

    (princ)
)
  Ability is what you're capable of doing.
  Motivation determines what you do.
  Attitude determines how well you do it.

    Lou Holtz
----------------------------------------------

  Revit Certified Professional

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Storing variables
« Reply #16 on: March 31, 2011, 10:40:14 AM »
Because vl-string-translate switches individual characters, look into the vl-string-subst function.
« Last Edit: March 31, 2011, 11:57:06 AM by Lee Mac »

cadman6735

  • Bull Frog
  • Posts: 255
  • Life is all about the attitude.
Re: Storing variables
« Reply #17 on: March 31, 2011, 11:12:52 AM »
If it is individual then why does it work on

Code: [Select]
(setq sheetTitle (vl-string-translate "\\pxqc;" ""  sheetTitle))  the entire "\\pxqc;" is removed

and why does it remove both \\ but leave the P?

Yes, I am starting to look in to the function   (vl-string-subst)  trying to learn how to loop thru a string right now.


  Ability is what you're capable of doing.
  Motivation determines what you do.
  Attitude determines how well you do it.

    Lou Holtz
----------------------------------------------

  Revit Certified Professional

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Storing variables
« Reply #18 on: March 31, 2011, 11:24:34 AM »
If it is individual then why does it work on

Code: [Select]
(setq sheetTitle (vl-string-translate "\\pxqc;" ""  sheetTitle))  the entire "\\pxqc;" is removed

I'm guessing because it doesn't determine which character to translate to which, since there is no destination character set, hence it just translates them all to empty strings.

EDIT: On testing, it doesn't translate any characters for me:

Code: [Select]
_$ (vl-string-translate "\\pxqc;" "" "CAD\\pxqc;Man")
"CAD\\pxqc;Man"

Which is more likely what I would've expected.

and why does it remove both \\ but leave the P?

"\\" is one character in LISP ( \ )

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Storing variables
« Reply #19 on: March 31, 2011, 11:34:05 AM »
Yes, I am starting to look in to the function   (vl-string-subst)  trying to learn how to loop thru a string right now.

As a nudge in the right direction, this is what I use for string substitution in Visual LISP:

Code: [Select]
(defun LM:StringSubst ( new old string / l i ) (setq l (strlen new) i 0)
  (while (setq i (vl-string-search old string i))
    (setq string (vl-string-subst new old string i) i (+ i l))
  )
  string
)

For complex substitutions I use RegularExpressions (RegExp)

cadman6735

  • Bull Frog
  • Posts: 255
  • Life is all about the attitude.
Re: Storing variables
« Reply #20 on: March 31, 2011, 11:37:38 AM »
el-Crap-o

I have to first center the text for it to not remove the //pxqc;   :ugly:


Ok, well at least I learned the // is one character, which makes no sense to me but I guess it is what it is, I'll accept it and move on.

Sorry for wasting your time.
  Ability is what you're capable of doing.
  Motivation determines what you do.
  Attitude determines how well you do it.

    Lou Holtz
----------------------------------------------

  Revit Certified Professional

cadman6735

  • Bull Frog
  • Posts: 255
  • Life is all about the attitude.
Re: Storing variables
« Reply #21 on: March 31, 2011, 11:39:42 AM »
Code: [Select]
(defun LM:StringSubst ( new old string / l i ) (setq l (strlen new) i 0)
  (while (setq i (vl-string-search old string i))
    (setq string (vl-string-subst new old string i) i (+ i l))
  )
  string
)
Thanks
  Ability is what you're capable of doing.
  Motivation determines what you do.
  Attitude determines how well you do it.

    Lou Holtz
----------------------------------------------

  Revit Certified Professional

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Storing variables
« Reply #22 on: March 31, 2011, 11:42:23 AM »
Ok, well at least I learned the // is one character, which makes no sense to me but I guess it is what it is, I'll accept it and move on.

Note that it is the "\\" string which is interpreted as a single backslash. This is because the usual use of a backslash is within a escape character, such as "\n" for newline or "\t" for tab etc. Hence when we want to use a backslash on its own, we must double it up so that any following characters aren't interpreted as escape characters.

Sorry for wasting your time.

Not at all - I like questions I can answer  :evil:
« Last Edit: March 31, 2011, 11:54:12 AM by Lee Mac »

cadman6735

  • Bull Frog
  • Posts: 255
  • Life is all about the attitude.
Re: Storing variables
« Reply #23 on: April 01, 2011, 11:46:30 AM »
Lee

I am strugaling to understand, I think I am close but not sure.  I am not sure if I am even passing the right info to the right variables...

Code: [Select]
  (defun c:test ( / ent entString pattern string)

    (setq ent (car (entsel)))
    (setq entString (cdr (assoc 1 (entget ent))))

    (setq pattern "\\P")
    (setq string " ")

    (setq l (strlen entString) i 0)    [color=red]<-----  I am not sure how this works[/color]
   
    (while
      (setq i (vl-string-search pattern string i))
      (setq entString (vl-string-subst string pattern entString i) i (+ i l))  [color=red]<----- nor this[/color]
    )


(print entString)
   
(princ)
)
  Ability is what you're capable of doing.
  Motivation determines what you do.
  Attitude determines how well you do it.

    Lou Holtz
----------------------------------------------

  Revit Certified Professional

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Storing variables
« Reply #24 on: April 01, 2011, 04:14:13 PM »
Ah - you broke the sub! :-P

You could call it like this:

Code: [Select]
(defun c:test ( / ent str )
  (if
    (and
      (setq ent (car (entsel)))
      (setq str (cdr (assoc 1 (entget ent))))
    )
    (setq str (LM:StringSubst " " "\\P" str))
  )
)

(defun LM:StringSubst ( new old string / l i ) (setq l (strlen new) i 0)
  (while (setq i (vl-string-search old string i))
    (setq string (vl-string-subst new old string i) i (+ i l))
  )
  string
)

To understand how the sub works, read all the documentation on the vl-string-search and vl-string-subst functions and all the arguments those functions can take.



cadman6735

  • Bull Frog
  • Posts: 255
  • Life is all about the attitude.
Re: Storing variables
« Reply #25 on: April 04, 2011, 09:08:15 AM »
Lee
I have stared at your code all weekend (well for a few hours anyway)

Code: [Select]
(setq str (LM:StringSubst " " "\\P" str))    [color=red]<----  What are you doing here?[/color]
  Ability is what you're capable of doing.
  Motivation determines what you do.
  Attitude determines how well you do it.

    Lou Holtz
----------------------------------------------

  Revit Certified Professional

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Storing variables
« Reply #26 on: April 04, 2011, 10:05:38 AM »
Hi CADMan,

On the line to which you referred, I am passing the necessary arguments (or parameters) to the subfunction called 'LM:StringSubst'. My subfunction takes three arguments: 'new', 'old' and 'string' which represent respectively the new string to be substituted, the pattern string to be matched and replaced, and the string in which to do the replacing. When the subfunction is called, the symbols 'new', 'old' and 'string' will hold the argument data passed to the subfunction.

Think of the subfunction as just another LISP function. For example, I'm sure you are familiar with the LISP function '+'. This function takes one or more numerical arguments and proceeds to add them together and return the sum:

Code: [Select]
(+ arg1 arg2 ... argN)
Returns:   arg1 + arg2 + ... + argN

But, say we wish to have a function that will return the supplied argument plus 2, we could define such a function in the following way:

Code: [Select]
(defun Add2 ( x ) (+ x 2))
Now, the above function takes a single numerical argument, denoted by the symbol 'x', and returns that supplied number incremented by 2. Just as we called the '+' function, we would call our subfunction in the following way:

Code: [Select]
(Add2 5)
Returns: 7

Hence in my example, I call the function 'LM:StringSubst' with the required arguments:

Code: [Select]
(LM:StringSubst " " "\\P" str)
Returns: the value of the variable 'str' with "\\P" substituted for " "

Lee


cadman6735

  • Bull Frog
  • Posts: 255
  • Life is all about the attitude.
Re: Storing variables
« Reply #27 on: April 04, 2011, 11:27:18 AM »
Hi Lee

I have the idea of what you are doing, the examples you give me make sense but they are basic examples using defun
and
your code uses setq

I ;|greyed out the subfunction|; ran it and it is looking for a function, this is interesting.  I am not sure how this is happening and I don't know enough to ask a logical question to pinpoint the answer I am looking for.  If I could see some more documentation or examples of this concept this would be great.

Is there a specific topic in the help menu, is there a good "key word" I can look for or any websites that discuss this concept.

Thanks
  Ability is what you're capable of doing.
  Motivation determines what you do.
  Attitude determines how well you do it.

    Lou Holtz
----------------------------------------------

  Revit Certified Professional

GDF

  • Water Moccasin
  • Posts: 2059
Re: Storing variables
« Reply #28 on: April 04, 2011, 11:35:43 AM »
Hmmm... but that 'danger' would exist should a string be stored in the registry, text file, or even if the string was disguised and present in the LISP code itself...

On this topic, I posted some tips on how to spot malicious code in programs here, might be useful to some.

Thanks for the info. Well written.
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Storing variables
« Reply #29 on: April 04, 2011, 11:38:15 AM »
I have the idea of what you are doing, the examples you give me make sense but they are basic examples using defun and your code uses setq

My use of setq just sets the return of the subfunction to a symbol in the calling function, just as we might do this:

Code: [Select]
(setq x (+ 1 2 3))
Where 'x' now holds the return of the '+' function (i.e. 'x' has the value 6)

In a similar way I could do this:

Code: [Select]
(setq x (LM:StringSubst " " "\\P" str))
And 'x' would hold the string returned by my function.

I ;|greyed out the subfunction|; ran it and it is looking for a function, this is interesting.  I am not sure how this is happening and I don't know enough to ask a logical question to pinpoint the answer I am looking for.  If I could see some more documentation or examples of this concept this would be great.

Correct - the function needs to be defined before it can be called, hence we define the function using the defun expression.

Perhaps have a read of these topics:

Using Defun

Functions with Arguments
« Last Edit: April 04, 2011, 11:57:47 AM by Lee Mac »