This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.
For more information, see this announcement post . Click the X in the top right-corner of this box to dismiss this message.
Old posts that have not been replied to for several years.
awyeah
Revered One
Posts: 1580 Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:
Post
by awyeah » Tue Jun 21, 2005 2:52 am
What is the best and easiest way to remove 2 or more than 2 duplicate elements from a list? LREPLACE? LSET? and how?
·awyeah·
==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
awyeah
Revered One
Posts: 1580 Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:
Post
by awyeah » Tue Jun 21, 2005 3:09 am
Something like this:
Code: Select all
set mynewlist [list]
foreach element $mylist {
if {[lsearch -exact [string tolower $mylist] [string tolower $element]] == -1} {
lappend mynewlist $element
}
}
Someone has something better maybe?
·awyeah·
==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
demond
Revered One
Posts: 3073 Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:
Post
by demond » Tue Jun 21, 2005 3:12 am
looping through the list, adding current element to another list after checking whether it's already there:
Code: Select all
proc remdup alist {
set blist {}
foreach elem $alist {
if {[lsearch -exact $blist $elem] == -1} {
lappend blist $elem
}
}
return blist
}
on a side note, [lset] has been introduced only recently (in Tcl 8.3 or 8.4 I believe), so it's not so good idea to use it in a general purpose script
awyeah
Revered One
Posts: 1580 Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:
Post
by awyeah » Tue Jun 21, 2005 3:32 am
Yeah I had that in mind actually, posted that earlier and above. Some one told me a way like:
Code: Select all
[lreplace $mylist [lsearch -exact $mylist $element] [lsearch -exact $mylist $element]]
Or it was something similar to something like this -- wanted to do it with lreplace.
·awyeah·
==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
demond
Revered One
Posts: 3073 Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:
Post
by demond » Tue Jun 21, 2005 3:56 am
amazingly simple as it may sound, yet valid and effective:
Code: Select all
proc remdum alist {
return [lsort -unique $alist]
}
awyeah
Revered One
Posts: 1580 Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:
Post
by awyeah » Tue Jun 21, 2005 4:07 am
Ah thanks, I didn't bother to lookup lsort. I know what it is used to arranging elements in some order and sorting them didn't know about the 'unique' switch. I'll give it a go, this is more simpler and easier.
·awyeah·
==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================