Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Appending text string to each item in a list

1 view
Skip to first unread message

Tony Tanzillo

unread,
Mar 28, 2003, 2:32:00 PM3/28/03
to
Ouch!

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
>
>
>


Dean Davis

unread,
Mar 28, 2003, 2:22:08 PM3/28/03
to

Kevin Nehls

unread,
Mar 28, 2003, 2:28:42 PM3/28/03
to
Um, I'm not going to dig through all that just to find this out. Is your
list a list of strings? If so use mapcar or apply. If not, you need to
convert all items in your list to a list of strings before appending a
string to it.

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...

Dean Davis

unread,
Mar 28, 2003, 2:34:56 PM3/28/03
to
Stripped out some of the comments, maybe this one will be easier to read.
Thanks for any help

;;;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))
)
)


Jim Claypool

unread,
Mar 28, 2003, 3:31:03 PM3/28/03
to
The error is coming when the counter reaches the length of the list.
Since the items in the list start at 0 you are processing 1 too many times
Use (< nCounter nLstLength)
There are other problems once you get past this.
All you will get in the variable lstAppended is the length of the list of
drawings.

"Dean Davis" <cdd...@vcn.com> wrote in message

news:F1A4E5E187C54CCA...@in.WebX.maYIadrTaRb...

Dean Davis

unread,
Mar 28, 2003, 3:25:53 PM3/28/03
to
Thanks to everybody who helped. Guess I was making my routine much more
difficult than necessary. I have not used mapcar/lambda functions much in
my previous lisp programs, but it looks like it will do what I need in this
circumstance.
Thanks,
Dean

Dean Davis

unread,
Mar 28, 2003, 4:58:32 PM3/28/03
to
Jim,
Thanks for the tip about the counter (should have caught that one OOPS!!).
Because my sub-routine was not returning what I had hoped, I sent it to the
trash can, and started again using some of the suggestions from BillZ and
Tony Tanzillo (Thanks guys!!) and accomplished what I was needing with
probably 50% less code and no sub-routines. Wow, what a difference. Need
to study up on/use mapcar and lambda more often.

(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...

0 new messages