How to insert a vaule in List

Guest
Can anybody tell me How to insert a value in a list..

Eg:

old_list=((0.39 1.2) (1.985 1.2) (1.985 1.4) (4.12 1.4) (4.12
1.11))

new_list=((0.39 1.2) (1.985 1.2) (0.4 1.4) (4.12 1.4) (4.12 1.11))

I have changed the xcoord value of 3rd (1.985 1.4) list.

How can I build a new list?.
 
Can anybody tell me How to insert a value in a list..

Eg:

old_list=((0.39 1.2) (1.985 1.2) (1.985 1.4) (4.12 1.4) (4.12
1.11))

new_list=((0.39 1.2) (1.985 1.2) (0.4 1.4) (4.12 1.4) (4.12 1.11))

I have changed the xcoord value of 3rd (1.985 1.4) list.

How can I build a new list?.

old_list='((0.39 1.2) (1.985 1.2) (1.985 1.4) (4.12 1.4) (4.12 1.11))
((0.39 1.2)
(1.985 1.2)
(1.985 1.4)
(4.12 1.4)
(4.12 1.11)
)

rplaca( nth(2 old_list) 0.4 )
(0.4 1.4)

old_list
((0.39 1.2)
(1.985 1.2)
(0.4 1.4)
(4.12 1.4)
(4.12 1.11)
)


simlarly, to change the y coordinate :

rplacd( nth(2 old_list) '(0.4) )
old_list
((0.39 1.2)
(1.985 1.2)
(0.4 0.4)
(4.12 1.4)
(4.12 1.11)
)

or to replace the whole points

rplaca( nthcdr(2 old_list) '(1.985 1.4) )
old_list
((0.39 1.2)
(1.985 1.2)
(1.985 1.4)
(4.12 1.4)
(4.12 1.11)
)



Cheers,

Stéphane
 
On Nov 26, 5:16 am, kumar_...@yahoo.co.in wrote:
Can anybody tell me How to insert a value in a list..

Eg:

old_list=((0.39 1.2) (1.985 1.2) (1.985 1.4) (4.12 1.4) (4.12
1.11))

new_list=((0.39 1.2) (1.985 1.2) (0.4 1.4) (4.12 1.4) (4.12 1.11))

I have changed the xcoord value of 3rd (1.985 1.4) list.

How can I build a new list?.
There's a couple of ways you can go about this, each with it's own
caveats.

The easiest way is to use the subst command.

1)

new_list = subst( '(1.985 1.4) '(0.4 1.4) old_list)

NOTE: subst is non-destructive, so it creates a new list and assigns
it to "new_list."
CAVEAT: the subst command will substitute ALL occurences of '(1.985
1.4) with '(0.4 1.4).

The second method (which I would most likely use) includes using
"copy" in conjunction with "rplaca."

2)

new_list = copy(old_list)
rplaca( member( '(1.985 1.4) new_list ) '(0.4 1.4))

NOTE: "copy returns a copy of a list. ... You should consider making
a copy of any list before using a destructive modification on the
list." -- SKILL Language User Guide
CAVEAT: "copy only duplicates the top-level list cells. All lower-
level objects are still shared." -- SUG

Both method 1) and 2) treat the duplicate elements within a list a
little differently. The first merely replaces all duplicate elements
in a list with the replacement element. This is probably not what you
want. The second replaces the FIRST matching element in a list with
the replacement element. This may not be what you want either. And
the reason for this is that the elements in a list are not guaranteed
to be unique.

GENERAL CAVEAT: When doing operations on a list (based on element
values), try to have some idea how likely it is each element will be
unique (not "equal") to any of the others and whether or not you're
okay with that. It's possible that a list may not be the data
structure you need for your project.

Edward
 

Welcome to EDABoard.com

Sponsor

Back
Top