Author Topic: Storing variables  (Read 14888 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Storing variables
« Reply #30 on: April 04, 2011, 11:40:12 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.

Thanks Gary  :-)

cadman6735

  • Bull Frog
  • Posts: 255
  • Life is all about the attitude.
Re: Storing variables
« Reply #31 on: April 04, 2011, 01:59:16 PM »
I have not read the links you posted yet, so if my question is covered there I appologize

I guess my question is, what defines LM:StringSubst as a function?  '+' is a built in functions used by LISP but 'LM:StringSubst' is defined by you.  I think this is where I am getting lost.  Is it in the structure of how your wrote the list itself?


Again if this is covered in the links just tell me to "read on" and tonight I will do my homework.

Thanks
Lee
  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 #32 on: April 04, 2011, 05:37:22 PM »
Ok, I think I have it but still have some questions

I removed the (setq str) from
Code: [Select]
(setq str (LM:StringSubst " " "\\P" str))and the function still works.
Code: [Select]
(LM:StringSubst " " "\\P" str)So when it looks like this I understand it. the setq was throwing me.
from your explainationg of 'x' holding the value of 6, this got me to remove the the 'str' to make it a global variable so when I type at the command prompt !str I got the result of "line1 line2 line3" as you said, holds the value.  I am with it so far.

The only reason I can think of for creating the 'str' variable (setq str) is for future use of the string, which we do not use or need here??? (is that a correct statment?) so it can be removed?

The tricky part for me now is lisp runs in a line by line format.  so how does the (setq str) know what the string is when the sub-function runs after the (setq str) line?  How does it go back and set itself to "line1 line2 line3".
  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

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: Storing variables
« Reply #33 on: April 04, 2011, 07:56:40 PM »
The function LM:StringSubst returns a string value so the line
Code: [Select]
(setq str (LM:StringSubst " " "\\P" str))stores that string value in the variable str.

So put the setq back as you found it. :-)
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

chlh_jd

  • Guest
Re: Storing variables
« Reply #34 on: April 04, 2011, 11:51:20 PM »
Very exciting discussion , I learn a lot .
Since this , I always save my Global Variables by the 'TEXT' way , save in the '.ini' file . :-)


cadman6735

  • Bull Frog
  • Posts: 255
  • Life is all about the attitude.
Re: Storing variables
« Reply #35 on: April 05, 2011, 08:47:11 AM »
Hi CAB

Yes, I have left the setq in place, I can only assume that you gurus know what you are doing,  :-)
I am experimenting trying to understand how the code works.  I don't see the str later in the program so I don't understand why it is there and the function seems to work fine without it.  I am also trying not to be the annoying newb with too many questions, I am bent on learning this stuff.  Anyways, I will understand it soon enough, I just need to find my lightswitch.

This has given me a better understanding how to use arguments, (that does not include my wife.  She always wins, pees me off it does.)
  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 #36 on: April 05, 2011, 08:54:02 AM »
I guess my question is, what defines LM:StringSubst as a function?  '+' is a built in functions used by LISP but 'LM:StringSubst' is defined by you.  I think this is where I am getting lost.  Is it in the structure of how your wrote the list itself?

The 'defun' is what defines it as a function - those earlier links should help you understand this  :-)

The only reason I can think of for creating the 'str' variable (setq str) is for future use of the string, which we do not use or need here??? (is that a correct statment?) so it can be removed?

OK, I can see how that might be misleading - in my example code, if run on its own, you wouldn't necessarily need this extra variable assigment, since the last expression evaluated is the 'LM:StringSubst' function and so the return of this function is printed to the command line. However, I assumed this would not be the final product of what you were trying to achieve, hence I assigned the return to the variable 'str' for future use in the code.

The tricky part for me now is lisp runs in a line by line format.  so how does the (setq str) know what the string is when the sub-function runs after the (setq str) line?  How does it go back and set itself to "line1 line2 line3".

LISP runs 'line by line' within the function definition, so if we call another function, the LISP now runs line by line within said function and then returns to the point at which the function was called. To illustrate how the code is evaluated, use the 'animate' option as part of the VLIDE debugging tools - tutorial here.

To understand how the 'setq' statement works, you need to understand that a LISP function will return the value of the last expression evaluated.

Looking back at my subfunction:

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
)

Notice that I have the local variable 'string' on its own at the end of the code. This holds the value of the modified string and is the last expression in the function and hence the value of string is returned by the function, just as the sum of supplied numbers is returned by the '+' function.

The function LM:StringSubst returns a string value so the line
Code: [Select]
(setq str (LM:StringSubst " " "\\P" str))stores that string value in the variable str.

So put the setq back as you found it. :-)

Thanks Alan for the support  :-)
« Last Edit: April 05, 2011, 08:57:51 AM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Storing variables
« Reply #37 on: April 05, 2011, 08:55:33 AM »
I am also trying not to be the annoying newb with too many questions, I am bent on learning this stuff.  Anyways, I will understand it soon enough, I just need to find my lightswitch.

That doesn't even come into the equation, ask away - that is what this forum was created for  :-)

cadman6735

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

Quote
I assumed this would not be the final product of what you were trying to achieve, hence I assigned the return to the variable 'str' for future use in the code.
  Yes, this is the answer I am looking for.  Very good

Quote
The 'defun' is what defines it as a function - those earlier links should help you understand this
Yes, I understand this part, no worries.  I did not ask my question right, let me try again.

When I run this by itself
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))
  )
)

