Author Topic: Getting Error: bad argument type: stringp  (Read 1742 times)

0 Members and 1 Guest are viewing this topic.

Cawaugh

  • Guest
Getting Error: bad argument type: stringp
« on: December 18, 2012, 06:16:07 PM »
I am getting this error: Error: bad argument type: stringp ("US001" "US253")
When I try and build a script line shown below:
(write-line (strcat "(setq code-list " (cdr (cons "" codelist)) ")") Ofil)

The codelist is ("US001" "US253"). I haven't had problems with other lists but this seems goofy.
When I put these command into a script directly they work just fine but the write-line aspect of the program doesn't like the way I set it up.
Am I missing something here?
I've been at the program a while so i'm probably seeing things but i could use some assistance as this is driving me crazy.  :ugly:

Thanks for any help you may send my way.
charles.waugh@wmeng.com

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Getting Error: bad argument type: stringp
« Reply #1 on: December 18, 2012, 06:27:50 PM »
Just catching a bus so no time to comment other than this quick "consider this" (assuming variable code-list is set as you say):

Code: [Select]
(write-line
    (strcat
        "(setq code-list "
        "'"
        (vl-prin1-to-string code-list)
        ")"
    )
    Ofil
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Getting Error: bad argument type: stringp
« Reply #2 on: December 18, 2012, 07:22:27 PM »
Alternatively (coded blind but should work):

Code: [Select]
(write-line
    (strcat
        "(setq code-list "
        (vl-prin1-to-string (cons 'list code-list))
        ")"
    )
    Ofil
)

More robust code might idiot proof the code-list variable etc.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Getting Error: bad argument type: stringp
« Reply #3 on: December 19, 2012, 08:06:22 AM »
Here is a 'console demonstration' to explain the cause of your error:

Code - Auto/Visual Lisp: [Select]
  1. ;; Bind list to 'codelist' symbol
  2. _$ (setq codelist '("US001" "US253"))
  3. ("US001" "US253")
  4.  
  5. ;; Add (push) "" to front of list
  6. _$ (cons "" codelist)
  7. ("" "US001" "US253")
  8.  
  9. ;; Remove (pop) "" from front of list
  10. _$ (cdr (cons "" codelist))
  11. ("US001" "US253")
  12.  
  13. ;; ...Back to where we started
  14. _$ (equal codelist (cdr (cons "" codelist)))
  15. T
  16.  
  17. ;; The result is a list, strcat requires a string
  18. _$ (type (cdr (cons "" codelist)))

To print the list to file, I would also likely opt for the solution proposed above by MP, using the vl-prin1-to-string function to convert the list to a string whilst retaining the string delimiters (vl-prin1-to-string follows the behaviour of the prin1 function [however returning the string rather than printing it]; note that vl-princ-to-string would remove such delimiters):

Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-prin1-to-string '("US001" "US253"))
  2. "(\"US001\" \"US253\")"
  3.  
  4. _$ (vl-princ-to-string '("US001" "US253"))
  5. "(US001 US253)"

Another method is to use the prin1 function directly:

Code: [Select]
(princ "\n(setq codelist '" ofil)
(prin1 codelist ofil)
(princ ")" ofil)

In summary, when unsure as to why your code is failing, I would recommend that you step through each evaluated expression to ensure that the expression is returning the value that you expect it to return; as shown above, the Visual LISP IDE Console is ideal for this purpose.

Cawaugh

  • Guest
Re: Getting Error: bad argument type: stringp
« Reply #4 on: December 19, 2012, 01:28:47 PM »
 :lmao: So simple. It must have been the 12 hours of work I did just before I tackled this piece again.
Thank you! Thank you! Thank you!
You'll all stay on the nice list this Christmas.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Getting Error: bad argument type: stringp
« Reply #5 on: December 21, 2012, 05:48:14 PM »
You're very welcome!  :-)