Adding commas to number string

Discussion in 'AutoCAD' started by tonyp_survyr, Nov 20, 2004.

  1. tonyp_survyr

    tonyp_survyr Guest

    A while back I wrote a quick routine in LISP to write a block of mtext which would list the area of a houselot in square feet& acres and calculate a shape factor (a zoning requirement). Today I decided to "improve" my routine by adding commas into the square footage (typically between ten thousand and a million) to make it more presentable and easier to read.
    When I run it I get an error "..bad argument type: consp 2.."
    What is "consp" ? Is this something simple that I don't get ?
    This is the first routine I've written in 5 years, so I'm at a loss for where to start looking for the error.
     
    tonyp_survyr, Nov 20, 2004
    #1
  2. tonyp_survyr

    Paul Turvill Guest

    Post the code.

    What is the file you attached (shapef.sit)?
    ___
     
    Paul Turvill, Nov 20, 2004
    #2
  3. tonyp_survyr

    Paul Turvill Guest

    Oh ... a Stuffit file. You'd be better off using ZIP format, since windows
    will open that without any extra software.
    ___
     
    Paul Turvill, Nov 20, 2004
    #3
  4. tonyp_survyr

    tonyp_survyr Guest

    I tried a zip first and kept getting an "invalid file type error" .
     
    tonyp_survyr, Nov 20, 2004
    #4
  5. tonyp_survyr

    tonyp_survyr Guest

    I checked my PC and found that WinRAR was associated with ZIP files so I installed WinZip and tried making a new zip file. I still can't attach the file, zipped or not. (it's small- 1631 bytes, 741 bytes zipped)
    Error " The file type of your attachment is not allowed. Try zipping the file and reattach "
     
    tonyp_survyr, Nov 20, 2004
    #5
  6. tonyp_survyr

    Paul Turvill Guest

    If you're using anything newer than Windows98, you should be able to create
    a .zip file by simply right clicking on the file name, then Send to and
    select "comrpressed file." This way Windows creates its own ZIP file, and
    you don't need any add-ons. If your ZIP file is still refused, then you may
    have to contact your ISP.
    ___
     
    Paul Turvill, Nov 20, 2004
    #6
  7. tonyp_survyr

    John Uhden Guest

    Here's one way:
    (defun @rtoc (N Prec / Sign Str i j)
    (setq Prec (max 0 Prec)
    Sign (if (minusp N) "-" "")
    Str (rtos (abs N) 2 Prec)
    i (vl-string-search "." Str)
    j i
    )
    (if (zerop (setq i (rem i 3)))(setq i 3))
    (while (< i j)
    (setq Str (strcat (substr Str 1 i) "," (substr Str (1+ i)))
    i (+ i 4)
    j (1+ j)
    )
    )
    (strcat Sign Str)
    )



    would list the area of a houselot in square feet& acres and calculate a shape
    factor (a zoning requirement). Today I decided to "improve" my routine by adding
    commas into the square footage (typically between ten thousand and a million) to
    make it more presentable and easier to read.
    to start looking for the error.
     
    John Uhden, Nov 20, 2004
    #7
  8. tonyp_survyr

    tonyp_survyr Guest

    I didn't know windows did zip files by itself, I've just always used a third party program. Apparently the file is handled differently (maybe just by the OS) because when I remove the file association to the third party program it suddenly works. File attached.
     
    tonyp_survyr, Nov 20, 2004
    #8
  9. tonyp_survyr

    John Uhden Guest

    Oops. I jeft out an imortant part. Corrected:
    (defun @rtoc (N Prec / Sign Str i j)
    (setq Prec (max 0 Prec)
    Sign (if (minusp N) "-" "")
    Str (rtos (abs N) 2 Prec)
    i (vl-string-search "." Str)
    )
    (if (not i)(setq i (strlen Str))) ; <-- Here
    (setq j i)
    (if (zerop (setq i (rem i 3)))(setq i 3))
    (while (< i j)
    (setq Str (strcat (substr Str 1 i) "," (substr Str (1+ i)))
    i (+ i 4)
    j (1+ j)
    )
    )
    (strcat Sign Str)
    )
     
    John Uhden, Nov 20, 2004
    #9
  10. tonyp_survyr

    Paul Turvill Guest

    For starters, you've used the wrong function in all of your string
    manipulations: it's (substr ...) not (subst ...).
    ___
     
    Paul Turvill, Nov 20, 2004
    #10
  11. tonyp_survyr

    Paul Turvill Guest

    Oh, and you should strongly consider using a single (cond ...) structure in
    place of those multiple (if ... ) clauses.
     
    Paul Turvill, Nov 20, 2004
    #11
  12. tonyp_survyr

    tonyp_survyr Guest

    Thank you for your reply. I tried patching your code into my routine but kept getting a bad argument type error. I really couldn't follow how the code worked (clearly my shortcoming there) so I did't know how to find the problem. I think the main problen is I don't know what I'm doing.
     
    tonyp_survyr, Nov 21, 2004
    #12
  13. tonyp_survyr

    Paul Turvill Guest

    .... note especially the correct syntax for the (cond ...) structure.
    ___
     
    Paul Turvill, Nov 21, 2004
    #13
  14. tonyp_survyr

    John Uhden Guest

    My apologies for lack of direction. The function wants to accept two (2)
    arguments...
    N = the number as a 'REAL or 'INT
    Prec = the precision as an 'INT

    For example:
    Command: (@rtoc 123456789.1234 4)
    should return the string "123,456,789.1234"

    Or suppose you had a numeric variable 'N and wanted the precision to be 2; then
    use...
    (@rtoc N 2)

    HTH - John



    getting a bad argument type error. I really couldn't follow how the code worked
    (clearly my shortcoming there) so I did't know how to find the problem. I think
    the main problen is I don't know what I'm doing.
     
    John Uhden, Nov 21, 2004
    #14
  15. tonyp_survyr

    tonyp_survyr Guest

    That does just what I want. Thanks for the help.
     
    tonyp_survyr, Nov 21, 2004
    #15
  16. Command: (car 2)
    ; error: bad argument type: consp 2

    "consp" is the name of a function which checks whether something is a
    cons cell, i.e. a data structure created by the "cons" function.
    In other words, a list or a dotted pair.

    (Actually not a user-visible part of Visual Lisp, but the -p naming
    convention is typical in Lisp culture.)


    So, in some place in your program you are giving number 2 as a parameter
    to something that expects a list.

    --
     
    Martti Halminen, Nov 23, 2004
    #16
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.