TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Dommy2Hotty on September 13, 2005, 06:04:33 PM

Title: Bad argument type: numberp: nil
Post by: Dommy2Hotty on September 13, 2005, 06:04:33 PM
I'm trying use this code to select text that has a number value, extract the text contents, and make calculations based on that.  
Code: [Select]
 (setq ent (entget (car (entsel "\nSELECT text: ")))
contents (cdr (assoc 1 ent))
8percent (rtos (* contents 0.08) 2 1)
);setq

But I keep getting [; error: bad argument type: numberp: nil]

I've done (type contents) and it comes back as STR, so RTOS should work, in my mind, anyway...

Does RTOS need to go in when I'm extracting assoc code 1 instead of during the calculation?
Title: Re: Bad argument type: numberp: nil
Post by: MP on September 13, 2005, 06:06:55 PM
Read up on distof, then consider this --

Code: [Select]
(defun MyDistOf ( string )
    ;;  returns numeric value or nil
    (cond
        ((/= 'str (type string)) nil)
        ((distof string 2))
        ((distof string 3))
        ((distof string 5))
        ((distof string 1))
    )
)
Title: Re: Bad argument type: numberp: nil
Post by: Dommy2Hotty on September 13, 2005, 06:11:35 PM
Read up on distof.

Fantastic!  Thank you very much! :mrgreen:

Finished code:
Code: [Select]
(setq ent (entget (car (entsel "\nSELECT text: ")))
      contents (cdr (assoc 1 ent))
      contents (distof contents 2)
      8percent (rtos (* contents 0.08) 2 1)
      );setq
Title: Re: Bad argument type: numberp: nil
Post by: MP on September 13, 2005, 06:14:32 PM
My pleasure.

 :-)
Title: Re: Bad argument type: numberp: nil
Post by: MP on September 13, 2005, 06:26:23 PM
Just for fun ...

Code: [Select]
(defun MyDistOf ( string / result )
    (if (eq 'str (type string))
        (vl-some
           '(lambda (mode)
                (setq result
                    (distof
                        string
                        mode
                    )
                )
            )
           '(2 3 5 4 1)
        )
    )   
    result
)
Title: Re: Bad argument type: numberp: nil
Post by: MP on September 14, 2005, 09:23:37 AM
Actually ... I shouldn't test that the argument passed is a string; iow, if passed anything but a string it should promptly crash. Thus:

Code: [Select]
(defun MyDistOf ( string / result )
    (vl-some
       '(lambda (mode)
            (setq result
                (distof
                    string
                    mode
                )
            )
        )
       '(2 3 5 4 1)
    )
    result
)

Or ...

Code: [Select]
(defun MyDistOf ( string )
    (cond
        ((distof string 2))
        ((distof string 3))
        ((distof string 5))
        ((distof string 4))
        ((distof string 1))
    )
)

Carry on.
Title: Re: Bad argument type: numberp: nil
Post by: Dommy2Hotty on September 14, 2005, 09:52:26 AM
 :? :-o :?