Toggle a variable

Toggle a variable

doni49
Mentor Mentor
800 Views
6 Replies
Message 1 of 7

Toggle a variable

doni49
Mentor
Mentor

Here's a little lsp routine that I started using many years ago when I needed to toggle a variable (one that is either 0 or 1).

 

(setq v (- 1 v))

If the variable starts out as 1, then subtract 1 from 1 and get 0.  If the variable starts out as 0, then subtract 0 from 1 and get 1.

 

I just thought someone might find it useful.  It works better than using an if statement.

 

Command: (setq v 1)
1
Command: (setq v (- 1 v))
0
Command: (setq v (- 1 v))
1
Command: (setq v (- 1 v))
0

 



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

801 Views
6 Replies
Replies (6)
Message 2 of 7

rkmcswain
Mentor
Mentor

Another way  Smiley Indifferent

 

_$ (setq x 0)
0
_$ (setq x (abs (1- x)))
1
_$ (setq x (abs (1- x)))
0
_$ (setq x (abs (1- x)))
1
_$ (setq x (abs (1- x)))
0
R.K. McSwain     | CADpanacea | on twitter
Message 3 of 7

ВeekeeCZ
Consultant
Consultant

@rkmcswain wrote:

Another way  Smiley Indifferent

 

...
_$ (setq x (abs (1- x)))
...

But you need twice as much functions than @doni49! 🙂

(setq does not count)

0 Likes
Message 4 of 7

Shneuph
Collaborator
Collaborator

Old Code:

 

(Defun C:TLC-Toggle_Perspective (/)
  (if (= (getvar "perspective") 0)
(setvar "perspective" 1)
(setvar "perspective" 0)
) (princ) );Defun

 

New Code:

(Defun C:TLC-Toggle_Perspective (/)
  (setvar "perspective" (- 1 (getvar "perspective")))
  (princ)
  );Defun

Thanks!

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 5 of 7

Kent1Cooper
Consultant
Consultant

@doni49 wrote:

Here's a little lsp routine that I started using many years ago when I needed to toggle a variable (one that is either 0 or 1).

 

(setq v (- 1 v))

 

....

I've used the same approach, too, for ages.

 

Here's an approach I've also used for such things when the options are not limited to only 0 and 1.  It will cycle around one step at a time through all possible integer values if they range from 0 upward.  [For example, the CenterType VLA Property of a Diameter or Radius Dimension (it's not anywhere in entity data, but there are entries about it in extended data) holds one of 3 values -- 0, 1 or 2 -- and determines whether it has crosshairs, crosshairs+lines, or neither.]

 

If the 'poss' value below represents the number of possible integer values [from 0 up to one less than the 'poss' number], then this will cycle through them:

(setq v (rem (1+ v) poss))

So, for example, with something that can have values from 0 through 4 [that is, 5 possible values] --

Set up the circumstances:

 

(setq poss 5); possible values 0 through 4
(setq v 3); [arbitrary starting value]

 

Then here we go:

 

Command: (setq v (rem (1+ v) poss))
4 ; [moves up 1 from what it was]

Command: (setq v (rem (1+ v) poss))
0 ; [cycles back to 0 after maximum value]

Command: (setq v (rem (1+ v) poss))
1 ; [then steps upward again from there]

Command: (setq v (rem (1+ v) poss))
2

Command: (setq v (rem (1+ v) poss))
3

 

It works with any integer value for 'poss'.

 

It could also be adjusted easily enough to suit different situations, like an other-than-0-upward range of values such as from -3 through 3.

Kent Cooper, AIA
Message 6 of 7

rkmcswain
Mentor
Mentor

And the original one from @doni49 is faster too.

 

(defun date2sec ()
  (setq s (getvar "DATE"))
  (setq seconds (* 86400.0 (- s (fix s))))
)
(setq x 0)

(defun C:1 (/ timer)
  (setq timer (date2sec))
  (repeat 1000000
    (setq x (abs (1- x)))
  )
  (princ (rtos (- (date2sec) timer) 2 10))
)


(defun C:2 (/ timer)
  (setq timer (date2sec))
  (repeat 1000000
    (setq x (- 1 x))
  )
  (princ (rtos (- (date2sec) timer) 2 10))
)
  
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

_$ (c:1)
0.3690183163
_$ (c:2)
0.2619981766
_$


Now if I ever need to run this function 1 million times, I'll have to figure out how to spend that extra 1/10 of a second of free time. 

Smiley Happy

 

 

 

R.K. McSwain     | CADpanacea | on twitter
Message 7 of 7

doni49
Mentor
Mentor
LOL


Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

0 Likes