I get this at the command line
Quote
Command: test
Select object: *Cancel*
no function definition: LM:STRINGSUBST
It is looking for LM:STRINGSUBST, how does it know to look for this?  What makes it look for LM:STRINGSUBST?  Is it in the way the list is structured?  Because '+' is a AutoLisp functions that adds but LM:STRINGSUBST is nothing, I can change the spelling and it will look for different spelling too AC:NEWSPELLING
(defun c:test ( / ent str )
  (if
    (and
      (setq ent (car (entsel)))
      (setq str (cdr (assoc 1 (entget ent))))
    )
    (setq str (AC:NEWSPELLING " " "\\P" str))
  )
)
Quote
Command: TEST
Select object: *Cancel*
no function definition: AC:NEWSPELLING
  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 #39 on: April 05, 2011, 11:49:35 AM »
Quote
That doesn't even come into the equation, ask away - that is what this forum was created for

Sometimes I think it does, because it is hard to put into text what is in ones mind, specialy when one is limited in experiance and does not know how to express ones thought properly.  You spent time and effort in explaining to me something that seems simple and logical to you when I am looking for a different answer but not sure how to express my thoughts without just rewording my question.  I don't want to wear anyone out.

Thanks for the reassurance
  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

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9719
Re: Storing variables
« Reply #40 on: April 05, 2011, 11:55:28 AM »
cadman6735,
preform a search for my (Se7en) "refine your methods" document in the "teach" me forum. it should answer your last  questions.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Storing variables
« Reply #41 on: April 05, 2011, 01:14:02 PM »
It is looking for LM:STRINGSUBST, how does it know to look for this?  What makes it look for LM:STRINGSUBST?  Is it in the way the list is structured?

It is the position of the symbol 'LM:STRINGSUBST' in the code that informs the interpreter (the facility that turns the code into 'machine language' so to speak) to look for a function defined as 'LM:STRINGSUBST'. When a symbol is placed immediately after an opening bracket, it is interpreted as a function to be evaluated.

Because '+' is a AutoLisp functions that adds but LM:STRINGSUBST is nothing, I can change the spelling and it will look for different spelling too AC:NEWSPELLING

In the same way that we give other symbols meaning when we assign variables:

Code: [Select]
(setq x 1)
The symbol 'x' is arbitrary, and could very well be renamed to 'y', however, this is irrelevant since it is merely a symbol to help us structure the code. From the above expression we know that 'x' is a variable holding a value of 1, hence we might use in the following way:

Code: [Select]
(1+ x)
However, consider that the symbol 'x' could very well be a defined as a function instead. We could have defined it as follows:

Code: [Select]
(defun x ( a ) (+ a 3))
In which case the symbol 'x' is now a function that adds three to the supplied argument. Now, of course, if we attempt to use the symbol 'x' in our previous expression we receive an error:

Code: [Select]
_$ (defun x ( a ) (+ a 3))
X
_$ (1+ X)
; error: bad argument type: numberp: #<USUBR @13df96f4 X>

Because the function '1+' was expecting a numerical argument, not a function. Used as a function, however:

Code: [Select]
_$ (x 5)
8

Note that this is because the symbol 'x' points to a user defined subroutine (USUBR), and hence this subroutine is evaluated when 'x' is used as a function.

If we tried to use another symbol in place of 'x', say 'y', we receive the following error:

Code: [Select]
_$ (y 5)
; error: no function definition: Y

Since the symbol 'y' has no value at this point.



OK, so I realise that this post isn't as structured as it could be, and hopefully you can glean more from Se7en's document to help with your understanding; but the point I am attempting to make is that the symbols we use for variables and functions are merely aids for the construction of a program, they are arbitrary - it is the value they point to which is relevant:

Code: [Select]
_$ (setq a 1 b "CAD" c 1+ d (eval (defun x ( a ) (+ a 3))) e 1.2 f '(1 2 3))

_$ (type a)
INT
_$ (type b)
STR
_$ (type c)
SUBR
_$ (type d)
USUBR
_$ (type e)
REAL
_$ (type f)
LIST

I hope this helps with your understanding, if you still have questions, please do ask.

Lee

cadman6735

  • Bull Frog
  • Posts: 255
  • Life is all about the attitude.
Re: Storing variables
« Reply #42 on: April 05, 2011, 01:41:18 PM »
Quote
When a symbol is placed immediately after an opening bracket, it is interpreted as a function to be evaluated.
I remember reading this early in my first weeks of starting my LISP journy, but I assosiated to actual built in Lisp functions such as '+', 'setq' etc ...  This is the answer that I am looking for, this  clears it for me.  Now I just need to build upon my new found knowledge.

Thanks for being patient.


Se7en:
I found your post but the link is broke, I downloaded the PDF, is this the same thing?
  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 #43 on: April 05, 2011, 02:00:45 PM »
Quote
When a symbol is placed immediately after an opening bracket, it is interpreted as a function to be evaluated.
I remember reading this early in my first weeks of starting my LISP journy, but I assosiated to actual built in Lisp functions such as '+', 'setq' etc ...  This is the answer that I am looking for, this  clears it for me.  Now I just need to build upon my new found knowledge.

Thanks for being patient.

You're welcome CADMan, happy that I could clarify things for you with my incohesive waffling  :lol:

I'm sure Se7en's document will be the icing on the cake  :wink:

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9719
Re: Storing variables
« Reply #44 on: April 05, 2011, 02:05:57 PM »
...
Se7en:
I found your post but the link is broke, I downloaded the PDF, is this the same thing?

The PDF is what you want. Read and study it; that document was written to be used by both beginers and "more advanced" users (I touch on a lot of subjects in that document). That document (because of how i wrote it) took me almost two years to write. Its almost sick that i put that much work into something most people just read and dismiss after the first read.

Good luck, and i hope you enjoy it.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org