Hi all,
I'm just learning Tcl, so this is probably basic. Essentially, I have two lists of data that can be any length (however both lists will always be the same length).
What I'm trying to do is create a 3rd and 4th list with the duplicates removed. I should mention that the two lists are associated with each other, so I cannot lose the position.
For example, say I have the following two lists:
set list [list 10 10 5 4 5]
set list2 [list 2 2 3 2 2]
What I want the code to do is create two lists (or an array?) that have removed the duplicates, so the output would look like:
set newlist [list 10 5 4 5]
set newlist2 [list 2 3 2 2]
Thus it has removed one of the duplicate data set 10,2.
The code I have written to do this is as follows:
This returns:
10 5 4
2 3 2
(thus missing the 5,2 data point). I think I've been going about this all the wrong way, so any help would be appreciated! (Also if you could explain the right code like I'm 5, that'd be great to help my learning of tcl!)
Thanks,
Matt
I'm just learning Tcl, so this is probably basic. Essentially, I have two lists of data that can be any length (however both lists will always be the same length).
What I'm trying to do is create a 3rd and 4th list with the duplicates removed. I should mention that the two lists are associated with each other, so I cannot lose the position.
For example, say I have the following two lists:
set list [list 10 10 5 4 5]
set list2 [list 2 2 3 2 2]
What I want the code to do is create two lists (or an array?) that have removed the duplicates, so the output would look like:
set newlist [list 10 5 4 5]
set newlist2 [list 2 3 2 2]
Thus it has removed one of the duplicate data set 10,2.
The code I have written to do this is as follows:
Code:
set list [list 10 10 5 4 5]
set list2 [list 2 2 3 2 2]
set newlist { }
set newlist2 { }
foreach i $list j $list2 {
if { $i ni $newlist && $j ni $newlist2 } {
lappend newlist $i
lappend newlist2 $j
} elseif { $i in $newlist && $j ni $newlist2 } {
lappend newlist $i
lappend newlist2 $j
} elseif { $i ni $newlist && $j in $newlist2 } {
lappend newlist $i
lappend newlist2 $j
}
}
puts $newlist
puts $newlist2
10 5 4
2 3 2
(thus missing the 5,2 data point). I think I've been going about this all the wrong way, so any help would be appreciated! (Also if you could explain the right code like I'm 5, that'd be great to help my learning of tcl!)
Thanks,
Matt