1515# - X1 to Xn: optional lists of numerical values
1616# --------------------------------------------------------------------
1717# ** Tcl **
18- # 1)
18+ #
1919# ::randFreq::getFreq values;
2020# It returns estimated frequencies from given data set.
2121#
2222# - $values: a list of numerical lists e.g., {{v11 v12 ... v1n} ... {vM1 ... vMm}}
2323#
24- # 2)
25- # ::randFreq::outputFreq values ?joinChar?;
24+ # ::randFreq::outputFreq values ?Char?;
2625# It outputs estimated frequencies as utf-8 encoded text in the current directory.
2726#
2827# - $values: a list of numerical lists e.g., {{v11 v12 ... v1n} ... {vM1 ... vMm}}
29- # - $joinChar: a join character; tab character is default value
28+ # - $Char: a join character; tab character is default value
29+ #
30+ # ::randFreq::loadFile filePath char ?encoding?;
31+ # It reads a given file and returns a list of numerical list
32+ #
33+ # - $filePath: filePath of a given file
34+ # - $char: a character used in order to split loaded data
35+ # - $encoding: an optional encoding of given file
3036# #===================================================================
3137set auto_noexec 1;
3238package require Tcl 8.6;
@@ -133,9 +139,9 @@ namespace eval ::randFreq {
133139 return $result ;
134140 };
135141 # procedure that outputs result as utf-8 encoded text in the current directory
136- proc outputFreq {values {joinChar \t }} {
142+ proc outputFreq {values {Char \t }} {
137143 # - $values: a list of numerical lists e.g., {{v11 v12 ... v1n} ... {vM1 ... vMm}}
138- # - $joinChar : a join character; tab character is default value
144+ # - $Char : a join character; tab character is default value
139145 set R [::randFreq::getFreq $values ];
140146 set C [open " [ clock seconds] dataFreq.txt" w];
141147 fconfigure $C -encoding utf-8;
@@ -145,6 +151,27 @@ namespace eval ::randFreq {
145151 puts stdout $R ;
146152 close $C ;unset C R;
147153 };
154+ # procedure that reads a given file and returns a list of numerical list
155+ proc loadFile {filePath char {encoding {}}} {
156+ # - $filePath: filePath of a given file
157+ # - $char: a character used in order to split loaded data
158+ # - $encoding: an optional encoding of given file
159+ set V {};
160+ # rgEx is regular expression that matches real number
161+ set rgEx {^(?:[+-]?[0-9]+(?:\. [0-9]+)?(?:[eE][+-]?[0-9]+(?:\. [0-9]+)?)?)$|^(?:\. [0-9]+)$};
162+ set C [open $filePath r];
163+ if {[llength $encoding ]<1} {fconfigure $C -encoding $encoding ;};
164+ set lines [split [read -nonewline $C ] \n ];
165+ close $C ;
166+ foreach l $lines {
167+ # it adds only real numbers from split string
168+ set x [lmap e [split $l $char ] {if {![regexp $rgEx $e ]} {continue ;};list $e ;}];
169+ if {[llength $x ]<1} {continue ;};
170+ lappend V $x ;
171+ };
172+ unset C lines rgEx x;
173+ return $V ;
174+ };
148175};
149176# === shell ===
150177if {$argc >0} {
0 commit comments