Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
*andrew
Message 1 of 6 (56 Views)

how to remove a number from list, modify it, and put it back

56 Views, 5 Replies
03-26-2009 11:54 AM
i have existing code that writes a list to file in a specific format.
is there an ease way to take a number out of this list, change that number
and put it back in the place i took it from?

anyone have any example code?

here is my example:
i want to take the "1 ,2.. etc " or however many numbers i have that are
single digit 0-9
and change them to double digits "01, 02.. etc"
the list looks like this

Command: (SETQ MAINLST (CONS " " MAINLST))
(" " " " " " " " "My Documents" " " " " " " " " " " "Drawing9" "-" " " " " "
"
"sdfg" " " " " " " " " "03-26-09" " " " " " " " " " " " " " " " " " " " " "
" "
" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "
" "
" " " " " " " "1" "X" "X" " " "X" "X" "X" "X" " " " " " " " " "-" "X" " " "
"
"-" "-" "-" "-" " " " " " " "2" "X" "X" " " "X" "X" "X" "X" " " " " " " " "
"-"
"X" " " " " "-" "-" "-" "-" " " " " " " "3" "X" "X" " " "X" "X" "X" "X" " "
" "
" " " " "-" "X" " " " " "-" "-" "-" "-" " " " " " " "4" "X" "X" " " "X" "X"
"X"
"X" " " " " " " " " "-" "X" " " " " "-" "-" "-" "-" " " " " " " "5" "X" "X"
" "
"X" "X" "X" "X" " " " " " " " " "-" "X" " " " " "-" "-" "-" "-" " " " " " ")
*Some Buddy
Message 2 of 6 (56 Views)

Re: how to remove a number from list, modify it, and put it back

03-26-2009 12:49 PM in reply to: *andrew
Hi, try this one:

;;;===================
(defun modlist (lst)
(mapcar
'(lambda (x)
(if
(and
(= (type (read x)) 'INT)
(= (strlen x) 1)
)
(strcat "0" x)
x
)
)
lst
)
)
;;;===================

Usage: (modlist mylist)

HTH

--
Humans are born with a wide horizon.
As time goes by, the horizon narrows and
narrows, until it becomes a point of view.

"andrew" a écrit dans le message de news:
6150101@discussion.autodesk.com...
i have existing code that writes a list to file in a specific format.
is there an ease way to take a number out of this list, change that number
and put it back in the place i took it from?

anyone have any example code?

here is my example:
i want to take the "1 ,2.. etc " or however many numbers i have that are
single digit 0-9
and change them to double digits "01, 02.. etc"
the list looks like this

Command: (SETQ MAINLST (CONS " " MAINLST))
(" " " " " " " " "My Documents" " " " " " " " " " " "Drawing9" "-" " " " " "
"
"sdfg" " " " " " " " " "03-26-09" " " " " " " " " " " " " " " " " " " " " "
" "
" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "
" "
" " " " " " " "1" "X" "X" " " "X" "X" "X" "X" " " " " " " " " "-" "X" " " "
"
"-" "-" "-" "-" " " " " " " "2" "X" "X" " " "X" "X" "X" "X" " " " " " " " "
"-"
"X" " " " " "-" "-" "-" "-" " " " " " " "3" "X" "X" " " "X" "X" "X" "X" " "
" "
" " " " "-" "X" " " " " "-" "-" "-" "-" " " " " " " "4" "X" "X" " " "X" "X"
"X"
"X" " " " " " " " " "-" "X" " " " " "-" "-" "-" "-" " " " " " " "5" "X" "X"
" "
"X" "X" "X" "X" " " " " " " " " "-" "X" " " " " "-" "-" "-" "-" " " " " " ")
Valued Mentor
stevor
Posts: 891
Registered: ‎12-26-2005
Message 3 of 6 (56 Views)

Re: how to remove a number from list, modify it, and put it back

03-27-2009 06:38 AM in reply to: *andrew
Accessing each element of the list, for testing or for revising, is often required for explicit changes.

If all instances of an element of a specific content are to substituted then the function subst can be used, eg:

(setq newlist (subst newElementvalue oldElementvalue oldList))

or for your example:

(SETQ MAINLST (subst "01" "1" MAINLST))
S
Contributor
DavidBethel
Posts: 25
Registered: ‎05-29-2003
Message 4 of 6 (56 Views)

Re: how to remove a number from list, modify it, and put it back

03-27-2009 08:18 AM in reply to: *andrew
Maybe something like this:

{code}(defun zl (l / i)
(setq i 0)
(repeat 10
(setq l (subst (strcat "0" (itoa i)) (itoa i) l)
i (1+ i)))
l)
{code} Edited by: DavidBethel on Mar 27, 2009 3:53 PM
*Martti Halminen
Message 5 of 6 (56 Views)

Re: how to remove a number from list, modify it, and put it back

03-30-2009 01:03 AM in reply to: *andrew
andrew wrote:
> i have existing code that writes a list to file in a specific format.
> is there an ease way to take a number out of this list, change that number
> and put it back in the place i took it from?

In full-scale Lisps, like Common Lisp, there are commands for modifying
the structure and contents of an existing list, for example SETF and RPLACD.

AutoLISP doesn't have those, so AutoLISP lists are essentially immutable,
and doing what you literally asked for is impossible.

What you can do is make a modified copy of your original list.
This is usually somewhat clumsier, and for huge lists rather inefficient,
but that's all we can do.

One way of doing this is MAPCAR, as shown in another message.

Another would be using a loop:

[code]

(defun maybe-modify-item (item)
;; put the one-item modification stuff here
;; return the item if no changes needed
)

(defun modify-list (lst / result)
(setq result nil)
(foreach item lst
(setq result (cons (maybe-modify-item item) result)))
(reverse result))


[/code]

--
*andrew
Message 6 of 6 (56 Views)

Re: how to remove a number from list, modify it, and put it back

04-17-2009 06:35 AM in reply to: *andrew

sorry for the very late reply

but this worked! i was surprised.

 

 

thanks everyone for their great tips


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Accessing
each element of the list, for testing or for revising, is often required for
explicit changes. If all instances of an element of a specific content are to
substituted then the function subst can be used, eg: (setq newlist (subst
newElementvalue oldElementvalue oldList)) or for your example: (SETQ MAINLST
(subst "01" "1" MAINLST))
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.