polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Files / FileReader.html
blob5bba56291fe9c14dec73ba6085cd0c1168354bc6
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.43">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060; min-height: 12.0px}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000}
22 span.s1 {color: #000000}
23 span.s2 {color: #0000bf}
24 span.s3 {color: #0000bf}
25 span.s4 {color: #606060}
26 span.s5 {color: #0000bf}
27 span.s6 {color: #bf0000}
28 span.Apple-tab-span {white-space:pre}
29 </style>
30 </head>
31 <body>
32 <p class="p1"><b>FileReader</b></p>
33 <p class="p2"><br></p>
34 <p class="p3"><span class="s1"><b>Inherits from: </b><a href="../Core/Object.html"><b>Object</b></a></span><b> </b><span class="s1"><b>: </b><a href="../Core/Kernel/AbstractFunction.html"><b>AbstractFunction</b></a></span><b> : </b><a href="../Streams-Patterns-Events/Stream.html"><b>Stream</b></a></p>
35 <p class="p2"><br></p>
36 <p class="p4"><b>FileReader reads space-delimited text files into 2D arrays line by line.</b></p>
37 <p class="p2"><br></p>
38 <p class="p4">For tab delimited files use: <a href="TabFileReader.html"><span class="s2">TabFileReader</span></a></p>
39 <p class="p4">For comma-separated files, use: <a href="CSVFileReader.html"><span class="s2">CSVFileReader</span></a>.</p>
40 <p class="p2"><br></p>
41 <p class="p2"><br></p>
42 <p class="p4"><b>*read(path, skipEmptyLines, skipBlanks, func, delimiter)</b></p>
43 <p class="p5"><br></p>
44 <p class="p5"><br></p>
45 <p class="p6">(</p>
46 <p class="p7">// write a test file:</p>
47 <p class="p8"><span class="s1">f = </span><span class="s3">File</span><span class="s1">(</span>"FileReaderTest.sc"<span class="s1">, </span>"w"<span class="s1">);</span></p>
48 <p class="p6">f.write(</p>
49 <p class="p8">"Some space delimited items in line 1</p>
50 <p class="p9"><br></p>
51 <p class="p8">and then some more with several blanks <span class="Apple-converted-space">    </span>in line 3</p>
52 <p class="p8">"</p>
53 <p class="p6">);</p>
54 <p class="p6">f.close;<span class="Apple-converted-space"> </span></p>
55 <p class="p6">)</p>
56 <p class="p5"><span class="Apple-tab-span"> </span></p>
57 <p class="p5"><span class="Apple-tab-span"> </span></p>
58 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span></span>// open file, read and put strings into array, close file.<span class="Apple-converted-space"> </span></p>
59 <p class="p6">x = <span class="s3">FileReader</span>.read(<span class="s4">"FileReaderTest.sc"</span>).postcs;</p>
60 <p class="p5"><br></p>
61 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span></span>// can skip empty lines:<span class="Apple-converted-space"> </span></p>
62 <p class="p6">x = <span class="s3">FileReader</span>.read(<span class="s4">"FileReaderTest.sc"</span>, <span class="s3">true</span>).postcs;</p>
63 <p class="p5"><br></p>
64 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span></span>// can skip blank entries caused by multiple spaces :<span class="Apple-converted-space"> </span></p>
65 <p class="p6">x = <span class="s3">FileReader</span>.read(<span class="s4">"FileReaderTest.sc"</span>, <span class="s3">true</span>, <span class="s3">true</span>).postcs;</p>
66 <p class="p5"><span class="Apple-tab-span"> </span></p>
67 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span></span>// do file open/close by hand if you prefer:</p>
68 <p class="p8"><span class="s1">f = </span><span class="s3">File</span><span class="s1">(</span>"FileReaderTest.sc"<span class="s1">, </span>"r"<span class="s1">); f.isOpen;</span></p>
69 <p class="p10"><span class="s1">t = </span>FileReader<span class="s1">(f, </span>true<span class="s1">, </span>true<span class="s1">);<span class="Apple-tab-span"> </span></span></p>
70 <p class="p6">t.read;</p>
71 <p class="p6">f.close;</p>
72 <p class="p5"><br></p>
73 <p class="p11"><span class="s1"><span class="Apple-tab-span"> </span></span>// take letter "a" as delimiter:<span class="Apple-converted-space"> </span></p>
74 <p class="p12">x = <span class="s5">FileReader</span>.read(<span class="s4">"FileReaderTest.sc"</span>, <span class="s5">true</span>, <span class="s5">true</span>, delimiter: $a).postcs;</p>
75 <p class="p5"><br></p>
76 <p class="p6">(</p>
77 <p class="p7">// write a test file with numbers:</p>
78 <p class="p8"><span class="s1">f = </span><span class="s3">File</span><span class="s1">(</span>"FileReadTestNum.sc"<span class="s1">, </span>"w"<span class="s1">);</span></p>
79 <p class="p5"><br></p>
80 <p class="p6">(1..10).do { <span class="s3">|n|</span> f.write(n.asString ++ <span class="s4">" "</span>); };</p>
81 <p class="p6">f.close;<span class="Apple-converted-space"> </span></p>
82 <p class="p6">)</p>
83 <p class="p5"><br></p>
84 <p class="p8"><span class="s1">x = </span><span class="s3">FileReader</span><span class="s1">.read(</span>"FileReadTestNum.sc"<span class="s1">).postcs;</span></p>
85 <p class="p6">x.collect(<span class="s3">_</span>.collect(<span class="s3">_</span>.interpret));<span class="Apple-tab-span"> </span><span class="s6">// convert to numbers.</span></p>
86 <p class="p5"><br></p>
87 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// or do it immediately:</p>
88 <p class="p6">x = <span class="s3">FileReader</span>.readInterpret(<span class="s4">"FileReadTestNum.sc"</span>).postcs;</p>
89 <p class="p5"><br></p>
90 <p class="p6">(</p>
91 <p class="p7">// write a test file with several lines of numbers:</p>
92 <p class="p8"><span class="s1">f = </span><span class="s3">File</span><span class="s1">(</span>"FileReadTestNum.sc"<span class="s1">, </span>"w"<span class="s1">);</span></p>
93 <p class="p5"><br></p>
94 <p class="p6">(1..100).do { <span class="s3">|n|</span><span class="Apple-converted-space"> </span></p>
95 <p class="p6"><span class="Apple-tab-span"> </span>f.write(n.asString ++ if (n % 10 != 0, <span class="s4">" "</span>, <span class="s3">Char</span>.nl)); };</p>
96 <p class="p6">f.close;<span class="Apple-converted-space"> </span></p>
97 <p class="p6">)</p>
98 <p class="p5"><br></p>
99 <p class="p5"><br></p>
100 <p class="p6">x = <span class="s3">FileReader</span>.readInterpret(<span class="s4">"FileReadTestNum.sc"</span>, <span class="s3">true</span>, <span class="s3">true</span>).postln;</p>
101 <p class="p5"><br></p>
102 </body>
103 </html>