<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title><![CDATA[Curl Global Community - Try 5: Reading Our Score Data]]></title>
		<link>https://communities.curl.com/</link>
		<description><![CDATA[Curl Global Community - https://communities.curl.com]]></description>
		<pubDate>Fri, 01 May 2026 20:43:02 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Page 8: Extra Practice]]></title>
			<link>https://communities.curl.com/showthread.php?tid=78</link>
			<pubDate>Mon, 20 Jun 2011 04:34:06 +0000</pubDate>
			<guid isPermaLink="false">https://communities.curl.com/showthread.php?tid=78</guid>
			<description><![CDATA[<span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Extra Practice</span></span></span><br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Practice Problem 1: Basics</span></span></span><br />
<br />
Let’s write a program that writes the data displayed by <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordGrid</span></span> into a file. The program must satisfy the following requirements:<br />
<ul class="mycode_list"><li>The location to which the file is saved is to be specified by the user, using the Save as dialog box.<br />
</li>
<li>The data displayed using <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordGrid</span></span> will have three fields: <span style="font-family: Courier;" class="mycode_font">name</span>, <span style="font-family: Courier;" class="mycode_font">age</span>, and <span style="font-family: Courier;" class="mycode_font">score</span>.<br />
</li>
<li>The overwriting of data in an existing file shall be prohibited.<br />
</li>
</ul>
<img src="wiki/easy-ide-book/try5/images/figure_5_13.png" /><br />
<br />
Hint: Use commas to delimit each of the fields of the data in each <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">Record</span></span>, and then write each <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">Record</span></span> to one line of the file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Solution</span></span></span><br />
<br />
Solution Program: <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\04_exercise1\start.curl</span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
&nbsp;&nbsp;&nbsp;&nbsp;let out:#TextOutputStream<br />
&nbsp;&nbsp;&nbsp;&nbsp;let data:RecordSet = {RecordSet<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordFields<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordField name, domain = String},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordField age, domain = int},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordField score, domain = int}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; },<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordData name = Matt, age = 33, score = 88},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordData name = Sarah, age = 27, score = 79},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordData name = Jacob, age = 26, score = 90}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp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;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;{VBox<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordGrid<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;record-source = data,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width = 10cm,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height = 3cm<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cb<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Explanation</span></span></span><br />
<br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span>s, like arrays, are collections, so a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span> can be passed to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> loop, which will process the contained <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">Record</span></span>s in order.<br />
]]></description>
			<content:encoded><![CDATA[<span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Extra Practice</span></span></span><br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Practice Problem 1: Basics</span></span></span><br />
<br />
Let’s write a program that writes the data displayed by <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordGrid</span></span> into a file. The program must satisfy the following requirements:<br />
<ul class="mycode_list"><li>The location to which the file is saved is to be specified by the user, using the Save as dialog box.<br />
</li>
<li>The data displayed using <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordGrid</span></span> will have three fields: <span style="font-family: Courier;" class="mycode_font">name</span>, <span style="font-family: Courier;" class="mycode_font">age</span>, and <span style="font-family: Courier;" class="mycode_font">score</span>.<br />
</li>
<li>The overwriting of data in an existing file shall be prohibited.<br />
</li>
</ul>
<img src="wiki/easy-ide-book/try5/images/figure_5_13.png" /><br />
<br />
Hint: Use commas to delimit each of the fields of the data in each <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">Record</span></span>, and then write each <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">Record</span></span> to one line of the file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Solution</span></span></span><br />
<br />
Solution Program: <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\04_exercise1\start.curl</span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
&nbsp;&nbsp;&nbsp;&nbsp;let out:#TextOutputStream<br />
&nbsp;&nbsp;&nbsp;&nbsp;let data:RecordSet = {RecordSet<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordFields<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordField name, domain = String},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordField age, domain = int},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordField score, domain = int}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; },<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordData name = Matt, age = 33, score = 88},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordData name = Sarah, age = 27, score = 79},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordData name = Jacob, age = 26, score = 90}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;let cb:CommandButton = {CommandButton<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label = Save,<br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
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;{VBox<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordGrid<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;record-source = data,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width = 10cm,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height = 3cm<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cb<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Explanation</span></span></span><br />
<br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span>s, like arrays, are collections, so a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span> can be passed to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> loop, which will process the contained <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">Record</span></span>s in order.<br />
]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Page 7: Summary]]></title>
			<link>https://communities.curl.com/showthread.php?tid=77</link>
			<pubDate>Mon, 20 Jun 2011 04:23:25 +0000</pubDate>
			<guid isPermaLink="false">https://communities.curl.com/showthread.php?tid=77</guid>
			<description><![CDATA[<span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">TRY 5 Summary</span></span></span><br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Reading and writing data from/to a file</span></span></span><br />
<br />
<span style="font-weight: bold;" class="mycode_b">Step 1. Specifying the location of a file</span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let location_variable_name:Url = {url “path to file location”}</code></div></div><br />
<span style="font-weight: bold;" class="mycode_b">Step 2. Opening file as a Stream</span><br />
<br />
<ul class="mycode_list"><li><span style="font-family: Courier;" class="mycode_font">TextInputStream</span> (read)<br />
</li>
<li><span style="font-family: Courier;" class="mycode_font">TextOutputStream</span> (write)<br />
</li>
</ul>
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let stream_variable_name:TextInputStream = {read-open location_variable_name}<br />
let stream_variable_name:TextOutputStream = {write-open location_variable_name}</code></div></div><br />
<span style="font-weight: bold;" class="mycode_b">Step 3. Using methods to read or write data</span><br />
<ul class="mycode_list"><li>Methods such as <span style="font-family: Courier;" class="mycode_font">read-line</span> or <span style="font-family: Courier;" class="mycode_font">write-one-string</span><br />
</li>
</ul>
<br />
<span style="font-weight: bold;" class="mycode_b">Step 4. Closing the file</span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null stream_variable_name then<br />
&nbsp;&nbsp;&nbsp;&nbsp;{stream_variable_name.close}<br />
}</code></div></div><br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Repeat Expressions</span></span></span><br />
<br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">until</span></span> expression (processing repeats until conditional expression is <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">true</span></span>) <br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{until conditional_expression do<br />
&nbsp;&nbsp;&nbsp;&nbsp;body<br />
}</code></div></div><br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression (specifying a range) <br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for repeat variable:int = initial_value to final_value step step_size value do<br />
&nbsp;&nbsp;&nbsp;&nbsp;body<br />
}</code></div></div><br />
As well as to, there are also the below, downto, and above keywords. The <span style="font-family: Courier;" class="mycode_font">step_size</span> can be omitted when the desired <span style="font-family: Courier;" class="mycode_font">step_size</span> is 1.<br />
<br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression (repeats only the values among the elements of a collection, such as an array) <br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for element_variable:element_type key key_variable[:key type] in (collection) name do<br />
&nbsp;&nbsp;&nbsp;&nbsp;processing<br />
}</code></div></div><br />
Note that <span style="font-family: Courier;" class="mycode_font">element_variable:Element_type</span> or <span style="font-family: Courier;" class="mycode_font">key key_variable: key_type</span> can be omitted.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Exception Processing</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{try<br />
&nbsp;&nbsp;&nbsp;&nbsp;Processing in which an exception may occur.<br />
 catch exception_variable_name: exception<br />
&nbsp;&nbsp;&nbsp;&nbsp;Exception Processing<br />
 finally<br />
&nbsp;&nbsp;&nbsp;&nbsp;Processing that must be performed regardless of whether an exception occurs<br />
}</code></div></div><br />
The catch and finally clauses can be omitted. Also, each try can be declared more than once.]]></description>
			<content:encoded><![CDATA[<span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">TRY 5 Summary</span></span></span><br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Reading and writing data from/to a file</span></span></span><br />
<br />
<span style="font-weight: bold;" class="mycode_b">Step 1. Specifying the location of a file</span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let location_variable_name:Url = {url “path to file location”}</code></div></div><br />
<span style="font-weight: bold;" class="mycode_b">Step 2. Opening file as a Stream</span><br />
<br />
<ul class="mycode_list"><li><span style="font-family: Courier;" class="mycode_font">TextInputStream</span> (read)<br />
</li>
<li><span style="font-family: Courier;" class="mycode_font">TextOutputStream</span> (write)<br />
</li>
</ul>
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let stream_variable_name:TextInputStream = {read-open location_variable_name}<br />
let stream_variable_name:TextOutputStream = {write-open location_variable_name}</code></div></div><br />
<span style="font-weight: bold;" class="mycode_b">Step 3. Using methods to read or write data</span><br />
<ul class="mycode_list"><li>Methods such as <span style="font-family: Courier;" class="mycode_font">read-line</span> or <span style="font-family: Courier;" class="mycode_font">write-one-string</span><br />
</li>
</ul>
<br />
<span style="font-weight: bold;" class="mycode_b">Step 4. Closing the file</span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null stream_variable_name then<br />
&nbsp;&nbsp;&nbsp;&nbsp;{stream_variable_name.close}<br />
}</code></div></div><br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Repeat Expressions</span></span></span><br />
<br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">until</span></span> expression (processing repeats until conditional expression is <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">true</span></span>) <br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{until conditional_expression do<br />
&nbsp;&nbsp;&nbsp;&nbsp;body<br />
}</code></div></div><br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression (specifying a range) <br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for repeat variable:int = initial_value to final_value step step_size value do<br />
&nbsp;&nbsp;&nbsp;&nbsp;body<br />
}</code></div></div><br />
As well as to, there are also the below, downto, and above keywords. The <span style="font-family: Courier;" class="mycode_font">step_size</span> can be omitted when the desired <span style="font-family: Courier;" class="mycode_font">step_size</span> is 1.<br />
<br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression (repeats only the values among the elements of a collection, such as an array) <br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for element_variable:element_type key key_variable[:key type] in (collection) name do<br />
&nbsp;&nbsp;&nbsp;&nbsp;processing<br />
}</code></div></div><br />
Note that <span style="font-family: Courier;" class="mycode_font">element_variable:Element_type</span> or <span style="font-family: Courier;" class="mycode_font">key key_variable: key_type</span> can be omitted.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Exception Processing</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{try<br />
&nbsp;&nbsp;&nbsp;&nbsp;Processing in which an exception may occur.<br />
 catch exception_variable_name: exception<br />
&nbsp;&nbsp;&nbsp;&nbsp;Exception Processing<br />
 finally<br />
&nbsp;&nbsp;&nbsp;&nbsp;Processing that must be performed regardless of whether an exception occurs<br />
}</code></div></div><br />
The catch and finally clauses can be omitted. Also, each try can be declared more than once.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Page 6: Structure of a Program that Reads our Score Data]]></title>
			<link>https://communities.curl.com/showthread.php?tid=76</link>
			<pubDate>Mon, 20 Jun 2011 04:21:42 +0000</pubDate>
			<guid isPermaLink="false">https://communities.curl.com/showthread.php?tid=76</guid>
			<description><![CDATA[<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Structure of the program that reads our score data</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_13.png" /><br />
<br />
In this example, the data that is read from the file is displayed using a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordGrid</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">1. Specifying the location of the file</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let loc:Url = {url score.txt}</code></div></div><br />
Specify the location of the file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">2. Declaration of a TextInputStream</span></span></span>s<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let in:#TextInputStream</code></div></div><br />
To read data from the file, we have to declare <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">3. Declaration of character string array used to save read-in data</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let data-array:StringArray = {StringArray}</code></div></div><br />
Declare the character string array to be used for the data read from a file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">4. Declaration of RecordSet used to store data</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let record-set:RecordSet =<br />
&nbsp;&nbsp;&nbsp;&nbsp;{RecordSet<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordFields<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordField name, domain = String},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordField age, domain = int},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordField score, domain = int}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</code></div></div><br />
In this sample, the data obtained from a file is converted to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span>, and then later displayed using <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordGrid</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">5. Opening a file</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>set in = {read-open loc}</code></div></div><br />
Using <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">read-open</span></span>, we open the specified file as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">6. Reading the data</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{until in.end-of-stream? do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{if-non-null line = {in.read-line} then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{data-array.append {line.to-String}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
The data is read, line-by-line, and then added to the character string array.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">7. Closing the file</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null in then<br />
&nbsp;&nbsp;&nbsp;&nbsp;{in.close}<br />
}</code></div></div><br />
Regardless of whether the data read from the file is successful or not, the open file must be closed.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">8. Data conversion</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for data:String in data-array do<br />
&nbsp;&nbsp;&nbsp;&nbsp;let array:StringArray = {data.split split-chars = ,}<br />
&nbsp;&nbsp;&nbsp;&nbsp;{if array.size == 3 then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{record-set.append<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordData<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name = array[0], age = {array[1].to-int}, score = {array[2].to-int}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
We have to convert the data that is stored in a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">StringArray</span></span> to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span>. Each line of data in the file corresponds to one record. Also, each field is delimited by a comma (,). So, using the String.split method and a comma (,) as the delimiter, we partition the data and then save it into an array. Using the data in that array, we create <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordData</span></span>, and then add it to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">9. Displaying the layout</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{RecordGrid<br />
&nbsp;&nbsp;&nbsp;&nbsp;record-source = record-set,<br />
&nbsp;&nbsp;&nbsp;&nbsp;width = 10cm,<br />
&nbsp;&nbsp;&nbsp;&nbsp;height = 3cm<br />
}</code></div></div><br />
We take the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span> that we created in (8) and then display it using a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordGrid</span></span>.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/hint.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Working with CSV Data</span></span></span><br />
<br />
The Curl language also provides a predefined API for reading and writing CSV (comma separated values) formatted files. Using <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">CsvRecordSet</span></span>, you can load data directly as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span> for further manipulation and/or display. Please refer to the Curl documentation for more information<br />
<br />
<hr class="mycode_hr" />]]></description>
			<content:encoded><![CDATA[<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Structure of the program that reads our score data</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_13.png" /><br />
<br />
In this example, the data that is read from the file is displayed using a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordGrid</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">1. Specifying the location of the file</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let loc:Url = {url score.txt}</code></div></div><br />
Specify the location of the file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">2. Declaration of a TextInputStream</span></span></span>s<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let in:#TextInputStream</code></div></div><br />
To read data from the file, we have to declare <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">3. Declaration of character string array used to save read-in data</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let data-array:StringArray = {StringArray}</code></div></div><br />
Declare the character string array to be used for the data read from a file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">4. Declaration of RecordSet used to store data</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let record-set:RecordSet =<br />
&nbsp;&nbsp;&nbsp;&nbsp;{RecordSet<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordFields<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordField name, domain = String},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordField age, domain = int},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordField score, domain = int}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</code></div></div><br />
In this sample, the data obtained from a file is converted to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span>, and then later displayed using <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordGrid</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">5. Opening a file</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>set in = {read-open loc}</code></div></div><br />
Using <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">read-open</span></span>, we open the specified file as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">6. Reading the data</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{until in.end-of-stream? do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{if-non-null line = {in.read-line} then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{data-array.append {line.to-String}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
The data is read, line-by-line, and then added to the character string array.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">7. Closing the file</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null in then<br />
&nbsp;&nbsp;&nbsp;&nbsp;{in.close}<br />
}</code></div></div><br />
Regardless of whether the data read from the file is successful or not, the open file must be closed.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">8. Data conversion</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for data:String in data-array do<br />
&nbsp;&nbsp;&nbsp;&nbsp;let array:StringArray = {data.split split-chars = ,}<br />
&nbsp;&nbsp;&nbsp;&nbsp;{if array.size == 3 then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{record-set.append<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordData<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name = array[0], age = {array[1].to-int}, score = {array[2].to-int}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
We have to convert the data that is stored in a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">StringArray</span></span> to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span>. Each line of data in the file corresponds to one record. Also, each field is delimited by a comma (,). So, using the String.split method and a comma (,) as the delimiter, we partition the data and then save it into an array. Using the data in that array, we create <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordData</span></span>, and then add it to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">9. Displaying the layout</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{RecordGrid<br />
&nbsp;&nbsp;&nbsp;&nbsp;record-source = record-set,<br />
&nbsp;&nbsp;&nbsp;&nbsp;width = 10cm,<br />
&nbsp;&nbsp;&nbsp;&nbsp;height = 3cm<br />
}</code></div></div><br />
We take the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span> that we created in (8) and then display it using a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordGrid</span></span>.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/hint.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Working with CSV Data</span></span></span><br />
<br />
The Curl language also provides a predefined API for reading and writing CSV (comma separated values) formatted files. Using <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">CsvRecordSet</span></span>, you can load data directly as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">RecordSet</span></span> for further manipulation and/or display. Please refer to the Curl documentation for more information<br />
<br />
<hr class="mycode_hr" />]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Page 5: Application: Reading Our Score Data]]></title>
			<link>https://communities.curl.com/showthread.php?tid=75</link>
			<pubDate>Mon, 20 Jun 2011 04:20:03 +0000</pubDate>
			<guid isPermaLink="false">https://communities.curl.com/showthread.php?tid=75</guid>
			<description><![CDATA[<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Application: Reading Our Score Data</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
We’ll read the data containing our scores, and then display it.<br />
<br />
<img src="wiki/easy-ide-book/common/images/step1.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Create the 'Try 5-3' Project</span></span></span><br />
<br />
Close the Try 5-2 project and then, from the IDE 'File' menu, select 'New Project'. In the 'New Project' dialog box, select “Applet Project” (1), input “Try5-3” (2), specify <span style="font-family: Courier;" class="mycode_font">c:\Curl\lesson\Try5\03_score_file</span> in the “Directory” field (3), set the <span style="font-family: Courier;" class="mycode_font">API Version</span> to <span style="font-family: Courier;" class="mycode_font">6.0</span> (4), and then click OK (5).<br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_9.png" /><br />
<br />
<img src="wiki/easy-ide-book/common/images/step2.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Inputting the Program</span></span></span><br />
<br />
Copy the <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\03_score_file\score.txt</span> file to the <span style="font-family: Courier;" class="mycode_font">c:\Curl\lesson\Try5\03_score_file</span> directory. Next, we’ll input the program. You can copy the program code below or use <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\03_score_file\start.curl</span> and then paste it into the editor in the IDE.<br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_10.png" /><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
&nbsp;&nbsp;&nbsp;&nbsp;let loc:Url = {url score.txt}<br />
&nbsp;&nbsp;&nbsp;&nbsp;let in:#TextInputStream<br />
&nbsp;&nbsp;&nbsp;&nbsp;let data-array:StringArray = {StringArray}<br />
&nbsp;&nbsp;&nbsp;&nbsp;let record-set:RecordSet = {RecordSet<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordFields<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordField name, domain = String},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordField age, domain = int},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordField score, domain = int}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{try<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set in = {read-open loc}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{until in.end-of-stream? do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{if-non-null line = {in.read-line} then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{data-array.append {line.to-String}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp; catch e:IOException do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{popup-message Error reading file}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{output e.message}<br />
&nbsp;&nbsp;&nbsp;&nbsp; finally<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{if-non-null in then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{in.close}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{for data:String in data-array do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let array:StringArray = {data.split split-chars = ,}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{if array.size == 3 then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{record-set.append<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordData<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name = array[0], age = {array[1].to-int}, score = {array[2].to-int}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{RecordGrid<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;record-source = record-set,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width = 10cm,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height = 3cm<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
<img src="wiki/easy-ide-book/common/images/step3.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Save the File, and then Execute the Program</span></span></span><br />
<br />
After inputting the code, save the file, and then execute the program. The following result will appear in your browser.<br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_11.png" />]]></description>
			<content:encoded><![CDATA[<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Application: Reading Our Score Data</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
We’ll read the data containing our scores, and then display it.<br />
<br />
<img src="wiki/easy-ide-book/common/images/step1.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Create the 'Try 5-3' Project</span></span></span><br />
<br />
Close the Try 5-2 project and then, from the IDE 'File' menu, select 'New Project'. In the 'New Project' dialog box, select “Applet Project” (1), input “Try5-3” (2), specify <span style="font-family: Courier;" class="mycode_font">c:\Curl\lesson\Try5\03_score_file</span> in the “Directory” field (3), set the <span style="font-family: Courier;" class="mycode_font">API Version</span> to <span style="font-family: Courier;" class="mycode_font">6.0</span> (4), and then click OK (5).<br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_9.png" /><br />
<br />
<img src="wiki/easy-ide-book/common/images/step2.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Inputting the Program</span></span></span><br />
<br />
Copy the <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\03_score_file\score.txt</span> file to the <span style="font-family: Courier;" class="mycode_font">c:\Curl\lesson\Try5\03_score_file</span> directory. Next, we’ll input the program. You can copy the program code below or use <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\03_score_file\start.curl</span> and then paste it into the editor in the IDE.<br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_10.png" /><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
&nbsp;&nbsp;&nbsp;&nbsp;let loc:Url = {url score.txt}<br />
&nbsp;&nbsp;&nbsp;&nbsp;let in:#TextInputStream<br />
&nbsp;&nbsp;&nbsp;&nbsp;let data-array:StringArray = {StringArray}<br />
&nbsp;&nbsp;&nbsp;&nbsp;let record-set:RecordSet = {RecordSet<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {RecordFields<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{try<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set in = {read-open loc}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{until in.end-of-stream? do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{if-non-null line = {in.read-line} then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{data-array.append {line.to-String}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp; catch e:IOException do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{popup-message Error reading file}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{output e.message}<br />
&nbsp;&nbsp;&nbsp;&nbsp; finally<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{if-non-null in then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{in.close}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{for data:String in data-array do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let array:StringArray = {data.split split-chars = ,}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{if array.size == 3 then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{record-set.append<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{RecordData<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name = array[0], age = {array[1].to-int}, score = {array[2].to-int}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{RecordGrid<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;record-source = record-set,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width = 10cm,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height = 3cm<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
<img src="wiki/easy-ide-book/common/images/step3.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Save the File, and then Execute the Program</span></span></span><br />
<br />
After inputting the code, save the file, and then execute the program. The following result will appear in your browser.<br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_11.png" />]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Page 4: Structure of a Program that Writes Data to a File]]></title>
			<link>https://communities.curl.com/showthread.php?tid=74</link>
			<pubDate>Mon, 20 Jun 2011 04:18:54 +0000</pubDate>
			<guid isPermaLink="false">https://communities.curl.com/showthread.php?tid=74</guid>
			<description><![CDATA[<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Structure of the Program that Writes Data to a File</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_8.png" /><br />
<br />
The prcoess for writing data into a file is similar to reading data from a file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">1. Declaring a TextOutputStream</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let out:#TextOutputStream</code></div></div><br />
To write data into a file, we use <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextOutputStream</span></span>. In the same way as in Try 5-1, we declare this variable so it can have a null value.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">2. Creating VBox for display</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let v:VBox = {VBox spacing = 3.5pt, margin = 10pt, halign = right}</code></div></div><br />
We declare a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">VBox</span></span>, which is used for displaying the layout. We also specify spacing property which defines the spacing between objects, and the margin attribute that defines the amount of white space around the outside edges. The property halign is used to align all child objects to the right side of the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">VBox</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">3. Declaring the character string array used for the titles</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let title-array:StringArray =<br />
&nbsp;&nbsp;&nbsp;&nbsp;{StringArray<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name: , Company: , Address: ,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Phone: , Email: <br />
&nbsp;&nbsp;&nbsp;&nbsp;}</code></div></div><br />
We declare the above character strings as an array. These will eventually be displayed in the layout.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">4. Declaring the input field array</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let field-array:{Array-of TextField} = {{Array-of TextField}}</code></div></div><br />
We create the array for the input text fields. The initial value is an empty array, that is, one that does not contain any elements.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">5. Adding the input fields to the layout</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for i:int = 0 below 5 do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{field-array.append {TextField width = 5cm}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;{v.add <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{HBox <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;title-array+,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field-array+<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
This time, we use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression to iterate over a specified range. Because we’ve specified below, the processing is not performed when i = 5 and the loop ends at that point. For each iteration of the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression, we add a new <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextField</span></span> to the array. Finally, in the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">VBox</span></span> used for the layout, we add an element from the title-array and one from the field-array.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">6. Specifying a location for the file</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>if-non-null loc = {choose-file style = save-as} then</code></div></div><br />
In Try 5-1, we specified the URL of a file directly in the program text. In this example, however, we use a procedure called <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">choose-file</span></span>. <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">choose-file</span></span> pops up a Save as dialog box. If the user then specifies a file, that file's URL is returned as a return value.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">7. Opening a File</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>set out = {write-open <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error-if-exists? = true,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loc<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</code></div></div><br />
To write data into a file, we use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">write-open</span></span> procedure. With <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">write-open</span></span>, if the specified file already exists, by default the data in that file will be overwritten.<br />
<br />
We can also call <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">write-open</span></span> in a way that prohibits the overwriting of an existing file. If we specify error-if-exists ?= true and we attempt to write data to an existing file, an exception called <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">ExistingFileException</span></span> will be thrown.<br />
<br />
Note that if we want to append data to a file, we would use <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">append-open</span></span> instead of <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">write-open</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">8. Writing data</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for tx:TextField in field-array do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{out.write-one-string tx.value &amp; "&#92;r&#92;n"}<br />
}</code></div></div><br />
Using the write-one-string method, we can write a single character string. We can also use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression to obtain, in order, each <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextField</span></span> in an array, and then write the obtained values into a file. \r\n is used to indicate a carriage return and newline respectively.<br />
<br />
In the same way as for reading data, there are several different methods for writing data such as write, write-one, and write-one-string. Please consult the Curl documentation for a full listing.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">9. Handling an exception</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>catch e:ExistingFileException do<br />
&nbsp;&nbsp;{popup-message That file already exists.}<br />
&nbsp;&nbsp;{output e.message}<br />
catch e:IOException do<br />
&nbsp;&nbsp;{popup-message Error writing to file.}<br />
&nbsp;&nbsp;{output e.message}</code></div></div><br />
Here, we use multiple catch clauses to perform the exception processing.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">10. Closing the file</span></span></span>s<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null out then<br />
&nbsp;&nbsp;&nbsp;&nbsp;{out.close}<br />
}</code></div></div><br />
We have to close the file in exactly the same way as when we read data.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Privileges and choose-file</span></span></span><br />
<br />
Curl applets without the necessary privileges cannot freely access local resources. If, however, we use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">choose-file</span></span> procedure to specify the location of a file (like we did in Try 5-2), then even applets without special privileges can access user files. For an explanation of privileges, see <a href="showthread.php?tid=39">Try 0 Page 2</a> or the Curl documentation.<br />
<hr class="mycode_hr" />
]]></description>
			<content:encoded><![CDATA[<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Structure of the Program that Writes Data to a File</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_8.png" /><br />
<br />
The prcoess for writing data into a file is similar to reading data from a file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">1. Declaring a TextOutputStream</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let out:#TextOutputStream</code></div></div><br />
To write data into a file, we use <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextOutputStream</span></span>. In the same way as in Try 5-1, we declare this variable so it can have a null value.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">2. Creating VBox for display</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let v:VBox = {VBox spacing = 3.5pt, margin = 10pt, halign = right}</code></div></div><br />
We declare a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">VBox</span></span>, which is used for displaying the layout. We also specify spacing property which defines the spacing between objects, and the margin attribute that defines the amount of white space around the outside edges. The property halign is used to align all child objects to the right side of the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">VBox</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">3. Declaring the character string array used for the titles</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let title-array:StringArray =<br />
&nbsp;&nbsp;&nbsp;&nbsp;{StringArray<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name: , Company: , Address: ,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Phone: , Email: <br />
&nbsp;&nbsp;&nbsp;&nbsp;}</code></div></div><br />
We declare the above character strings as an array. These will eventually be displayed in the layout.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">4. Declaring the input field array</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let field-array:{Array-of TextField} = {{Array-of TextField}}</code></div></div><br />
We create the array for the input text fields. The initial value is an empty array, that is, one that does not contain any elements.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">5. Adding the input fields to the layout</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for i:int = 0 below 5 do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{field-array.append {TextField width = 5cm}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;{v.add <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{HBox <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;title-array+,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field-array+<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
This time, we use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression to iterate over a specified range. Because we’ve specified below, the processing is not performed when i = 5 and the loop ends at that point. For each iteration of the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression, we add a new <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextField</span></span> to the array. Finally, in the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">VBox</span></span> used for the layout, we add an element from the title-array and one from the field-array.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">6. Specifying a location for the file</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>if-non-null loc = {choose-file style = save-as} then</code></div></div><br />
In Try 5-1, we specified the URL of a file directly in the program text. In this example, however, we use a procedure called <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">choose-file</span></span>. <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">choose-file</span></span> pops up a Save as dialog box. If the user then specifies a file, that file's URL is returned as a return value.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">7. Opening a File</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>set out = {write-open <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error-if-exists? = true,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loc<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</code></div></div><br />
To write data into a file, we use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">write-open</span></span> procedure. With <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">write-open</span></span>, if the specified file already exists, by default the data in that file will be overwritten.<br />
<br />
We can also call <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">write-open</span></span> in a way that prohibits the overwriting of an existing file. If we specify error-if-exists ?= true and we attempt to write data to an existing file, an exception called <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">ExistingFileException</span></span> will be thrown.<br />
<br />
Note that if we want to append data to a file, we would use <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">append-open</span></span> instead of <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">write-open</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">8. Writing data</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for tx:TextField in field-array do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{out.write-one-string tx.value &amp; "&#92;r&#92;n"}<br />
}</code></div></div><br />
Using the write-one-string method, we can write a single character string. We can also use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression to obtain, in order, each <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextField</span></span> in an array, and then write the obtained values into a file. \r\n is used to indicate a carriage return and newline respectively.<br />
<br />
In the same way as for reading data, there are several different methods for writing data such as write, write-one, and write-one-string. Please consult the Curl documentation for a full listing.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">9. Handling an exception</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>catch e:ExistingFileException do<br />
&nbsp;&nbsp;{popup-message That file already exists.}<br />
&nbsp;&nbsp;{output e.message}<br />
catch e:IOException do<br />
&nbsp;&nbsp;{popup-message Error writing to file.}<br />
&nbsp;&nbsp;{output e.message}</code></div></div><br />
Here, we use multiple catch clauses to perform the exception processing.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">10. Closing the file</span></span></span>s<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null out then<br />
&nbsp;&nbsp;&nbsp;&nbsp;{out.close}<br />
}</code></div></div><br />
We have to close the file in exactly the same way as when we read data.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Privileges and choose-file</span></span></span><br />
<br />
Curl applets without the necessary privileges cannot freely access local resources. If, however, we use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">choose-file</span></span> procedure to specify the location of a file (like we did in Try 5-2), then even applets without special privileges can access user files. For an explanation of privileges, see <a href="showthread.php?tid=39">Try 0 Page 2</a> or the Curl documentation.<br />
<hr class="mycode_hr" />
]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Page 3: Basics 2: Writing a Control Value into a File]]></title>
			<link>https://communities.curl.com/showthread.php?tid=73</link>
			<pubDate>Mon, 20 Jun 2011 04:16:35 +0000</pubDate>
			<guid isPermaLink="false">https://communities.curl.com/showthread.php?tid=73</guid>
			<description><![CDATA[<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Basics 2: Writing a Control Value into a File</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
<img src="wiki/easy-ide-book/common/images/step1.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Create the 'Try 5-2' Project</span></span></span><br />
<br />
Close the Try 5-1 project and then, from the IDE 'File' menu, select 'New Project'. In the 'New Project' dialog box, select “Applet Project” (1), input “Try5-2” (2), specify <span style="font-family: Courier;" class="mycode_font">c:\Curl\lesson\Try5\02_write_file</span> in the “Directory” field (3), set the <span style="font-family: Courier;" class="mycode_font">API Version</span> to <span style="font-family: Courier;" class="mycode_font">6.0</span> (4), and then click OK (5).<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_6.png" /><br />
<br />
<img src="wiki/easy-ide-book/common/images/step2.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Inputting the Program</span></span></span><br />
<br />
Next, we’ll input the program. You can copy the program code below or use <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\02_write_file\start.curl</span> and then paste it into the editor in the IDE.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
&nbsp;&nbsp;&nbsp;&nbsp;let out:#TextOutputStream<br />
&nbsp;&nbsp;&nbsp;&nbsp;let v:VBox = {VBox spacing = 3.5pt, margin = 10pt, halign = right}&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;let title-array:StringArray = {StringArray<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name: , Company: , Address: ,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Phone: , Email: <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;let field-array:{Array-of TextField} = {{Array-of TextField}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{for i:int = 0 below 5 do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{field-array.append {TextField width = 5cm}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{v.add <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{HBox <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;title-array+,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field-array+<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{v.add<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{CommandButton<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;label = Save,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n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nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{for tx:TextField in field-array do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{out.write-one-string tx.value &amp; "&#92;r&#92;n}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch e:ExistingFileException do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{popup-message That file already exists.}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{output e.message}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch e:IOException do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{popup-message Error writing to file."}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{output e.message}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; finally<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{if-non-null out then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{out.close}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;v<br />
}</code></div></div><br />
<img src="wiki/easy-ide-book/common/images/step3.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Save the File, and then Execute the Program</span></span></span><br />
<br />
After inputting the code, save the file, and then execute the program. The execution results are displayed by the browser ((1)). Input values into a text fields, and then click the \[Save\] button ((2)). The “Save as” dialog box is displayed. This lets us create a new text file and then save the data to a convenient location. If we check the contents of the text file, then we should find that it contains the data that we entered.<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_7.png" />]]></description>
			<content:encoded><![CDATA[<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Basics 2: Writing a Control Value into a File</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
<img src="wiki/easy-ide-book/common/images/step1.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Create the 'Try 5-2' Project</span></span></span><br />
<br />
Close the Try 5-1 project and then, from the IDE 'File' menu, select 'New Project'. In the 'New Project' dialog box, select “Applet Project” (1), input “Try5-2” (2), specify <span style="font-family: Courier;" class="mycode_font">c:\Curl\lesson\Try5\02_write_file</span> in the “Directory” field (3), set the <span style="font-family: Courier;" class="mycode_font">API Version</span> to <span style="font-family: Courier;" class="mycode_font">6.0</span> (4), and then click OK (5).<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_6.png" /><br />
<br />
<img src="wiki/easy-ide-book/common/images/step2.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Inputting the Program</span></span></span><br />
<br />
Next, we’ll input the program. You can copy the program code below or use <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\02_write_file\start.curl</span> and then paste it into the editor in the IDE.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
&nbsp;&nbsp;&nbsp;&nbsp;let out:#TextOutputStream<br />
&nbsp;&nbsp;&nbsp;&nbsp;let v:VBox = {VBox spacing = 3.5pt, margin = 10pt, halign = right}&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;let title-array:StringArray = {StringArray<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name: , Company: , Address: ,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Phone: , Email: <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;let field-array:{Array-of TextField} = {{Array-of TextField}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{for i:int = 0 below 5 do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{field-array.append {TextField width = 5cm}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{v.add <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{HBox <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;title-array+,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field-array+<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{v.add<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{CommandButton<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;label = Save,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{on Action do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{if-non-null loc = {choose-file style = save-as} then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{try<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set out = {write-open <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch e:ExistingFileException do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{popup-message That file already exists.}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{output e.message}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch e:IOException do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{popup-message Error writing to file."}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{output e.message}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; finally<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{if-non-null out then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{out.close}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;v<br />
}</code></div></div><br />
<img src="wiki/easy-ide-book/common/images/step3.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Save the File, and then Execute the Program</span></span></span><br />
<br />
After inputting the code, save the file, and then execute the program. The execution results are displayed by the browser ((1)). Input values into a text fields, and then click the \[Save\] button ((2)). The “Save as” dialog box is displayed. This lets us create a new text file and then save the data to a convenient location. If we check the contents of the text file, then we should find that it contains the data that we entered.<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_7.png" />]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Page 2: Structure of a Program that Reads Data from a File]]></title>
			<link>https://communities.curl.com/showthread.php?tid=72</link>
			<pubDate>Mon, 20 Jun 2011 04:13:53 +0000</pubDate>
			<guid isPermaLink="false">https://communities.curl.com/showthread.php?tid=72</guid>
			<description><![CDATA[<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Structure of the Program that Reads Data from a File</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
First, let’s consider how we can read the data that is written into a file. There are actually four steps involved in reading such data. These are as follows:<ul class="mycode_list"><li>Step 1: Specifying the location of the file<br />
</li>
<li>Step 2: Opening the file<br />
</li>
<li>Step 3: Reading the data<br />
</li>
<li>Step 4: Closing the file<br />
</li>
</ul>
<br />
Let’s start by taking a look at the program’s source code.<br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_3.png" /><br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">1. Specifying the location of the file (Step 1)</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let loc:Url = {url data.txt}</code></div></div><br />
In the first step we need to specify the location of the file we will be reading. The location of the file is specified with a variable called a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">Url</span></span>. In the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">url</span></span> procedure, we set a character string value that specifies the path to the file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">2. Declaring TextInputStream</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let input: #TextInputStream</code></div></div><br />
To read a text file, we use <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span>. In this example, an initial value is not set for the variable declaration. In this case, we preceed <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span> with a # so that the default value (null) will be legal.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">3. Declaring the character string array that is used to store the read data</span></span></span>s<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let data-array:StringArray = {StringArray}</code></div></div><br />
Here, data is read from the file line-by-line and then saved into a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">StringArray</span></span>. <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">StringArray</span></span> is an abbreviation for {Array-of String}.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">4. Creating the variables that are used for display</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let list:DropdownList = {DropdownList width = 5cm}</code></div></div><br />
We declare the list variable as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">DropdownList</span></span> that is used to display the read-in data.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">5. Exception processing</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{try<br />
&nbsp;&nbsp;&nbsp;&nbsp;... (code here) ...<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
 catch e:IOException do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{popup-message Error reading file}<br />
&nbsp;&nbsp;&nbsp;&nbsp;{output e.message}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
 finally<br />
&nbsp;&nbsp;&nbsp;&nbsp;... (code here) ...<br />
}</code></div></div><br />
If the file cannot be opened or read, the Curl RTE detects that the operatio cannot be completed normally. This is known as an exception. If an exception occurs, this program performs exception processing to prevent an error with the applet.<br />
<br />
In our example, when the file at the specified URL is opened, an exception may occur when data is read from the file.<br />
<br />
To handle an exception, we use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">try</span></span> expression. That location at which an exception may occur is enclosed in the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">try</span></span> expression. Also, we define a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">catch</span></span> clause that handles exceptions that may occur.<br />
<br />
For every <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">try</span></span> expression, we can define multiple <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">catch</span></span> clauses. Each <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">catch</span></span> clause is used to define a specific exception to be handled. In our example, we specify an <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">IOException</span></span> because we are dealing with input/output operations. It is also possible to catch all exceptions by specifying <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">Exception</span></span> for the type of the exception processed by each <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">catch</span></span> phrase.<br />
<br />
At the end, we use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">finally</span></span> clause to describe processing that must be performed regardless of whether an exception has occurred.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Exception Processing</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{try<br />
&nbsp;&nbsp;&nbsp;&nbsp;body <br />
&nbsp;&nbsp;&nbsp;&nbsp;[catch-clauses] <br />
&nbsp;&nbsp;&nbsp;&nbsp;[finally-clause]<br />
}</code></div></div><br />
where<ul class="mycode_list"><li>body is one or more Curl language expressions (this is the code that throws the exception).<br />
</li>
<li>catch-clauses is zero or more optional catch clauses.<br />
</li>
<li>finally-clause is an optional finally clause.<br />
</li>
</ul>
<br />
<hr class="mycode_hr" />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">6. Opening a file (Step 2)</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>set input = {read-open loc}</code></div></div><br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span> is opened from the file URL; this is the second step. Using the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">read-open</span></span> procedure, we specify the URL as the argument and the file is opened as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">7. Opening a file (Step 3)</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{until input.end-of-stream? do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{if-non-null line = {input.read-line} then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{data-array.append {line.to-String}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
After the file is opened as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span>, the data is read using the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">until</span></span> expression. In this case, the code block is processed repeatedly until the specified boolean expression becomes true.<br />
<br />
There are three types looping expressions in Curl: until, while, and for. Let's look at the until and while expressions now (we'll cover the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression later). These two expressions have a body that is repeatedly executed until a specified expression becomes either true or false.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">until expression</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{until conditional_expression do<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
}</code></div></div><br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">until</span></span> processing is repeated until the condition is true. <br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">while expression</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{while conditional_expression do<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
}</code></div></div><br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">while</span></span> processing is repeated until the condition is false. <br />
<br />
<hr class="mycode_hr" />
<br />
Now, let’s take a look at our example.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{until input.end-of-stream? do<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
}</code></div></div><br />
In the example, the input.end-of-stream? expression indicates the condition. When the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span> reaches the end of the stream, the expression returns a value of true, so, the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">until</span></span> expression is repeatedly processed until it reaches the end of the stream.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/point.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Streams</span></span></span><br />
<br />
Streams are sequences of data that your applets can either read from or write to. They create a generic interface for exchanging data with resources.<br />
<br />
<hr class="mycode_hr" />
<br />
The next expression in our example is <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">if-non-null</span></span>. This expression is very similar to the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">if</span></span>, but the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">if-non-null</span></span> expression branches based on whether the value is of the expression is <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">null</span></span> or not (rather than true or false). Therefore, when the value of the expression is non-null, the the specified code block is executed.<br />
<br />
<hr class="mycode_hr" />
<br />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">if-non-null</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null [var-name[:type] = ] expr<br />
 then if-body<br />
&nbsp;&nbsp;&nbsp;&nbsp;[elseif cond then elseif-body] <br />
&nbsp;&nbsp;&nbsp;&nbsp;[else else-body]<br />
}</code></div></div><ul class="mycode_list"><li>When the expression is not null, processing is performed<br />
</li>
<li>The elseif and else phrases can be omitted.<br />
</li>
</ul>
<br />
<hr class="mycode_hr" />
<br />
Let’s take a look at the example.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null line = {input.read-line} then<br />
&nbsp;&nbsp;&nbsp;&nbsp;{data-array.append {line.to-String}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
The input.read-line method lets us read data from the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span> line by line. In our example, whenever the value is non-null, one line of data has been read. The text of each line read will be appended to an array. In addition to read-line, there are several methods for reading data.<br />
<br />
Finally, let’s take a look at the processing following the if-non-null expression. data-array is a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">String</span></span> array, as described above. Using the append method, we can add an element to an array. The following table describes other methods that can be used to manipulate array contents.<br />
<br />
<img src="attachment.php?aid=105" /><br />
<br />
The input.read-line method returns the data that has been read from a file. This value is returned as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">StringBuf</span></span>. Since elements of this array are of <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">String</span></span> type, we use the to-String method to convert from <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">StringBuf</span></span> to <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">String</span></span>. The value is then added to the array.<br />
<br />
In summary, the processing performed involves reading all the stream data line-by-line and provided that data is non-null, converting the data to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">String</span></span> and adding it to the array.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">8. Closing the file (Step 4)</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null input then<br />
&nbsp;&nbsp;&nbsp;&nbsp;{input.close}<br />
}</code></div></div><br />
The file is closed after the data is read from the file. A close must always be performed, regardless of whether an exception has occurred, so it is performed in the finally clause. In our example, we use if-non-null to check whether the file was ever opened. If the file is open, the close method is used to close the file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">9. Adding elements to the drop-down list</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for s:String in data-array do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{list.append s}<br />
}</code></div></div><br />
Since the data that is read from a file is stored in the array named data-array, we can access each element as input for our dropdown list. The data is taken, in order, and then added to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">DropdownList</span></span> control named list. The body of the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression is executed repeatedly, each time adding another element to the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">DropdownList</span></span>.<br />
<br />
Curl supports two variations of the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression. The first is used to specify a range and executes the body for each value in the range, while the second executes the body for each value in a collection of elements, such as an array.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/point.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">What is a Collection?</span></span></span><br />
<br />
A “collection” is a group of elements, all of which are of the same data type. Arrays and hash tables are both typical examples of collections. <br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">for expression, executed repeatedly within a given range</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for repeat variable:int = initial_value to final_value step step_size do<br />
&nbsp;&nbsp;&nbsp;&nbsp;body<br />
}</code></div></div><br />
<hr class="mycode_hr" />
<br />
If we omit step step_size, the variable value is increased (or decreased), by 1 each.<br />
<br />
Instead of to, we can also specify below, downto, or above.<br />
<ul class="mycode_list"><li>to … Variable value is increased in increments of the step_size. Processing is repeated while the variable value is less than or equal to the final value.<br />
</li>
<li>below … Variable value is increased in increments of the step_size. Processing is repeated while the variable value is less than the final value.<br />
</li>
<li>downto … Variable value is decreased in increments of the step_size. Processing is repeated while the variable value is greater than or equal to the final value.<br />
</li>
<li>above … Variable value is decreased in increments of the step_size. Processing is repeated while the variable value is greater than the final value.<br />
</li>
</ul>
<br />
Let’s execute the following two pieces of source code and see how the results differ. When we use to, the results should be 0 2 4 6 8. When we use below, it should return 0 2 4 6.<br />
<ul class="mycode_list"><li>When we use to<br />
</li>
</ul>
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
&nbsp;&nbsp;&nbsp;&nbsp;let message:HBox = {HBox}<br />
&nbsp;&nbsp;&nbsp;&nbsp;{for x:int = 0 to 8 step 2 do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{message.add x}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;message<br />
}</code></div></div><br />
<img src="wiki/easy-ide-book/try5/images/figure_5_4.png" /><br />
<ul class="mycode_list"><li>When we use below<br />
</li>
</ul>
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
&nbsp;&nbsp;&nbsp;&nbsp;let message:HBox = {HBox}<br />
&nbsp;&nbsp;&nbsp;&nbsp;{for x:int = 0 below 8 step 2 do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{message.add x}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;message<br />
}</code></div></div><br />
<img src="wiki/easy-ide-book/try5/images/figure_5_5.png" /><br />
<br />
The <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression that takes its values from the elements of a collection, will be as follows:<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">for expression</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for element _variable[:element_type] key key_variable[:key_type] in collection_name do<br />
&nbsp;&nbsp;&nbsp;&nbsp;processing<br />
}</code></div></div><br />
<hr class="mycode_hr" />
<br />
In our example, we use the for expression which repeats only the values among the elements of an array. The data is extracted from the element values in the array, and then added as items to be shown in the dropdown list.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">10. Adding an event handler</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{list.add-event-handler<br />
&nbsp;&nbsp;&nbsp;&nbsp;{on ValueChanged at lb:DropdownList do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{popup-message lb.value &amp; [color=red]was selected}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
Curl supports a method for adding an event handler, as mentioned in Try 2. We can use this method to specify that, when a value is selected from the dropdown list, it is displayed in a pop-up window.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">11. Specifying the layout</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{VBox<br />
&nbsp;&nbsp;&nbsp;&nbsp;[color=red]Select a US state[/color],<br />
&nbsp;&nbsp;&nbsp;&nbsp;list<br />
}</code></div></div><br />
We specify the screen display layout at the end of value. Character strings and the dropdown list are arranged vertically.<br /><!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://communities.curl.com/images/attachtypes/image.gif" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=105" target="_blank" title="">5-1.PNG</a> (Size: 12.23 KB / Downloads: 1256)
<!-- end: postbit_attachments_attachment -->]]></description>
			<content:encoded><![CDATA[<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Structure of the Program that Reads Data from a File</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
First, let’s consider how we can read the data that is written into a file. There are actually four steps involved in reading such data. These are as follows:<ul class="mycode_list"><li>Step 1: Specifying the location of the file<br />
</li>
<li>Step 2: Opening the file<br />
</li>
<li>Step 3: Reading the data<br />
</li>
<li>Step 4: Closing the file<br />
</li>
</ul>
<br />
Let’s start by taking a look at the program’s source code.<br />
<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_3.png" /><br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">1. Specifying the location of the file (Step 1)</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let loc:Url = {url data.txt}</code></div></div><br />
In the first step we need to specify the location of the file we will be reading. The location of the file is specified with a variable called a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">Url</span></span>. In the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">url</span></span> procedure, we set a character string value that specifies the path to the file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">2. Declaring TextInputStream</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let input: #TextInputStream</code></div></div><br />
To read a text file, we use <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span>. In this example, an initial value is not set for the variable declaration. In this case, we preceed <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span> with a # so that the default value (null) will be legal.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">3. Declaring the character string array that is used to store the read data</span></span></span>s<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let data-array:StringArray = {StringArray}</code></div></div><br />
Here, data is read from the file line-by-line and then saved into a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">StringArray</span></span>. <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">StringArray</span></span> is an abbreviation for {Array-of String}.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">4. Creating the variables that are used for display</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>let list:DropdownList = {DropdownList width = 5cm}</code></div></div><br />
We declare the list variable as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">DropdownList</span></span> that is used to display the read-in data.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">5. Exception processing</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{try<br />
&nbsp;&nbsp;&nbsp;&nbsp;... (code here) ...<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
 catch e:IOException do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{popup-message Error reading file}<br />
&nbsp;&nbsp;&nbsp;&nbsp;{output e.message}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
 finally<br />
&nbsp;&nbsp;&nbsp;&nbsp;... (code here) ...<br />
}</code></div></div><br />
If the file cannot be opened or read, the Curl RTE detects that the operatio cannot be completed normally. This is known as an exception. If an exception occurs, this program performs exception processing to prevent an error with the applet.<br />
<br />
In our example, when the file at the specified URL is opened, an exception may occur when data is read from the file.<br />
<br />
To handle an exception, we use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">try</span></span> expression. That location at which an exception may occur is enclosed in the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">try</span></span> expression. Also, we define a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">catch</span></span> clause that handles exceptions that may occur.<br />
<br />
For every <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">try</span></span> expression, we can define multiple <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">catch</span></span> clauses. Each <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">catch</span></span> clause is used to define a specific exception to be handled. In our example, we specify an <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">IOException</span></span> because we are dealing with input/output operations. It is also possible to catch all exceptions by specifying <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">Exception</span></span> for the type of the exception processed by each <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">catch</span></span> phrase.<br />
<br />
At the end, we use the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">finally</span></span> clause to describe processing that must be performed regardless of whether an exception has occurred.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Exception Processing</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{try<br />
&nbsp;&nbsp;&nbsp;&nbsp;body <br />
&nbsp;&nbsp;&nbsp;&nbsp;[catch-clauses] <br />
&nbsp;&nbsp;&nbsp;&nbsp;[finally-clause]<br />
}</code></div></div><br />
where<ul class="mycode_list"><li>body is one or more Curl language expressions (this is the code that throws the exception).<br />
</li>
<li>catch-clauses is zero or more optional catch clauses.<br />
</li>
<li>finally-clause is an optional finally clause.<br />
</li>
</ul>
<br />
<hr class="mycode_hr" />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">6. Opening a file (Step 2)</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>set input = {read-open loc}</code></div></div><br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span> is opened from the file URL; this is the second step. Using the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">read-open</span></span> procedure, we specify the URL as the argument and the file is opened as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span>.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">7. Opening a file (Step 3)</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{until input.end-of-stream? do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{if-non-null line = {input.read-line} then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{data-array.append {line.to-String}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
After the file is opened as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span>, the data is read using the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">until</span></span> expression. In this case, the code block is processed repeatedly until the specified boolean expression becomes true.<br />
<br />
There are three types looping expressions in Curl: until, while, and for. Let's look at the until and while expressions now (we'll cover the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression later). These two expressions have a body that is repeatedly executed until a specified expression becomes either true or false.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">until expression</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{until conditional_expression do<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
}</code></div></div><br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">until</span></span> processing is repeated until the condition is true. <br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">while expression</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{while conditional_expression do<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
}</code></div></div><br />
<span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">while</span></span> processing is repeated until the condition is false. <br />
<br />
<hr class="mycode_hr" />
<br />
Now, let’s take a look at our example.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{until input.end-of-stream? do<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
}</code></div></div><br />
In the example, the input.end-of-stream? expression indicates the condition. When the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span> reaches the end of the stream, the expression returns a value of true, so, the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">until</span></span> expression is repeatedly processed until it reaches the end of the stream.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/point.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Streams</span></span></span><br />
<br />
Streams are sequences of data that your applets can either read from or write to. They create a generic interface for exchanging data with resources.<br />
<br />
<hr class="mycode_hr" />
<br />
The next expression in our example is <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">if-non-null</span></span>. This expression is very similar to the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">if</span></span>, but the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">if-non-null</span></span> expression branches based on whether the value is of the expression is <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">null</span></span> or not (rather than true or false). Therefore, when the value of the expression is non-null, the the specified code block is executed.<br />
<br />
<hr class="mycode_hr" />
<br />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">if-non-null</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null [var-name[:type] = ] expr<br />
 then if-body<br />
&nbsp;&nbsp;&nbsp;&nbsp;[elseif cond then elseif-body] <br />
&nbsp;&nbsp;&nbsp;&nbsp;[else else-body]<br />
}</code></div></div><ul class="mycode_list"><li>When the expression is not null, processing is performed<br />
</li>
<li>The elseif and else phrases can be omitted.<br />
</li>
</ul>
<br />
<hr class="mycode_hr" />
<br />
Let’s take a look at the example.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null line = {input.read-line} then<br />
&nbsp;&nbsp;&nbsp;&nbsp;{data-array.append {line.to-String}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
The input.read-line method lets us read data from the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">TextInputStream</span></span> line by line. In our example, whenever the value is non-null, one line of data has been read. The text of each line read will be appended to an array. In addition to read-line, there are several methods for reading data.<br />
<br />
Finally, let’s take a look at the processing following the if-non-null expression. data-array is a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">String</span></span> array, as described above. Using the append method, we can add an element to an array. The following table describes other methods that can be used to manipulate array contents.<br />
<br />
<img src="attachment.php?aid=105" /><br />
<br />
The input.read-line method returns the data that has been read from a file. This value is returned as a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">StringBuf</span></span>. Since elements of this array are of <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">String</span></span> type, we use the to-String method to convert from <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">StringBuf</span></span> to <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">String</span></span>. The value is then added to the array.<br />
<br />
In summary, the processing performed involves reading all the stream data line-by-line and provided that data is non-null, converting the data to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">String</span></span> and adding it to the array.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">8. Closing the file (Step 4)</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{if-non-null input then<br />
&nbsp;&nbsp;&nbsp;&nbsp;{input.close}<br />
}</code></div></div><br />
The file is closed after the data is read from the file. A close must always be performed, regardless of whether an exception has occurred, so it is performed in the finally clause. In our example, we use if-non-null to check whether the file was ever opened. If the file is open, the close method is used to close the file.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">9. Adding elements to the drop-down list</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for s:String in data-array do<br />
&nbsp;&nbsp;&nbsp;&nbsp;{list.append s}<br />
}</code></div></div><br />
Since the data that is read from a file is stored in the array named data-array, we can access each element as input for our dropdown list. The data is taken, in order, and then added to a <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">DropdownList</span></span> control named list. The body of the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression is executed repeatedly, each time adding another element to the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">DropdownList</span></span>.<br />
<br />
Curl supports two variations of the <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression. The first is used to specify a range and executes the body for each value in the range, while the second executes the body for each value in a collection of elements, such as an array.<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/point.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">What is a Collection?</span></span></span><br />
<br />
A “collection” is a group of elements, all of which are of the same data type. Arrays and hash tables are both typical examples of collections. <br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">for expression, executed repeatedly within a given range</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for repeat variable:int = initial_value to final_value step step_size do<br />
&nbsp;&nbsp;&nbsp;&nbsp;body<br />
}</code></div></div><br />
<hr class="mycode_hr" />
<br />
If we omit step step_size, the variable value is increased (or decreased), by 1 each.<br />
<br />
Instead of to, we can also specify below, downto, or above.<br />
<ul class="mycode_list"><li>to … Variable value is increased in increments of the step_size. Processing is repeated while the variable value is less than or equal to the final value.<br />
</li>
<li>below … Variable value is increased in increments of the step_size. Processing is repeated while the variable value is less than the final value.<br />
</li>
<li>downto … Variable value is decreased in increments of the step_size. Processing is repeated while the variable value is greater than or equal to the final value.<br />
</li>
<li>above … Variable value is decreased in increments of the step_size. Processing is repeated while the variable value is greater than the final value.<br />
</li>
</ul>
<br />
Let’s execute the following two pieces of source code and see how the results differ. When we use to, the results should be 0 2 4 6 8. When we use below, it should return 0 2 4 6.<br />
<ul class="mycode_list"><li>When we use to<br />
</li>
</ul>
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
&nbsp;&nbsp;&nbsp;&nbsp;let message:HBox = {HBox}<br />
&nbsp;&nbsp;&nbsp;&nbsp;{for x:int = 0 to 8 step 2 do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{message.add x}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;message<br />
}</code></div></div><br />
<img src="wiki/easy-ide-book/try5/images/figure_5_4.png" /><br />
<ul class="mycode_list"><li>When we use below<br />
</li>
</ul>
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
&nbsp;&nbsp;&nbsp;&nbsp;let message:HBox = {HBox}<br />
&nbsp;&nbsp;&nbsp;&nbsp;{for x:int = 0 below 8 step 2 do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{message.add x}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;message<br />
}</code></div></div><br />
<img src="wiki/easy-ide-book/try5/images/figure_5_5.png" /><br />
<br />
The <span style="color: #9400D3;" class="mycode_color"><span style="font-family: Courier;" class="mycode_font">for</span></span> expression that takes its values from the elements of a collection, will be as follows:<br />
<br />
<hr class="mycode_hr" />
<br />
<img src="wiki/easy-ide-book/common/images/syntax.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">for expression</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{for element _variable[:element_type] key key_variable[:key_type] in collection_name do<br />
&nbsp;&nbsp;&nbsp;&nbsp;processing<br />
}</code></div></div><br />
<hr class="mycode_hr" />
<br />
In our example, we use the for expression which repeats only the values among the elements of an array. The data is extracted from the element values in the array, and then added as items to be shown in the dropdown list.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">10. Adding an event handler</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{list.add-event-handler<br />
&nbsp;&nbsp;&nbsp;&nbsp;{on ValueChanged at lb:DropdownList do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{popup-message lb.value &amp; [color=red]was selected}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div><br />
Curl supports a method for adding an event handler, as mentioned in Try 2. We can use this method to specify that, when a value is selected from the dropdown list, it is displayed in a pop-up window.<br />
<br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">11. Specifying the layout</span></span></span><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{VBox<br />
&nbsp;&nbsp;&nbsp;&nbsp;[color=red]Select a US state[/color],<br />
&nbsp;&nbsp;&nbsp;&nbsp;list<br />
}</code></div></div><br />
We specify the screen display layout at the end of value. Character strings and the dropdown list are arranged vertically.<br /><!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://communities.curl.com/images/attachtypes/image.gif" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=105" target="_blank" title="">5-1.PNG</a> (Size: 12.23 KB / Downloads: 1256)
<!-- end: postbit_attachments_attachment -->]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Page 1: Reading Our Score Data]]></title>
			<link>https://communities.curl.com/showthread.php?tid=71</link>
			<pubDate>Mon, 20 Jun 2011 04:12:52 +0000</pubDate>
			<guid isPermaLink="false">https://communities.curl.com/showthread.php?tid=71</guid>
			<description><![CDATA[<img src="wiki/easy-ide-book/common/images/try.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Try 5: Reading Our Score Data</span></span></span><br />
<br />
<span style="font-weight: bold;" class="mycode_b">Please download the <span style="font-family: Courier;" class="mycode_font">Try5.zip</span> file by selecting the following link: <span style="font-family: Courier;" class="mycode_font"><a href="http://developers.curl.com/wiki/easy-ide-book/try5/Try5.zip" target="_blank" rel="noopener" class="mycode_url">Try5.zip</a></span>.</span><br />
<br />
Unzip the contents to the <span style="font-family: Courier;" class="mycode_font">C:\Curl</span> directory. This is the working directory for all the examples and exercises contained within this tutorial. Note: If <span style="font-family: Courier;" class="mycode_font">C:\Curl</span> has not already been created, you can create it now.<br />
<br />
<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Basics: Reading Data from a File, Line by Line</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
<img src="wiki/easy-ide-book/common/images/step1.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Create the 'Try 5-1' Project</span></span></span><br />
<br />
Close the Try 4 project and then, from the IDE 'File' menu, select 'New Project'. In the 'New Project' dialog box, select “Applet Project” (1), input “Try5-1” (2), specify <span style="font-family: Courier;" class="mycode_font">c:\Curl\lesson\Try5\01_read_file</span> in the “Directory” field (3), set the <span style="font-family: Courier;" class="mycode_font">API Version</span> to <span style="font-family: Courier;" class="mycode_font">6.0</span> (4), and then click OK (5).<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_1.png" /><br />
<br />
<img src="wiki/easy-ide-book/common/images/step2.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Inputting the Program</span></span></span><br />
<br />
Copy the <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\01_read_file\data.txt</span> file to <span style="font-family: Courier;" class="mycode_font">c:\Curl\lesson\Try5\01_read_file</span>. Next, we’ll input the program. You can copy the program code below or use <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\01_read_file\start.curl</span> and then paste it into the editor in the IDE.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
 let loc:Url = {url data.txt}<br />
 let input:#TextInputStream<br />
 let data-array:StringArray = {StringArray}<br />
 let list:DropdownList = {DropdownList width = 5cm}<br />
 <br />
 {try<br />
&nbsp;&nbsp;&nbsp;&nbsp; set input = {read-open loc}<br />
&nbsp;&nbsp;&nbsp;&nbsp; {until input.end-of-stream? do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {if-non-null line = {input.read-line} then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {data-array.append {line.to-String}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;catch e:IOException do<br />
&nbsp;&nbsp;&nbsp;&nbsp; {popup-message An error reading the file has occurred.}<br />
&nbsp;&nbsp;&nbsp;&nbsp; {output e.message}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;finally<br />
&nbsp;&nbsp;&nbsp;&nbsp; {if-non-null input then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {input.close}<br />
&nbsp;&nbsp;&nbsp;&nbsp; }<br />
 }<br />
 <br />
 {for s:String in data-array do<br />
&nbsp;&nbsp;&nbsp;&nbsp; {list.append s}<br />
 }<br />
 <br />
 {list.add-event-handler<br />
&nbsp;&nbsp;&nbsp;&nbsp; {on ValueChanged at lb:DropdownList do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {popup-message lb.value &amp;&nbsp;&nbsp;was selected}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp; }<br />
 }<br />
 <br />
 {VBox<br />
&nbsp;&nbsp;&nbsp;&nbsp; Please select a state,<br />
&nbsp;&nbsp;&nbsp;&nbsp; list<br />
 }<br />
}</code></div></div><br />
<img src="wiki/easy-ide-book/common/images/step3.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Save the File, and then Execute the Program</span></span></span><br />
<br />
After inputting the code, save the file, and then execute the program. The following result will appear in your browser.<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_2.png" />]]></description>
			<content:encoded><![CDATA[<img src="wiki/easy-ide-book/common/images/try.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Try 5: Reading Our Score Data</span></span></span><br />
<br />
<span style="font-weight: bold;" class="mycode_b">Please download the <span style="font-family: Courier;" class="mycode_font">Try5.zip</span> file by selecting the following link: <span style="font-family: Courier;" class="mycode_font"><a href="http://developers.curl.com/wiki/easy-ide-book/try5/Try5.zip" target="_blank" rel="noopener" class="mycode_url">Try5.zip</a></span>.</span><br />
<br />
Unzip the contents to the <span style="font-family: Courier;" class="mycode_font">C:\Curl</span> directory. This is the working directory for all the examples and exercises contained within this tutorial. Note: If <span style="font-family: Courier;" class="mycode_font">C:\Curl</span> has not already been created, you can create it now.<br />
<br />
<img src="wiki/easy-ide-book/common/images/topic.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Basics: Reading Data from a File, Line by Line</span></span></span><img src="wiki/easy-ide-book/common/images/topic.gif" /><br />
<br />
<img src="wiki/easy-ide-book/common/images/step1.gif" /><span style="font-size: large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Create the 'Try 5-1' Project</span></span></span><br />
<br />
Close the Try 4 project and then, from the IDE 'File' menu, select 'New Project'. In the 'New Project' dialog box, select “Applet Project” (1), input “Try5-1” (2), specify <span style="font-family: Courier;" class="mycode_font">c:\Curl\lesson\Try5\01_read_file</span> in the “Directory” field (3), set the <span style="font-family: Courier;" class="mycode_font">API Version</span> to <span style="font-family: Courier;" class="mycode_font">6.0</span> (4), and then click OK (5).<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_1.png" /><br />
<br />
<img src="wiki/easy-ide-book/common/images/step2.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Inputting the Program</span></span></span><br />
<br />
Copy the <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\01_read_file\data.txt</span> file to <span style="font-family: Courier;" class="mycode_font">c:\Curl\lesson\Try5\01_read_file</span>. Next, we’ll input the program. You can copy the program code below or use <span style="font-family: Courier;" class="mycode_font">c:\Curl\Try5\01_read_file\start.curl</span> and then paste it into the editor in the IDE.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>{value<br />
 let loc:Url = {url data.txt}<br />
 let input:#TextInputStream<br />
 let data-array:StringArray = {StringArray}<br />
 let list:DropdownList = {DropdownList width = 5cm}<br />
 <br />
 {try<br />
&nbsp;&nbsp;&nbsp;&nbsp; set input = {read-open loc}<br />
&nbsp;&nbsp;&nbsp;&nbsp; {until input.end-of-stream? do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {if-non-null line = {input.read-line} then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {data-array.append {line.to-String}}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;catch e:IOException do<br />
&nbsp;&nbsp;&nbsp;&nbsp; {popup-message An error reading the file has occurred.}<br />
&nbsp;&nbsp;&nbsp;&nbsp; {output e.message}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;finally<br />
&nbsp;&nbsp;&nbsp;&nbsp; {if-non-null input then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {input.close}<br />
&nbsp;&nbsp;&nbsp;&nbsp; }<br />
 }<br />
 <br />
 {for s:String in data-array do<br />
&nbsp;&nbsp;&nbsp;&nbsp; {list.append s}<br />
 }<br />
 <br />
 {list.add-event-handler<br />
&nbsp;&nbsp;&nbsp;&nbsp; {on ValueChanged at lb:DropdownList do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {popup-message lb.value &amp;&nbsp;&nbsp;was selected}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp; }<br />
 }<br />
 <br />
 {VBox<br />
&nbsp;&nbsp;&nbsp;&nbsp; Please select a state,<br />
&nbsp;&nbsp;&nbsp;&nbsp; list<br />
 }<br />
}</code></div></div><br />
<img src="wiki/easy-ide-book/common/images/step3.gif" /><span style="font-size: x-large;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b">Save the File, and then Execute the Program</span></span></span><br />
<br />
After inputting the code, save the file, and then execute the program. The following result will appear in your browser.<br />
<img src="wiki/easy-ide-book/try5/images/figure_5_2.png" />]]></content:encoded>
		</item>
	</channel>
</rss>