DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Parse Data-File And Do Some Maths
A FAST way to proceed tabular data from a textfile. Also, do some statistics with tcl-math class. The data-file must look like this:
1 -62805
2 -62468
3 -62351
4 -62408
5 -62256
6 -62473
7 -62759
8 -62768
#!/usr/bin/tclsh
#
# fileio.tcl
#
# A FAST way to proceed tabular data from a textfile
# Also, do some statistics with tcl-math class. The
# data-file must look like this:
#
# 1 -62805
# 2 -62468
# 3 -62351
# 4 -62408
# 5 -62256
# 6 -62473
# 7 -62759
# 8 -62768
#
package require Tcl 8.4
package require textutil
package require math::statistics
namespace import ::textutil::*
namespace import ::math::statistics::*
set filename "data.dat"
puts "Reading file $filename"
if [catch {open $filename RDONLY} f] {
puts $f
} else {
while {1} {
gets $f line
if [eof $f] break
set vdata [ splitx $line "\t"]
lappend x [ lindex $vdata 0]
lappend y [ lindex $vdata 1]
}
close $f
}
puts "Lines read:"
puts [llength $x]
puts "Calculating statistics"
# Uncomment here to print the columns to screen.
# foreach does a good job, here.
#puts "\nx-column:"
#foreach dx $x { puts $dx }
#puts "\ny-column:"
#foreach dy $y { puts $dy }
set meany [mean $y]
set vary [var $y]
set stdevy [stdev $y]
puts "Mean y: $meany, Var x: $vary, StDev x: $stdevy"





