Skip to content

Commit 9da2371

Browse files
authored
Merge pull request #3 from YujiSODE/Y20180112
Y20180112
2 parents ddedf67 + 9c71aec commit 9da2371

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ ______
1414
- `X1` to `Xn`: optional lists of numerical values
1515

1616
**Tcl**
17-
1\) `::randFreq::getFreq values;`
18-
It returns estimated frequencies from given data set.
19-
17+
`::randFreq::getFreq values;`
18+
It returns estimated frequencies from given data set.
2019
- `$values`: a list of numerical lists e.g., `{{v11 v12 ... v1n} ... {vM1 ... vMm}}`
2120

22-
2\) `::randFreq::outputFreq values ?joinChar?;`
23-
It outputs estimated frequencies as utf-8 encoded text in the current directory.
24-
21+
`::randFreq::outputFreq values ?joinChar?;`
22+
It outputs estimated frequencies as utf-8 encoded text in the current directory.
2523
- `$values`: a list of numerical lists e.g., `{{v11 v12 ... v1n} ... {vM1 ... vMm}}`
2624
- `$joinChar`: a join character; tab character is default value
2725

26+
`::randFreq::loadFile filePath char ?encoding?;`
27+
It reads a given file and returns a list of numerical list.
28+
- `$filePath`: filePath of a given file
29+
- `$char`: a character used in order to split loaded data
30+
- `$encoding`: an optional encoding of given file
31+
2832
## 2. Script
2933
It requires Tcl/Tk 8.6+.
3034
- `randFreq.tcl`

randFreq.tcl

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@
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
##===================================================================
3137
set auto_noexec 1;
3238
package 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 ===
150177
if {$argc>0} {

0 commit comments

Comments
 (0)