Please observe:
(setq oldlist '("One" "Two" "Three"))
(setq newlist
(mapcar
'(lambda (item)
(strcat item " Mississippi")
)
oldlist
)
)
(print newlist)
("One Mississippi" "Two Mississippi" "Three Mississippi")
"Dean Davis" <cdd...@vcn.com> wrote in message news:5ABED2BFAF32C726...@in.WebX.maYIadrTaRb...
> I am working on writing a routine which hopefully will append a particular
> string of text to each item in a list. I've already written the code,
> however I keep getting a bad argument: string p nil error when running the
> routine, and can't seem to determine the cause. Can anybody figure out how
> I have written this wrong? Any help would be greatly appreciated!!
>
> Thanks
>
> Here is the code and a short introduction:
>
> Intro:
> C:app2list will load active x support, create a list of drawings
> via (vl-directory-files), create a variable which will hold a string that
> will be appended as a prefix to each item in the list, then call a
> subfunction passing as arguments the list and the string and save the result
> as a variable to be used later.
> (app2listsubfun) will hopefully perform the actual work of
> appending the text string to the list. It will take the list argument and
> save it to a new variable name so the list argument will not be passed into
> the while loop multiple times, retrieve the length of the list to be used as
> a counter and index, set up a counter, start a while loop to run for the
> length of the list, retrieve the first item in the list, strcat the text
> argument to the item in the list, substitute the new item back into the
> list, advance the counter by one, finish running through the loop, and
> finally hopefully return the updated list back to the calling function.
>
> And finally here is the code....
>
> ;;;APPEND TO LIST.LSP
> ;;;
> ;;;C:app2list generates an initial list, a string that will be appended to
> each item,
> ;;;and then calls the app2listmain function with the previous two items as
> arguments.
> ;;;
> (defun c:app2list ( / lstDrawings sPrefixtext lstAppended) ;declare
> function & variables
> (vl-load-com)
> ;load active x
> (setq lstDrawings (vl-directory-files nil "*.dwg" 1)) ;construct
> list of dwgs in current folder
> (setq sPrefixtext "Test text")
> ;saves string to be appended to list
> (setq lstAppended (app2listsubfun lstDrawings sPrefixtext));call the main
> function & pass arg's
> )
> ;end of function
> ;;;
> ;;;app2listmain accepts two arguments: a list(lstInitarg), and a
> string(sPreapptxt) that
> ;;;will hopefully be appended to each item in the list and then return the
> list to the
> ;;;calling function
> ;;;
> (defun app2listsubfun (lstInitarg sApptxtarg /
> ;declare function & variables
> lstDrawingsmain nLstlength
> ;declare function & variables
> nCounter sOldlistitem
> ;declare function & variables
> sNewlistitem)
> ;declare function & variables
> (setq lstDrawingsmain lstInitarg)
> ;isolate/create new list from argument list
> (setq nLstlength (length lstDrawingsmain))
> ;gets the list length for while loop
> (setq nCounter 0)
> ;set the counter
> (while (<= nCounter nLstlength)
> ;start the while loop for the length of list
> (setq sOldlistitem (nth nCounter lstDrawingsmain))
> ;retrieves each item in list
> (setq sNewlistitem (strcat sApptxtarg sOldlistitem))
> ;concatenates string arg with list item
> (subst sNewlistitem sOldlistitem lstDrawingsmain) ;substitute
> newitem for olditem in list
> (setq nCounter (1+ nCounter))
> ;advance the counter by one
> )
> ;end of while loop
> )
> ;end of function
>
>
>
Or you could be going 1 step to far in your list. By the looks of you error
it looks like your routine is doing something like this:
(substr nil "fdsa")
Error: bad argument type: stringp nil
You can also use the debugger in VLIDE to animate your code and step through
it when it runs or to break on error to find out exactly where in your code
the problem is. More eyes isn't always the answer when you have tools that
can find the problems ;)
--
Kevin Nehls
"Dean Davis" <cdd...@vcn.com> wrote in message
news:5ABED2BFAF32C726...@in.WebX.maYIadrTaRb...
;;;APPEND TO LIST.LSP
;;;
;;;C:app2list generates an initial list, a string that will be appended to
each item,
;;;and then calls the app2listmain function with the previous two items as
arguments.
;;;
(defun c:app2list ( / lstDrawings sPrefixtext lstAppended)
(vl-load-com)
(setq lstDrawings (vl-directory-files nil "*.dwg" 1))
(setq sPrefixtext "Test text")
(setq lstAppended (app2listmain lstDrawings sPrefixtext))
)
;;;
;;;app2listmain accepts two arguments: a list(lstInitarg), and a
string(sPreapptxt) that
;;;will hopefully be appended to each item in the list and then return the
list to the
;;;calling function
;;;
(defun app2listmain (lstInitarg sApptxtarg /
lstDrawingsmain nLstlength
nCounter sOldlistitem
sNewlistitem)
(setq lstDrawingsmain lstInitarg)
(setq nLstlength (length lstDrawingsmain))
(setq nCounter 0)
(while (<= nCounter nLstlength)
(setq sOldlistitem (nth nCounter lstDrawingsmain))
(setq sNewlistitem (strcat sApptxtarg sOldlistitem))
(subst sNewlistitem sOldlistitem lstDrawingsmain)
(setq nCounter (1+ nCounter))
)
)
"Dean Davis" <cdd...@vcn.com> wrote in message
news:F1A4E5E187C54CCA...@in.WebX.maYIadrTaRb...
(defun c:app2list (/ lstDrawings sPrefixtext lstAppended)
(vl-load-com)
(setq lstDrawings (vl-directory-files nil "*.dwg" 1))
(setq sPrefixtext "Test text")
(setq lstAppended
(mapcar '(lambda (item) (strcat sPrefixtext item)) lstDrawings)
)
)
Thanks,
Dean
"Jim Claypool" <jcla...@kc.rr.com> wrote in message
news:67863C00E44E0C4D...@in.WebX.maYIadrTaRb...