#!/usr/bin/tclsh # # comparecols - compare columns of real numbers with tolerance. # # USAGE: comparecols [-v] file1 file2 1.0 2.0 3.0 ... # compares file1 with file2, assuming they are ASCII files containing # columns of real numbers. Successive arguments are the tolerances # for each column; additional columns in the files are ignored. # Lines beginning with # are ignored -- those at the top of the files # need not match, but elsewhere in the file the same lines must be # comments (text need not match). A tolerance < 0 means ignore this # column (need not be numeric, such as 'z=1000'). set verbose [lindex $argv 0] if {$verbose == "-v"} { set verbose 1 set argv [lreplace $argv 0 0] } else { set verbose 0 } set tol [lreplace $argv 0 1] set f1 [open [lindex $argv 0] r] set f2 [open [lindex $argv 1] r] set file1 [split [read $f1] "\n"] set file2 [split [read $f2] "\n"] close $f1 close $f2 # remove comments at the start of the file while {[regexp "^#" [lindex $file1 0]]} {set file1 [lreplace $file1 0 0]} while {[regexp "^#" [lindex $file2 0]]} {set file2 [lreplace $file2 0 0]} if {[llength $file1] != [llength $file2]} {exit 1} set n [llength $file1] set k [llength $tol] for {set i 0} {$i<$n} {incr i} { set l1 [string trim [lindex $file1 $i]] set l2 [string trim [lindex $file2 $i]] if {[regexp "^#" $l1] && [regexp "^#" $l2]} {continue} if {[string compare $l1 $l2]==0} {continue} for {set j 0} {$j<$k} {incr j} { set v1 [lindex $l1 $j] set v2 [lindex $l2 $j] set t [lindex $tol $j] if {$t<0.0} {continue} if {[expr "abs($v1-$v2)>$t"]} { if {$verbose} {puts "Error col=$j\nl1=$l1\nl2=$l2\n"} if {$verbose} {break} exit 1 } } } exit 0