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.

Removing duplicate or more elements from a list.

Old posts that have not been replied to for several years.
Locked
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Removing duplicate or more elements from a list.

Post by awyeah »

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.
==================================
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

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.
==================================
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

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
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

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.
==================================
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

amazingly simple as it may sound, yet valid and effective:

Code: Select all

proc remdum alist {
   return [lsort -unique $alist]
}
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

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