I then sort the lists, and now....
w do I check the set of equal length lists, which all contain just 2
bit hexadecimal strings, are all the same as one another.
I can code this up into nested loops, exiting when differences found,
but is there a better way?
set isEqual [expr {[lsort $list1] == [lsort $list2]}]
--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+
set isEqual [expr {[lsort $list1] eq [lsort $list2]}]
might be slightly faster - no need to interpret the list as a number
then.
Regards,
Arjen
Assuming I have 16 lists to check against one another, I can compare
sorted (-unique) L1 with L2 with L3.....with L16 to check are all the
same, or (N-1) compares for N lists; but checking unsorted are
different is not so easy, testing (L1 is diff to L2) and L2 is diff to
L3 does nothing for L1 vs L3. I need to loop through all others to
test L1 diffs, then all L2 (except L1) then L3 (except L1 & L2), which
makes N*(N-1)/2 tests altogether! Not too bad for 16 lists, but I may
expand to 256, so ~ 32000 comapres to do. (Gulp!) Is there a quicker
method anyone can see?
Regards, Niv
Well, sorting the master list is O(N*log(N)), which is better than the
dumb quadratic way as you noticed. So why insist on "unsorted" ?
-Alex
Ok, assume masterList contains the names of your list. Assuming that you
want to check for order equality within the list (i.e. {1 2} != {2 1}) then,
set allDiff 1
foreach listName $masterList {
set list [set $listName]
if {[info exists seen($list)]} {
set allDiff 0
break
}
set seen($list) 1
}
if {$allDiff} {
puts {All different}
} else {
puts {Opps! Not all unique!!!}
}
Note, you are still doing a bunch of compares, but allowing [info exists] to
do it for you.
--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+
As arrays in Tcl are implemented as efficient hash arrays, the order
of
comparisons is N. Check if an individual array element exists may be a
bit more
costly than checking the individual list element, as you compare the
whole
string representation at once, but the other method is O(N ln N) ...
Regards,
Arjen