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

Snippets has posted 5883 posts at DZone. View Full User Profile

Parse Data-File And Do Some Maths (Alternate)

01.17.2007
| 1648 views |
  • submit to reddit
        An alternate (but SLOW) way to proceed tabular data from a textfile
This one uses fileutil for proceeding the file.

#!/usr/bin/tclsh
#
# fileutils.tcl
# 
# An alternate (but SLOW) way to proceed tabular data from a textfile
# This one uses fileutil for proceeding the file
# 
#
#
package require Tcl 8.4
package require fileutil 
package require textutil
package require math::statistics

namespace import ::fileutil::*
namespace import ::textutil::*
namespace import ::math::statistics::*

set filename "data.dat"
puts "Reading file $filename"

foreachLine line $filename { 
	set vdata  [ splitx $line "\t"]
	lappend x [ lindex $vdata 0]
	lappend y [ lindex $vdata 1]
}

puts "Lines read:"
puts [llength $x] 
puts "Calculating statistics"
#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"