Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
274 Views, 5 Replies

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

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" " " " " "-" "-" "-" "-" " " " " " ")
5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: Anonymous

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" " " " " "-" "-" "-" "-" " " " " " ")
Message 3 of 6
stevor
in reply to: Anonymous

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
Message 4 of 6
DavidBethel
in reply to: Anonymous

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
Message 5 of 6
Anonymous
in reply to: Anonymous

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]

--
Message 6 of 6
Anonymous
in reply to: Anonymous


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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost