Author Topic: alot of if statements  (Read 2005 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
alot of if statements
« on: January 19, 2011, 09:56:40 AM »
I have alot of "if" statements
Code: [Select]
(if (= blockname "fmta12k6")
  (progn
     (load "a-xprt")
  )
that look like this
I know i can put them all in a cond statement but how do i get it to load the lisp files in the cond

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: alot of if statements
« Reply #1 on: January 19, 2011, 10:10:17 AM »
Code: [Select]
(cond
  (
    (eq blockname "fmta12k6")
   
    (load "a-xprt")
  )
  ( (eq blockname ...
  )
)

?

ronjonp

  • Needs a day job
  • Posts: 7529
Re: alot of if statements
« Reply #2 on: January 19, 2011, 10:18:40 AM »
If you have a bunch of these you could also lump them into a list and use ASSOC to load:


Code: [Select]
(setq lst '(("fmta12k6" . "a-xprt")
    ("fmta12k6_2" . "a-xprt_2")
    ("fmta12k6_3" . "a-xprt_3")
    ("fmta12k6_4" . "a-xprt_4")
   )
)
;;Assoc is case sensitive
(if (setq tmp (assoc blockname lst))
  (load (cdr tmp))
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
Re: alot of if statements
« Reply #3 on: January 20, 2011, 08:38:56 AM »
gah!

I had left the progn in my cond statements, thats why it didnt work for me.  :ugly:

thanks all for the help

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: alot of if statements
« Reply #4 on: January 20, 2011, 12:05:47 PM »
The progn is ok to use but you may have placed it incorrectly.
This should still work:
Code: [Select]
(cond
  (
    (eq blockname "fmta12k6")
    
    (progn (load "a-xprt"))
  )
  ( (eq blockname ...
  )
)
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.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: alot of if statements
« Reply #5 on: January 20, 2011, 01:40:18 PM »
Side note, but still relevant: one should fill the second argument to ensure that an error is not thrown.
eg.
Code: [Select]
Command: (load "bread.lsp")

Error: LOAD failed: "bread.lsp"
Command: (load "bread.lsp" nil)
nil

Command: (load "bread.lsp" "you want bread, three dollars! no soup for you!")
"you want bread, three dollars! no soup for you!"

I prefer the nil option since I can then use a logical evaluation (or, and, if, etc.) to see if the desired routine actually loaded, since if loaded, it will return the symbol of the last loaded routine in the loaded file.

eg.
Code: [Select]
Command: (load "toggle.lsp" nil)
C:SSM

eg. (evaluation)
Code: [Select]
Command: (and (load "chicken.lsp" nil)) <<Nothing loaded, nil returned
nil

Command: (and (load "toggle.lsp" nil)) <<Code loaded, T returned
T
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: alot of if statements
« Reply #6 on: January 20, 2011, 01:58:25 PM »
Good point Alan.  8-)
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.

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: alot of if statements
« Reply #7 on: January 20, 2011, 02:00:58 PM »
it will return the symbol of the last loaded routine in the loaded file.
what if the last symbol is nil? or the last expression evaluates to nil? :)
i use
Code: [Select]
(= (setq tmp (vk_RandNum)) (load "toggle" tmp))