<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Reborn &#187; Auto-It</title>
	<atom:link href="http://www.lixiaolai.com/archives/category/computer-related/auto-it/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lixiaolai.com</link>
	<description></description>
	<lastBuildDate>Sun, 12 Feb 2012 17:28:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>AutoIt教程：11. 什么叫“为所欲为”</title>
		<link>http://www.lixiaolai.com/archives/9304.html</link>
		<comments>http://www.lixiaolai.com/archives/9304.html#comments</comments>
		<pubDate>Fri, 07 May 2010 01:24:24 +0000</pubDate>
		<dc:creator>xiaolai</dc:creator>
				<category><![CDATA[Auto-It]]></category>

		<guid isPermaLink="false">http://www.lixiaolai.com/?p=9304</guid>
		<description><![CDATA[相信我，总有一天，你会被一些程序惹恼。 金山词霸的生词本就是这么个程序，尽管它也确实很有用。 这个程序窗口上的所有按钮都没有快捷键！也就是说，如果你想编辑哪一个条目的话，就能用鼠标操作，先选中要编辑的条目，而后用鼠标点击“编辑”按钮，然后在跳出的窗口中操作键盘，再然后你的手要离开键盘，拿起鼠标，继续以后的操作……（这种程序一看就是“员工按最小工作量完成老板交代的工作”的结果，本质上它根本就不是写给用户的。） 不过，如果你会用AutoIt的话，那么，你就不仅不会因此生气，还可能会因此高兴──因为你很快就可以解决问题，而与此同时，你知道生气无奈的是别人，并且大有人在…… If Not ProcessExists&#40;&#34;NewWord2008.exe&#34;&#41; Then Run&#40;&#34;C:\Program Files\Kingsoft\PowerWord_Oxford\NewWord2008.exe&#34;&#41; &#160; HotKeySet&#40;&#34;!a&#34;, &#34;AddButtonClick&#34;&#41; HotKeySet&#40;&#34;!e&#34;, &#34;EditButtonClick&#34;&#41; HotKeySet&#40;&#34;!d&#34;, &#34;DeleteButtonClick&#34;&#41; HotKeySet&#40;&#34;!s&#34;, &#34;SaveButtonClick&#34;&#41; &#160; While 1 If Not ProcessExists&#40;&#34;NewWord2008.exe&#34;&#41; Then ExitLoop Sleep&#40;100&#41; WEnd &#160; Func AddButtonClick&#40;&#41; ControlClick&#40;&#34;生词本&#34;, &#34;&#34;, &#34;Button9&#34;&#41; EndFunc &#160; Func EditButtonClick&#40;&#41; ControlClick&#40;&#34;生词本&#34;, &#34;&#34;, &#34;Button10&#34;&#41; EndFunc &#160; Func DeleteButtonClick&#40;&#41; ControlClick&#40;&#34;生词本&#34;, &#34;&#34;, &#34;Button11&#34;&#41; EndFunc &#160; Func SaveButtonClick&#40;&#41; ControlClick&#40;&#34;生词本&#34;, &#34;&#34;, [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>相信我，总有一天，你会被一些程序惹恼。</p>
<p>金山词霸的生词本就是这么个程序，尽管它也确实很有用。</p>
<p><a href="http://www.lixiaolai.com/wp-content/uploads/2010/05/NewWord2008.png"><img class="aligncenter size-full wp-image-9306" title="NewWord2008" src="http://www.lixiaolai.com/wp-content/uploads/2010/05/NewWord2008-420.png" alt="" width="420" height="276" /></a></p>
<p>这个程序窗口上的所有按钮都没有快捷键！也就是说，如果你想编辑哪一个条目的话，就能用鼠标操作，先选中要编辑的条目，而后用鼠标点击“编辑”按钮，然后在跳出的窗口中操作键盘，再然后你的手要离开键盘，拿起鼠标，继续以后的操作……（这种程序一看就是“员工按最小工作量完成老板交代的工作”的结果，本质上它根本就不是写给用户的。）</p>
<p>不过，如果你会用AutoIt的话，那么，你就不仅不会因此生气，还可能会因此高兴──因为你很快就可以解决问题，而与此同时，你知道生气无奈的是别人，并且大有人在……</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #0000FF; font-weight: bold;">Not</span> <span style="color: #000080; font-style: italic; font-weight: bold;">ProcessExists</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;NewWord2008.exe&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #000080; font-style: italic; font-weight: bold;">Run</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;C:\Program Files\Kingsoft\PowerWord_Oxford\NewWord2008.exe&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000080; font-style: italic; font-weight: bold;">HotKeySet</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;!a&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;AddButtonClick&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">HotKeySet</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;!e&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;EditButtonClick&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">HotKeySet</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;!d&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;DeleteButtonClick&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">HotKeySet</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;!s&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;SaveButtonClick&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #0000FF; font-weight: bold;">Not</span> <span style="color: #000080; font-style: italic; font-weight: bold;">ProcessExists</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;NewWord2008.exe&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">ExitLoop</span>
    <span style="color: #000080; font-style: italic; font-weight: bold;">Sleep</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">100</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Func</span> AddButtonClick<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">ControlClick</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;生词本&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Button9&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Func</span> EditButtonClick<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">ControlClick</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;生词本&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Button10&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Func</span> DeleteButtonClick<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">ControlClick</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;生词本&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Button11&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Func</span> SaveButtonClick<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">ControlClick</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;生词本&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Button12&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span></pre></div></div>

<p>以后你就可以用这个程序启动金山词霸的生词本，而后可以用以下快捷键控制程序：</p>
<ul>
<li>Alt+A: 添加</li>
<li>Alt+E: 编辑</li>
<li>Alt+D: 删除</li>
<li>Alt+S: 保存</li>
</ul>
<p>尽管程序简单，但理解起来还是比较绕的；请逐一查询陌生函数的帮助文档。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lixiaolai.com/archives/9304.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>AutoIt教程：10. 模拟键盘鼠标操作</title>
		<link>http://www.lixiaolai.com/archives/9291.html</link>
		<comments>http://www.lixiaolai.com/archives/9291.html#comments</comments>
		<pubDate>Fri, 07 May 2010 00:19:48 +0000</pubDate>
		<dc:creator>xiaolai</dc:creator>
				<category><![CDATA[Auto-It]]></category>

		<guid isPermaLink="false">http://www.lixiaolai.com/?p=9291</guid>
		<description><![CDATA[AutoIt的独特之处之一在于它可以非常方便地模拟Windows上的键盘鼠标操作，进而“自动”、“批量”地完成一些复杂而又重复的操作。 想象一下： 你有一个词表，你想把它的所有单词在Collins COUBILD中的释义文本拷贝出来。 那么，你原本只能这样： 打开词表文本文件； 新建一个文本文档（如果想要保留格式的话，就要新建一个rtf文件──用“写字板”程序，而非“记事本”） 打开Collins词典（Collins COBUILD Dictionary On CDROM） 从词表文件中Control+C拷贝出一个词 切换到Collins词典，将刚刚拷贝的词粘贴到输入框中，而后按回车键 再从菜单中选择Edit，再选择Copy Entry 切换到写字板程序，按Control+V粘贴 &#8230; 周而复始，直至所有词汇全部处理完毕…… 也许过去你就是这么做的。可是，现在你可以像这段录像所演示的那样：RetriveEntryTextFromCollins（或者你可以在这个链接上点击鼠标右键，选择“另存为……”将其下载到本地查看。） 程序的代码很简单（点击这个链接下载这个代码和它所用到的词表文件；另，你的电脑上应该已经安装好Collins COBUILD Dictionary On CDROM；还有，在执行这个程序之前，你得先打开一个写字板程序、打开Collins词典……）： $file = FileOpen&#40;&#34;WordList.txt&#34;&#41; While 1 $line = FileReadLine&#40;$file&#41; If @error = -1 Then ExitLoop CopyEntryFromCollinsToWordPad&#40;$line, &#34;Document - WordPad&#34;&#41; WEnd &#160; Func CopyEntryFromCollinsToWordPad&#40;$Entry, $WordPadTitle = &#34;文档 - 写字板&#34;, $CollinsTitle = &#34;Collins [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>AutoIt的独特之处之一在于它可以非常方便地模拟Windows上的键盘鼠标操作，进而“自动”、“批量”地完成一些复杂而又重复的操作。</p>
<p>想象一下：</p>
<blockquote><p>你有一个词表，你想把它的所有单词在Collins COUBILD中的释义文本拷贝出来。</p></blockquote>
<p>那么，你原本只能这样：</p>
<blockquote><p>打开词表文本文件；<br />
新建一个文本文档（如果想要保留格式的话，就要新建一个rtf文件──用“写字板”程序，而非“记事本”）<br />
打开Collins词典（<a href="http://www.lixiaolai.com/archives/7759.html">Collins COBUILD Dictionary On CDROM</a>）</p>
<p>从词表文件中Control+C拷贝出一个词<br />
切换到Collins词典，将刚刚拷贝的词粘贴到输入框中，而后按回车键<br />
再从菜单中选择Edit，再选择Copy Entry<br />
切换到写字板程序，按Control+V粘贴<br />
&#8230;<br />
周而复始，直至所有词汇全部处理完毕……</p></blockquote>
<p>也许过去你就是这么做的。可是，现在你可以像这段录像所演示的那样：<a href="http://www.lixiaolai.com/wp-content/uploads/2010/05/RetriveEntryTextFromCollins.swf" target="_blank">RetriveEntryTextFromCollins</a>（或者你可以在这个链接上点击鼠标右键，选择“另存为……”将其下载到本地查看。）</p>
<p>程序的代码很简单（<a href="http://www.lixiaolai.com/wp-content/uploads/2010/05/RETFC.zip" target="_blank">点击这个链接下载这个代码和它所用到的词表文件</a>；另，你的电脑上应该已经安装好Collins COBUILD Dictionary On CDROM；还有，在执行这个程序之前，你得先打开一个写字板程序、打开Collins词典……）：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$file</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileOpen</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;WordList.txt&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
	<span style="font-weight: bold; color: #AA0000;">$line</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileReadLine</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@error</span></span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #FF0000; font-weight: bold;">-</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">ExitLoop</span>
	CopyEntryFromCollinsToWordPad<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$line</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Document - WordPad&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Func</span> CopyEntryFromCollinsToWordPad<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$Entry</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$WordPadTitle</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;文档 - 写字板&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$CollinsTitle</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Collins COBUILD - Lexicon&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> 
&nbsp;
	<span style="color: #000080; font-style: italic; font-weight: bold;">ControlSend</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$CollinsTitle</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Edit2&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$Entry</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="font-weight: bold; color: #9977BB;">&quot;{Enter}&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> 
&nbsp;
	<span style="color: #000080; font-style: italic; font-weight: bold;">WinWait</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$CollinsTitle</span><span style="color: #FF0000; font-weight: bold;">,</span><span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #0000FF; font-weight: bold;">Not</span> <span style="color: #000080; font-style: italic; font-weight: bold;">WinActive</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$CollinsTitle</span><span style="color: #FF0000; font-weight: bold;">,</span><span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #000080; font-style: italic; font-weight: bold;">WinActivate</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$CollinsTitle</span><span style="color: #FF0000; font-weight: bold;">,</span><span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">WinWaitActive</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$CollinsTitle</span><span style="color: #FF0000; font-weight: bold;">,</span><span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">Send</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;{ALTDOWN}{ALTUP}ee&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
	<span style="color: #000080; font-style: italic; font-weight: bold;">WinWait</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$WordPadTitle</span><span style="color: #FF0000; font-weight: bold;">,</span><span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #0000FF; font-weight: bold;">Not</span> <span style="color: #000080; font-style: italic; font-weight: bold;">WinActive</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$WordPadTitle</span><span style="color: #FF0000; font-weight: bold;">,</span><span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #000080; font-style: italic; font-weight: bold;">WinActivate</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$WordPadTitle</span><span style="color: #FF0000; font-weight: bold;">,</span><span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">WinWaitActive</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$WordPadTitle</span><span style="color: #FF0000; font-weight: bold;">,</span><span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">Send</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;{CTRLDOWN}v{CTRLUP}&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
	<span style="color: #000080; font-style: italic; font-weight: bold;">Send</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;{Enter}&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">Send</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;================================&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">Send</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;{Enter}&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span>
&nbsp;
&nbsp;
<span style="font-style: italic; color: #009933;">; Finished!</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;AutoIt Message&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Finished!&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>试用过后，请打开AutoIt帮助文件，细读这段代码中所用到的所有函数帮助文档。</p>
<p>另，AutoIt有一个“AutoIt Windows Info”程序（在开始菜单中可以找到），是用来获得Windows窗口信息的，调用出来把玩一下：</p>
<p><img src="http://www.lixiaolai.com/wp-content/uploads/2010/05/AutoIt-Window-Info.png" alt="" title="AutoIt Window Info" width="312" height="470" class="aligncenter size-full wp-image-9298" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.lixiaolai.com/archives/9291.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>自动更换网桥</title>
		<link>http://www.lixiaolai.com/archives/9278.html</link>
		<comments>http://www.lixiaolai.com/archives/9278.html#comments</comments>
		<pubDate>Tue, 04 May 2010 11:55:41 +0000</pubDate>
		<dc:creator>xiaolai</dc:creator>
				<category><![CDATA[Auto-It]]></category>

		<guid isPermaLink="false">http://www.lixiaolai.com/?p=9278</guid>
		<description><![CDATA[这是过去我为了方便用AutoIt写的。 这个程序很简单，为你自动更新最新的网桥（bridge for tor）。双击执行该程序： 如果你的Windows上没有安装Vidalia，那它会自动开始下载并开始安装（请不要更改默认安装位置）…… 否则…… (如果你正在运行Vidalia，它会退出Vidalia) 它将自动找到最新的网桥，添加进Vidalia的设置，然后，启动Vidalia…… 如果你不知道Vidalia是干什么的，那就看看这个文档：http://is.gd/53NAv。 啥也别说。你懂的。]]></description>
			<content:encoded><![CDATA[<p></p><p>这是过去我为了方便<a href="http://www.lixiaolai.com/index.php/archives/category/computer-related/auto-it">用AutoIt</a>写的。</p>
<p><a href="http://ifile.it/kcboa43" target="_blank">这个程序很简单</a>，为你自动更新最新的网桥（bridge for tor）。双击执行该程序：</p>
<ul>
<li>如果你的Windows上没有安装Vidalia，那它会自动开始下载并开始安装（请不要更改默认安装位置）……</li>
<li>否则……</li>
<li>(如果你正在运行Vidalia，它会退出Vidalia) 它将自动找到最新的网桥，添加进Vidalia的设置，然后，启动Vidalia……</li>
</ul>
<p>如果你不知道Vidalia是干什么的，那就看看这个文档：<a href="http://is.gd/53NAv" target="_blank">http://is.gd/53NAv</a>。</p>
<p>啥也别说。你懂的。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lixiaolai.com/archives/9278.html/feed</wfw:commentRss>
		<slash:comments>38</slash:comments>
		</item>
		<item>
		<title>AutoIt教程：9. 问题、方案与实现</title>
		<link>http://www.lixiaolai.com/archives/9264.html</link>
		<comments>http://www.lixiaolai.com/archives/9264.html#comments</comments>
		<pubDate>Mon, 03 May 2010 03:40:16 +0000</pubDate>
		<dc:creator>xiaolai</dc:creator>
				<category><![CDATA[Auto-It]]></category>

		<guid isPermaLink="false">http://www.lixiaolai.com/?p=9264</guid>
		<description><![CDATA[第7节里提到过的问题，实际上在《把时间当作朋友》一书中出现过，作为反驳“成功学谬误”的例子： 除了“以偏概全”、“单向成立”之外，“成功学”书籍里甚至不乏赤裸裸的、厚颜无耻逻辑混乱。最令我印象深刻的赤裸裸地逻辑混乱的“成功学”例子是关于“态度改变一切”的一个例子。我知道，也相信“有些时候，仅仅态度上的改变就真的可以带来不同的结果”。然而，我认为用以下的逻辑让我接受，或者让任何人接受，都不仅是不可能的，甚至是不道德的。 如果将字母 A 到 Z 分别编上 1 到 26 的分数，(A=1,B=2…,Z=26) ，那么： 你的知识(KNOWLEDGE)得到 96 分(11+14+15+23+12+5+4+7+5=96) 你的努力(HARDWORK)也只得到 98 分(8+1+18+4+23+15+18+11=98) 你的态度(ATTITUDE)才是左右你生命的全部，因为1+20+20+9+20+21+4+5=100 拜托，有点基础逻辑好不好呢？事实上，用这种方法计算，结果等于100的单词多的去了： alienation (疏远) apoplectic (中风患者) boycott (联合抵制) cacophony (杂音、刺耳的音调) chimpanzee (黑猩猩) connivance (纵容) coyness (羞怯) flurry (慌张) frisson (颤抖) impotence (阳痿) inflation (通货膨胀) pussy (小猫&#60;俚&#62;女阴/阴户) socialism (社会主义) status (身份、地位) stress (压力) surcharge (超载, 追加罚款, 额外费) syndicate [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>第7节里提到过的问题，实际上在《把时间当作朋友》一书中出现过，<a href="http://www.lixiaolai.com/archives/442.html" target="_blank">作为反驳“成功学谬误”的例子</a>：</p>
<blockquote><p>除了“以偏概全”、“单向成立”之外，“成功学”书籍里甚至不乏赤裸裸的、厚颜无耻逻辑混乱。最令我印象深刻的赤裸裸地逻辑混乱的“成功学”例子是关于“态度改变一切”的一个例子。我知道，也相信“有些时候，仅仅态度上的改变就真的可以带来不同的结果”。然而，我认为用以下的逻辑让我接受，或者让任何人接受，都不仅是不可能的，甚至是不道德的。</p>
<blockquote><p>如果将字母 A 到 Z 分别编上 1 到 26 的分数，(A=1,B=2…,Z=26) ，那么：</p>
<ul>
<li>你的知识(KNOWLEDGE)得到 96 分(11+14+15+23+12+5+4+7+5=96)</li>
<li>你的努力(HARDWORK)也只得到 98 分(8+1+18+4+23+15+18+11=98)</li>
<li>你的态度(ATTITUDE)才是左右你生命的全部，因为1+20+20+9+20+21+4+5=100</li>
</ul>
</blockquote>
<p>拜托，有点基础逻辑好不好呢？事实上，用这种方法计算，结果等于100的单词多的去了：</p>
<blockquote><p>alienation (疏远)<br />
apoplectic (中风患者)<br />
boycott (联合抵制)<br />
cacophony (杂音、刺耳的音调)<br />
chimpanzee (黑猩猩)<br />
connivance (纵容)<br />
coyness (羞怯)<br />
flurry (慌张)<br />
frisson (颤抖)<br />
impotence (阳痿)<br />
inflation (通货膨胀)<br />
pussy (小猫&lt;俚&gt;女阴/阴户)<br />
socialism (社会主义)<br />
status (身份、地位)<br />
stress (压力)<br />
surcharge (超载, 追加罚款, 额外费)<br />
syndicate (财团)<br />
tuppence (微不足道的东西)<br />
turkey (火鸡，无用的东西)<br />
Wednesday (星期三)<br />
wholesale (批发)</p></blockquote>
<p>以上罗列的只是我在BNC英文词汇表中找到的一千多个中的一小部分名词而已。对了，形容词useless如果按照这种算法，结果也等于100。补记：刚刚写完，顺手在GOOGLE上搜索了一下 “KNOWLEDGE 11+14+15+23+12+5+4+7+5=96″。得到的结果特别值得回味：前150项几乎都来自中文网站。搜索结果的第16页才开始陆续有一些英文网站提到这个莫名其妙的算法，在GOOGLE搜索结果的第151项 看到这么一篇：指出 bullshit = 103，于是接着说，“So, it stands to reason that hard work and knowledge will get you close, attitude will get you there, but bullshit will put you over the top.” 还有更逗的，作者又接着指出另外一个词：<strong>asskissing=127</strong> ！</p></blockquote>
<p>当时我就想，要是有很多莫名其妙的词按这种算法加起来也等于100呢？我猜肯定有的！只不过，用手工计算是（几乎）不可能的事情。还好，我有工具<sup>[<a href="http://www.lixiaolai.com/archives/9264.html#footnote_0_9264" id="identifier_0_9264" class="footnote-link footnote-identifier-link" title="当时我用的是Python，现在是在讲解AutoIt，所以当然得用AutoIt重写一遍。">1</a>]</sup> 和技能，所以，就可以去验证，而后“精彩而又直接地”驳斥这种荒谬的说法。</p>
<p>有些时候我们非常依赖工具和技能。在心存狐疑之时，拥有工具掌握技能的人，可以去验证；而没有工具没有技能的人，只能“心有余力不足”。事实上，大多数“力不足”的人“心并无余”，他们甚至不会“心存狐疑”，而是直接“被震惊”、“被惊喜”，“被打动”……直接被这种说法带到坑里去了……</p>
<p>问题是：“有哪些词按这种算法也等于100？”──而解决方案倒也简单：</p>
<ul>
<li>找一个词表（如有必要，就把它转换为文本文件，便于程序阅读）</li>
<li>逐一计算</li>
<li>把那些按这种算法计算等于100的词挑出来</li>
</ul>
<p>刚开始的时候，其实没必要一定要先去找个词表，我只新建了名称为一个“wordlist.txt”的文件，其中只有以下几行内容：</p>
<blockquote><p>Hardwork<br />
Attitude<br />
Knowledge<br />
Success<br />
Anything</p></blockquote>
<p>而后我们就可以写这样的代码处理这个文本文件中的每一行：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$file</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileOpen</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;wordlist.txt&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
	<span style="font-weight: bold; color: #AA0000;">$line</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileReadLine</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@error</span></span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #FF0000; font-weight: bold;">-</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">ExitLoop</span>
	<span style="font-style: italic; color: #009933;">; test if sum of character values in this word equals to 100, if so, print it to the output...</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">FileClose</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>注意，在AutoIt代码中，分号“;”后面的内容是“注释”，是给代码阅读者看的，AutoIt在执行代码的时候，忽略一切“;”和它后面的内容。注释的写法有若干种，请参阅帮助文档。</p>
<p>现在的问题是，如何才能计算出一个单词按这种算法等于多少呢？我到AutoIt的帮助文件里翻了翻与字符串（String）相关的函数，看看有没有能够用得上的……当然有，我找到了个<strong>StringSplit()</strong>。它会把一个字符串按照指定的“分隔符”拆分成若干段，而后把这些拆分后的片段按顺序保存在一个数组之中返回……如果分隔符是<strong>空字符</strong>（即，<strong>&#8220;&#8221;</strong>)，那么它就把一个字符串拆分成一个字母一个片段……就是它了！</p>
<p>大致看了一下帮助文档，而后写了个代码片段测试这个函数：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #F000FF; font-style: italic;">#Include</span>
<span style="color: #0080FF; font-style: italic; font-weight: bold;">_ArrayDisplay</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #000080; font-style: italic; font-weight: bold;">StringSplit</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;Success&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>结果竟然出错了……仔细看了看出错提示，才发现<strong>_ArrayDisplay()</strong>这个函数第一个参数标明了是<strong>ByRef</strong>的，即，只接受变量作为参数，不接受表达式或函数调用。所以，改了改测试代码：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #F000FF; font-style: italic;">#Include</span>
<span style="font-weight: bold; color: #AA0000;">$a</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringSplit</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;Success&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0080FF; font-style: italic; font-weight: bold;">_ArrayDisplay</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$a</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>运行结果如下：</p>
<p><img class="aligncenter size-full wp-image-9265" title="ch09-01" src="http://www.lixiaolai.com/wp-content/uploads/2010/05/ch09-01.png" alt="" width="250" height="214" /></p>
<p>看懂了，原来StringSplit()返回的是一个这样的数组：$a[0]之中保存的是“分割后片段的个数”（即，Success这个字符串总计7个字母），而后的元素则为每一个字母……</p>
<p>现在的问题是，如何让a=1，b=2&#8230; c=26呢？</p>
<p>在AutoIt帮助文件的Variables and Conversion章节中，找到一个函数<strong>Asc()</strong>。这个函数返回一个字符对应的ASCII代码。例如：</p>
<blockquote><p>Asc(&#8220;0&#8243;) == 48, Asc(&#8220;9&#8243;) == 57, Asc(&#8220;A&#8221;) == 65, Asc(&#8220;Z&#8221;) == 90, Asc(&#8220;a&#8221;) == 97, Asc(&#8220;z&#8221;) == 122</p></blockquote>
<p>所以，Asc(&#8220;a&#8221;) == 97 、 Asc(&#8220;b&#8221;) == 98 &#8230; 于是，只要用这个函数的返回值再减去96就可以得到1、2、&#8230;26这些数值了。不过，还有个问题，大小写字母的值不一样，Asc(&#8220;A&#8221;) == 65，而Asc(&#8220;a&#8221;) == 97……再去看看跟String相关的函数，找到一个<strong>StringLower()</strong>──我们可以先用它把一个词中的字符全部转换为小写，而后再进行计算……</p>
<p>现在我们可以写出（或起码能够完全理解）以下的代码了：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$file</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileOpen</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;wordlist.txt&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
	<span style="font-weight: bold; color: #AA0000;">$line</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileReadLine</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@error</span></span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #FF0000; font-weight: bold;">-</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">ExitLoop</span>
	<span style="font-style: italic; color: #009933;">; test if sum of character values in this word equals to 100, if so, print it to the output...</span>
	<span style="font-weight: bold; color: #AA0000;">$wordLowerCase</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringLower</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$line</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$Characters</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringSplit</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$wordLowerCase</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$sum</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
	<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="font-weight: bold; color: #AA0000;">$characters</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#93;</span>
		<span style="font-weight: bold; color: #AA0000;">$sum</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$sum</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="color: #000080; font-style: italic; font-weight: bold;">Asc</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$characters</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="font-weight: bold; color: #AA0000;">$i</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #FF0000; font-weight: bold;">-</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">96</span>
	<span style="color: #0000FF; font-weight: bold;">Next</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$line</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$sum</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span></pre></div></div>

<p><img class="aligncenter size-full wp-image-9270" title="ch09-02" src="http://www.lixiaolai.com/wp-content/uploads/2010/05/ch09-02.png" alt="" width="104" height="100" /></p>
<p>仔细看看，就会发现“计算总和”的过程是可以被独立出来的，那还不如把它写成一个函数，将来可以随意调用呢：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$file</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileOpen</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;wordlist.txt&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
	<span style="font-weight: bold; color: #AA0000;">$line</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileReadLine</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@error</span></span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #FF0000; font-weight: bold;">-</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">ExitLoop</span>
	<span style="font-style: italic; color: #009933;">; test if sum of character values in this word equals to 100, if so, print it to the output...</span>
	<span style="font-weight: bold; color: #AA0000;">$sum</span> <span style="color: #FF0000; font-weight: bold;">=</span> AddUpCharactersInWordAsNumber<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$line</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$line</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$sum</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Func</span> AddUpCharactersInWordAsNumber<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$word</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$wordLowerCase</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringLower</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$word</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$Characters</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringSplit</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$wordLowerCase</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$count</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
	<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="font-weight: bold; color: #AA0000;">$Characters</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#93;</span>
		<span style="font-weight: bold; color: #AA0000;">$count</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$count</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="color: #000080; font-style: italic; font-weight: bold;">Asc</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$Characters</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="font-weight: bold; color: #AA0000;">$i</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #FF0000; font-weight: bold;">-</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">96</span>
	<span style="color: #0000FF; font-weight: bold;">Next</span>
	<span style="color: #0000FF; font-weight: bold;">Return</span> <span style="font-weight: bold; color: #AA0000;">$count</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span></pre></div></div>

<p>如此这般之后，我们可以去找一个很全的英文词汇表去测试了。我到网上Google了一下“<a href="http://is.gd/bRBEg" target="_blank">BNC Frequency Word List</a>”，很快在kilgarriff.co.uk这个网站上找到一个词汇表，“<a href="http://www.kilgarriff.co.uk/BNClists/lemma.al" target="_blank">lemma.al</a>”。网站上有这个词表的格式说明：</p>
<blockquote><p>The format for the list is:</p>
<pre>        sort-order, frequency, word, word-class</pre>
<p>and a sample from the top of the alphabetically-ordered list is:</p>
<pre>	5 2186369 a det
	2107 4249 abandon v
	5204 1110 abbey n
	966 10468 ability n
	321 30454 able a</pre>
</blockquote>
<p>这个比较好办，就用我们刚刚用过的StringSplit()函数，用一个空格（&#8221; &#8220;）作为分隔符，就能把每一行中的“word”提取出来，然后在调用我们刚写过的AddUpCharactersInWordAsNumber()函数就可以算出数值……</p>
<p>然后，程序就变成了这个样子：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #000080; font-style: italic; font-weight: bold;">InetGet</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;http://www.kilgarriff.co.uk/BNClists/lemma.al&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;lemma.al&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="font-weight: bold; color: #AA0000;">$file</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileOpen</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;lemma.al&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
	<span style="font-weight: bold; color: #AA0000;">$line</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileReadLine</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@error</span></span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #FF0000; font-weight: bold;">-</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">ExitLoop</span>
	<span style="font-weight: bold; color: #AA0000;">$lineElements</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringSplit</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$line</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot; &quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> AddUpCharactersInWordAsNumber<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$lineElements</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">3</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #FF0000; font-weight: bold;">==</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">100</span> <span style="color: #0000FF; font-weight: bold;">Then</span>
		<span style="color: #000080; font-style: italic; font-weight: bold;">ConsoleWrite</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$lineElements</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">3</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@CRLF</span></span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">EndIf</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">FileClose</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Finished!&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Func</span> AddUpCharactersInWordAsNumber<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$word</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$wordLowerCase</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringLower</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$word</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$Characters</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringSplit</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$wordLowerCase</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$count</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
	<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="font-weight: bold; color: #AA0000;">$Characters</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#93;</span>
		<span style="font-weight: bold; color: #AA0000;">$count</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$count</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="color: #000080; font-style: italic; font-weight: bold;">Asc</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$Characters</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="font-weight: bold; color: #AA0000;">$i</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #FF0000; font-weight: bold;">-</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">96</span>
	<span style="color: #0000FF; font-weight: bold;">Next</span>
	<span style="color: #0000FF; font-weight: bold;">Return</span> <span style="font-weight: bold; color: #AA0000;">$count</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span></pre></div></div>

<p>如果你想把结果都写到一个文本文件之中，可以把第9行的</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #000080; font-style: italic; font-weight: bold;">ConsoleWrite</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$lineElements</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">3</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@CRLF</span></span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>改成</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #000080; font-style: italic; font-weight: bold;">FileWriteLine</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;result.txt&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$lineElements</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">3</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>而<strong>FileWriteLine()</strong>这个函数的用法，当然要参见帮助文档。</p>
<blockquote><p><strong>作业</strong>：</p>
<ul>
<li>把程序里的StringLower()函数换成StringUpper()，并修改其它相应的部分，使其得出正常结果。</li>
<li>这里有个更长的词表：http://www.kilgarriff.co.uk/BNClists/all.al.gz，下载后解压缩，看看这个词表中有哪些词按这种算法等于100？</li>
<li>这里还有一个词表：http://www.bckelk.ukfsn.org/words/wlist.zip,下载后解压缩，看看这个词表中有哪些词按这种算法等于100？</li>
<li>回顾一下之前的所有文章，确保每一行代码都读得懂……</li>
</ul>
</blockquote>
<hr />
<p><span>Footnotes:</span></p><ol class="footnotes"><li id="footnote_0_9264" class="footnote">当时我用的是<em>Python</em>，现在是在讲解<strong>AutoIt</strong>，所以当然得用AutoIt重写一遍。</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.lixiaolai.com/archives/9264.html/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>AutoIt教程：8. 阅读帮助文件</title>
		<link>http://www.lixiaolai.com/archives/9248.html</link>
		<comments>http://www.lixiaolai.com/archives/9248.html#comments</comments>
		<pubDate>Sun, 02 May 2010 16:35:38 +0000</pubDate>
		<dc:creator>xiaolai</dc:creator>
				<category><![CDATA[Auto-It]]></category>

		<guid isPermaLink="false">http://www.lixiaolai.com/?p=9248</guid>
		<description><![CDATA[AutoIt实际上很强大，至少“足够强大”。除了它的内建函数相当丰富之外[1] ，还有大量的UDF，并且，在AutoIt论坛上时时刻刻都有人在不断地补充各种功用的新UDF。 尽管看起来很简单，这一节可能是最重要的内容。 先让我们拿MsgBox()这个函数作为讲解起点。打开MsgBox()这个函数的帮助文档，我们最先看到的是它的简要说明和参数定义： MsgBox Displays a simple message box with optional timeout. MsgBox &#40; flag, &#34;title&#34;, &#34;text&#34; &#91;, timeout &#91;, hwnd&#93;&#93; &#41; 参数定义中的方括号（“[ ]”）的含义是，“该参数在函数定义中设定了默认值，所以，这个参数是可选参数”，即，可以不指定这个参数，如果不指定的话，则直接使用函数定义的默认值──而这个默认值究竟是什么，在接下来的文档中有详细的解释。 Parameters flag The flag indicates the type of message box and the possible button combinations. See remarks. title The title of the message box. text The text of the message [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>AutoIt实际上很强大，至少“足够强大”。除了它的内建函数相当丰富之外<sup>[<a href="http://www.lixiaolai.com/archives/9248.html#footnote_0_9248" id="identifier_0_9248" class="footnote-link footnote-identifier-link" title="以后你会知道，任何一种编程语言，真正强大的地方都在于它们各自的&ldquo;函数库&rdquo;（Library）&hellip;&hellip;">1</a>]</sup> ，还有大量的UDF，并且，在AutoIt论坛上时时刻刻都有人在不断地补充各种功用的新UDF。</p>
<p><strong>尽管看起来很简单，这一节可能是最重要的内容。</strong></p>
<p>先让我们拿MsgBox()这个函数作为讲解起点。打开MsgBox()这个函数的帮助文档，我们最先看到的是它的简要说明和参数定义：</p>
<blockquote><p><span style="font-size: xx-small;">MsgBox</span><br />
Displays a simple message box with optional timeout.</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span> <span style="color: #FF0000; font-weight: bold;">&#40;</span> flag<span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;title&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;text&quot;</span> <span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #FF0000; font-weight: bold;">,</span> timeout <span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #000080; font-style: italic; font-weight: bold;">hwnd</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

</blockquote>
<p>参数定义中的方括号（“<strong>[ ]</strong>”）的含义是，“该参数在函数定义中设定了<strong>默认值</strong>，所以，这个参数是<strong>可选参数</strong>”，即，可以不指定这个参数，如果不指定的话，则直接使用函数定义的默认值──而这个默认值究竟是什么，在接下来的文档中有详细的解释。</p>
<blockquote><p><strong>Parameters</strong></p>
<table border="1" cellspacing="0" cellpadding="3" width="100%" bordercolor="#C0C0C0">
<tbody>
<tr>
<td width="15%">flag</td>
<td width="85%">The flag indicates the type of message box and the possible button combinations.  See remarks.</td>
</tr>
<tr>
<td>title</td>
<td>The title of the message box.</td>
</tr>
<tr>
<td>text</td>
<td>The text of the message box.</td>
</tr>
<tr>
<td>timeout</td>
<td><strong>[optional]</strong> Timeout in seconds.  After the timeout has elapsed the message box will be automatically closed.  The default is 0, which is no timeout.</td>
</tr>
<tr>
<td>hwnd</td>
<td><strong>[optional]</strong> The window handle to use as the parent for this dialog.</td>
</tr>
</tbody>
</table>
</blockquote>
<p>读过文档之后，就会知道</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">4096</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Test&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;This box has no timeout limit&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>和</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">4096</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Test&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;This box has no timeout limit&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>是一样的。</p>
<p>而“4096”究竟是什么意思，在接下来的文档中可以找到：</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%" bordercolor="#C0C0C0">
<tbody>
<tr>
<td style="border-bottom-style: solid; border-bottom-width: 1;"><strong>decimal flag</strong></td>
<td style="border-bottom-style: solid; border-bottom-width: 1;"><strong>Button-related Result</strong></td>
<td style="border-bottom-style: solid; border-bottom-width: 1;"><strong>hexadecimal flag</strong></td>
</tr>
<tr>
<td>4096</td>
<td>System modal (dialog has an icon)</td>
<td>0&#215;1000</td>
</tr>
</tbody>
</table>
<p>在使用MsgBox()的话，AutoIt用这个函数的返回值来判断用户究竟按下了对话框中的哪一个按钮，比如，如果用户按下的是“OK”（确定）按钮，那么返回值是1；如果按下的是“ABORT”（取消）按钮，则返回值是3；如果在Time Out的时间内没有任何动作，返回值是-1……</p>
<p>请尝试一下以下代码（<strong>Testing17.au3</strong>）：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
	<span style="font-weight: bold; color: #AA0000;">$n</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">3</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">'MsgBox Demo'</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">'Please Press one of the buttons...'</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">Select</span>
		<span style="color: #0000FF; font-weight: bold;">Case</span> <span style="font-weight: bold; color: #AA0000;">$n</span> <span style="color: #FF0000; font-weight: bold;">==</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">6</span>
			<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">'Info'</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">'You pressed &quot;YES&quot;'</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">2</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">Case</span> <span style="font-weight: bold; color: #AA0000;">$n</span> <span style="color: #FF0000; font-weight: bold;">==</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">7</span>
			<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">'Info'</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">'You pressed &quot;NO&quot;'</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">2</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">Case</span> <span style="font-weight: bold; color: #AA0000;">$n</span> <span style="color: #FF0000; font-weight: bold;">==</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">2</span>
			<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">'Info'</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">'You pressed &quot;CANCEL&quot;, and quit the program...'</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">2</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
			<span style="color: #0000FF; font-weight: bold;">ExitLoop</span>
	<span style="color: #0000FF; font-weight: bold;">EndSelect</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span></pre></div></div>

<p><strong>注意</strong>：在AutoIt中，每个函数都是有“返回值”的──即便在定义的时候没有给出返回值，AutoIt也会自动返回一个<strong>0</strong>……</p>
<p>请尝试一下以下代码（<strong>Testing18.au3</strong>）：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Func</span> __DoNothing<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Function __DoNothing() returns &quot;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> __DoNothing<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>如果函数调用处于表达式之中，那么它的返回值讲参与表达式计算；如果函数只是被调用（比如，一行代码中只有一个MsgBox()函数）而已，那么它的返回值将被“扔掉”（Throw out）。</p>
<p>另外，调用UDF的时候，需要“引用”定义文件，语法是：<strong>#Include &lt;Filename.au3&gt;</strong>。之前我们已经见过具体的例子，比如想使用_ArrayDisplay这个函数的话，那么就得在程序开头加上<strong>#Include &lt;Array.au3&gt;</strong>。_ArrayDisplay的参数定义如下：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #F000FF; font-style: italic;">#Include</span> 
<span style="color: #0080FF; font-style: italic; font-weight: bold;">_ArrayDisplay</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #0000FF; font-weight: bold;">Const</span> <span style="color: #0000FF; font-weight: bold;">ByRef</span> <span style="font-weight: bold; color: #AA0000;">$avArray</span> <span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$sTitle</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Array: ListView Display&quot;</span> <span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$iItemLimit</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #FF0000; font-weight: bold;">-</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$iTranspose</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span> <span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$sSeparator</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span> <span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$sReplace</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;|&quot;</span> <span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$sHeader</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>以后当你需要做什么事情的时候，</p>
<ol>
<li>先去看看AutoIt帮助文档，看看有没有能够完成相应功能的函数</li>
<li>认真阅读该函数的帮助文档，以及其中的范例</li>
<li>将此函数把玩一阵，测试它的方方面面</li>
<li>将其应用到你的程序中去……</li>
<li>最后，如果你有兴趣的话，可以去阅读一下那些UDF的源码（都是公开的），学习它们的构建方法……</li>
</ol>
<p>学会看帮助文档，就好像是学会查词典一样，是最基础，却又最重要的技能。听起来很荒唐，但确实有很多人学了一辈子英语却不懂如何查词典，于是一辈子有解决不完的问题，而又不知道症结究竟在哪里；同样的道理，有很多人学了很久的编程，却从来没有认真研究过如何查看文档，于是最终与那些学英语却不会查词典的人同一个下场，并且从不自知。</p>
<p>Wikipedia上有个很好的条目，建议所有人都去认真阅读一下：<a href="http://en.wikipedia.org/wiki/Rtfm" target="_blank">http://en.wikipedia.org/wiki/Rtfm</a></p>
<hr />
<p><span>Footnotes:</span></p><ol class="footnotes"><li id="footnote_0_9248" class="footnote">以后你会知道，任何一种编程语言，真正强大的地方都在于它们各自的“函数库”（Library）……</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.lixiaolai.com/archives/9248.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>AutoIt教程：7. 文件读写</title>
		<link>http://www.lixiaolai.com/archives/9237.html</link>
		<comments>http://www.lixiaolai.com/archives/9237.html#comments</comments>
		<pubDate>Sun, 02 May 2010 02:00:45 +0000</pubDate>
		<dc:creator>xiaolai</dc:creator>
				<category><![CDATA[Auto-It]]></category>

		<guid isPermaLink="false">http://www.lixiaolai.com/?p=9237</guid>
		<description><![CDATA[用AutoIt可以很方便地读写文本文件。 新建一个AutoIt文件，将其命名为Testing16.au3，输入以下代码： ;Once I read in an article: ; ; Take a piece of paper, right now, and write out the word ATTITUDE. ; Now assign each letter the number that letter corresponds to and ; add those numbers up. Guess what, they add up to 100%. A=1, T=20, ; T=20, I=9, T=20, U=21, D=4, [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>用AutoIt可以很方便地读写文本文件。</p>
<p>新建一个AutoIt文件，将其命名为<strong>Testing16.au3</strong>，输入以下代码：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-style: italic; color: #009933;">;Once I read in an article:</span>
<span style="font-style: italic; color: #009933;">;</span>
<span style="font-style: italic; color: #009933;">; Take a piece of paper, right now, and write out the word ATTITUDE.</span>
<span style="font-style: italic; color: #009933;">; Now assign each letter the number that letter corresponds to and</span>
<span style="font-style: italic; color: #009933;">; add those numbers up. Guess what, they add up to 100%. A=1, T=20,</span>
<span style="font-style: italic; color: #009933;">; T=20, I=9, T=20, U=21, D=4, E=5 = 100%. Attitude is everything folks.</span>
<span style="font-style: italic; color: #009933;">; You can’t hide it; you can’t even deny it.;</span>
<span style="font-style: italic; color: #009933;">; It’s who you are. It’s the outside of your inside.</span>
<span style="font-style: italic; color: #009933;">;</span>
<span style="font-style: italic; color: #009933;">;I thought, Oh! Really?</span>
<span style="font-style: italic; color: #009933;">;Seconds later, I became suspicious of this claim...</span>
<span style="font-style: italic; color: #009933;">;OK, let's find out the truth, since we have tools to utilize...</span>
&nbsp;
<span style="color: #000080; font-style: italic; font-weight: bold;">InetGet</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;http://www.kilgarriff.co.uk/BNClists/lemma.al&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;lemma.al&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="font-weight: bold; color: #AA0000;">$file</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileOpen</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;lemma.al&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
	<span style="font-weight: bold; color: #AA0000;">$line</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileReadLine</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@error</span></span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #FF0000; font-weight: bold;">-</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">ExitLoop</span>
	<span style="font-weight: bold; color: #AA0000;">$lineElements</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringSplit</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$line</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot; &quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> AddUpCharactersInWordAsNumber<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$lineElements</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">3</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #FF0000; font-weight: bold;">==</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">100</span> <span style="color: #0000FF; font-weight: bold;">Then</span>
		<span style="color: #000080; font-style: italic; font-weight: bold;">ConsoleWrite</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$lineElements</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">3</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@CRLF</span></span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">EndIf</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">FileClose</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Finished!&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Func</span> AddUpCharactersInWordAsNumber<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$word</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$wordLowerCase</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringLower</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$word</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$Characters</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringSplit</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$wordLowerCase</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$count</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
	<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="font-weight: bold; color: #AA0000;">$Characters</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#93;</span>
		<span style="font-weight: bold; color: #AA0000;">$count</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$count</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="color: #000080; font-style: italic; font-weight: bold;">Asc</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$Characters</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="font-weight: bold; color: #AA0000;">$i</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #FF0000; font-weight: bold;">-</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">96</span>
	<span style="color: #0000FF; font-weight: bold;">Next</span>
	<span style="color: #0000FF; font-weight: bold;">Return</span> <span style="font-weight: bold; color: #AA0000;">$count</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span></pre></div></div>

<p>按F5查看一下运行结果。</p>
<p>再隔一篇文章，我才会详细解释这些代码──因为还有一些细节需要交代。</p>
<p>让我们先了解一下AutoIt是怎样进行文件读写的。请阅读一下以下代码：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$file</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileOpen</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;lemma.al&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
	<span style="font-weight: bold; color: #AA0000;">$line</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileReadLine</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@error</span></span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #FF0000; font-weight: bold;">-</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">ExitLoop</span>
	<span style="color: #FF0000; font-weight: bold;">...</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">FileClose</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>查看帮助文档的话，它告诉你说，FileOpen()这个函数返回一个File “Handle&#8221;（翻译成中文，是一个不知所云的词组“文件句柄”）。实际上，还不如干脆把这一行代码完整翻译成自然语言：</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$file</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileOpen</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;lemma.al&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>相当于:</p>
<blockquote><p>从此之后，我们就可以用变量$file指代那个以只读模式打开并保存于内存中的”lemma.al&#8221;文件。<sup>[<a href="http://www.lixiaolai.com/archives/9237.html#footnote_0_9237" id="identifier_0_9237" class="footnote-link footnote-identifier-link" title=" FileOpen()的第二个参数是用来指定文件读取模式的，0为读模式，1为写模式&hellip;&hellip;具体请参阅FileOpen()帮助文档。">1</a>]</sup> </p></blockquote>
</blockquote>
<p><strong>While</strong>后面的<strong>1</strong>是怎么回事儿？</p>
<p>帮助文件里，While&#8230;WEnd的示例是这么写的：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">While</span>
    statements
    <span style="color: #FF0000; font-weight: bold;">...</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span></pre></div></div>

<p><strong>While</strong>后面接一个<strong>表达式</strong>，即，“<em>当这个表达式为真之时，循环执行以下代码……</em>”</p>
<p>事实上，对AutoIt来说，<strong>1</strong>也是一个表达式，这个表达式的运算结果就是<em>1</em>。AutoIt从设计初始，就被定义为“弱类型”语言，即，对数据类型的定义不严格（至于这句话究竟是什么意思，以后的文章中会详细解释）。于是，在AutoIt中，<strong><em>1</em>和<em>True</em>是一回事</strong>（暂时别奇怪你为什么搞不清楚这句话的因果关系，反正记住<strong><em>1</em>和<em>True</em>是一回事</strong>就行了）──都可以当作表达式，且它们的值是相同的。</p>
<p>所以，</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
<span style="color: #FF0000; font-weight: bold;">...</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span></pre></div></div>

<p>是个无限循环。<strong>While 1 &#8230; WEnd</strong>之间的代码会无穷无尽地重复执行下去，除非……</p>
<p>请注意这行代码：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@error</span></span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #FF0000; font-weight: bold;">-</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">ExitLoop</span></pre></div></div>

<p>在循环内部，每次执行</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$line</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileReadLine</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>的时候，FileReadLine()都从$file里读出一行内容，并保存到变量$line之中（$line就成了一个字符串变量）……</p>
<p>FileReadLine()的帮助文档里写着：</p>
<blockquote><p><strong>Return Value</strong><br />
Success: Returns a line of text.<br />
Special: Sets @error to -1 if end-of-file is reached.</p></blockquote>
<p>也就是说，当读到文件结尾的时候，这个函数会把<strong>@error</strong>的值设置为<em>-1</em>。</p>
<p>这里有两个东西要解释：</p>
<ol>
<li><strong>end-of-file</strong>，又常常被缩写为<strong>EOF</strong>。它是所有文本文件的末尾的一个用记事本打开看不到的字符（文本文件中用记事本打开看不到的字符有很多，比如换行符Carriage Return Line Feed……）。当FileReadLine()读到这个字符之时，它就“知道”文件已经全部读取完毕了。</li>
<li><strong>@error</strong>，是AutoIt内建的一个“宏”，它的初始值是<strong>0</strong>。你不妨新建一个AutoIt文件，输入一行代码：
<ul>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #000080; font-style: italic; font-weight: bold;">ConsoleWrite</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@error</span></span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>看看@error的初始值到底是什么……</ul>
</li>
</ol>
<p>所以，以下代码：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$file</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileOpen</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;lemma.al&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
	<span style="font-weight: bold; color: #AA0000;">$line</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">FileReadLine</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #800000; font-weight: bold;"><span style="font-weight: bold; color: #AA0000;">@error</span></span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #FF0000; font-weight: bold;">-</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">ExitLoop</span>
	<span style="color: #000080; font-style: italic; font-weight: bold;">ConsoleWrite</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$line</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">FileClose</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$file</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>的意思是说：</p>
<ol>
<li>以“读模式”打开lemma.al文件；</li>
<li>逐行读取lemma.al文件内容；</li>
<li>将每一行内容输出至控制台；</li>
<li>关闭lemmal.al文件。</li>
</ol>
<blockquote><p><strong>作业</strong>：</p>
<ol>
<li><strong>Testing16.au3</strong>里用了好几个“陌生”的函数，请逐一去查阅相关文档。</li>
<li>尝试理解每一行代码，并分析整个程序的运行流程。</li>
</ol>
</blockquote>
<hr />
<p><span>Footnotes:</span></p><ol class="footnotes"><li id="footnote_0_9237" class="footnote"> FileOpen()的第二个参数是用来指定文件读取模式的，0为读模式，1为写模式……具体请参阅<strong>FileOpen()</strong>帮助文档。</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.lixiaolai.com/archives/9237.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>AutoIt教程：6. 数组</title>
		<link>http://www.lixiaolai.com/archives/9223.html</link>
		<comments>http://www.lixiaolai.com/archives/9223.html#comments</comments>
		<pubDate>Sat, 01 May 2010 14:07:47 +0000</pubDate>
		<dc:creator>xiaolai</dc:creator>
				<category><![CDATA[Auto-It]]></category>

		<guid isPermaLink="false">http://www.lixiaolai.com/?p=9223</guid>
		<description><![CDATA[变量（Variable）是用来保存一个数据的，数组（Array）是用来保存一系列数据的。比如，我们可以把一个班里所有人的名字保存到一个数组之中。事实上，你可以把数组理解为“最简单形式的数据库”，或者“只有一列数据的表单”。 Dim $name&#91;5&#93; $name&#91;0&#93; = &#34;Tom&#34; $name&#91;1&#93; = &#34;Jerry&#34; $name&#91;2&#93; = &#34;Mike&#34; $name&#91;3&#93; = &#34;Joey&#34; $name&#91;4&#93; = &#34;Jane&#34; 注意，方括号内的“下标”，是zero-based，即，从零开始。所以，在第1行代码中我们定义了一个总计有5个元素的数组，名称是$name，第一个元素用$name[0]表示，最后一个元素用$name[4]表示。数组中的每一个元素，相当于一个变量，可以保存一个数据。 用一个循环语句，就可以遍历（逐一访问，或读，或存）所有元素： For $i = 0 To 4 MsgBox&#40;0, $i, $name&#91;$i&#93;, 1&#41; Next 数组的操作向来很麻烦。不过，幸运的是，AutoIt安装的过程中提供了很多UDF，即所谓的“用户自定义函数库”。UDF，是User Defined Function的首字母缩写。AutoIt是免费开源的软件，所以有很多人参与了开发过程，同时写了许多各式各样的，能够完成特定功能的函数。一旦你需要使用数组，最方便的方法就是直接调用一些UDF中的函数。 引用UDF函数库的方法是在程序开头添上这样一句： #include &#60;Array.au3&#62; 之后，你就可以用_ArraySort()为某个数组排序，用_ArraySwap()为某两个元素调换位置，用_ArrayDisplay()显示一个数组内容，或者用_ArrayUnique()查询某个元素的值在整个数组中是否是唯一的…… 如果一个数组是“动态形成的”，或者，在程序运行过程中，数组增加或减少了一些元素，那么我们可以用UBound()函数得知某个特定数组的元素数量。 新建一个AutoIt文件，将其命名为Testing15.au3，输入以下代码，并按F5运行，查看结果： #include &#60;Array.au3&#62; &#160; Dim $name&#91;5&#93; $name&#91;0&#93; = &#34;Tom&#34; $name&#91;1&#93; = &#34;Jerry&#34; $name&#91;2&#93; = &#34;Mike&#34; $name&#91;3&#93; [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>变量（Variable）是用来保存<strong>一个</strong>数据的，数组（Array）是用来保存<strong>一系列</strong>数据的。比如，我们可以把一个班里所有人的名字保存到一个数组之中。事实上，你可以把数组理解为“最简单形式的数据库”，或者“只有一列数据的表单”。</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Dim</span> <span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">5</span><span style="color: #FF0000; font-weight: bold;">&#93;</span>
<span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Tom&quot;</span>
<span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Jerry&quot;</span>
<span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">2</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Mike&quot;</span>
<span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">3</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Joey&quot;</span>
<span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">4</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Jane&quot;</span></pre></div></div>

<p>注意，方括号内的“下标”，是zero-based，即，从零开始。所以，在第1行代码中我们定义了一个总计有5个元素的数组，名称是<strong>$name</strong>，第一个元素用<strong>$name[0]</strong>表示，最后一个元素用<strong>$name[4]</strong>表示。数组中的每一个元素，相当于一个变量，可以保存<strong>一个</strong>数据。</p>
<p>用一个循环语句，就可以<strong>遍历</strong>（逐一访问，或读，或存）所有元素：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">4</span>
    <span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$i</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="font-weight: bold; color: #AA0000;">$i</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">Next</span></pre></div></div>

<p>数组的操作向来很麻烦。不过，幸运的是，AutoIt安装的过程中提供了很多UDF，即所谓的“用户自定义函数库”。UDF，是User Defined Function的首字母缩写。AutoIt是免费开源的软件，所以有很多人参与了开发过程，同时写了许多各式各样的，能够完成特定功能的函数。一旦你需要使用数组，最方便的方法就是直接调用一些UDF中的函数。</p>
<p>引用UDF函数库的方法是在程序开头添上这样一句：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #F000FF; font-style: italic;">#include</span><span style="font-style: italic; color: #9977BB;"> &lt;Array.au3&gt;</span></pre></div></div>

<p>之后，你就可以用<strong>_ArraySort()</strong>为某个数组排序，用<strong>_ArraySwap()</strong>为某两个元素调换位置，用<strong>_ArrayDisplay()</strong>显示一个数组内容，或者用<strong>_ArrayUnique()</strong>查询某个元素的值在整个数组中是否是唯一的……</p>
<div id="attachment_9227" class="wp-caption aligncenter" style="width: 203px">
	<img src="http://www.lixiaolai.com/wp-content/uploads/2010/05/UDFArray.png" alt="" title="UDFArray" width="203" height="429" class="size-full wp-image-9227" />
	<p class="wp-caption-text">UDF中关于数组管理的所有函数……</p>
</div>
<p>如果一个数组是“动态形成的”，或者，在程序运行过程中，数组增加或减少了一些元素，那么我们可以用<strong>UBound()</strong>函数得知某个特定数组的元素数量。</p>
<p>新建一个AutoIt文件，将其命名为Testing15.au3，输入以下代码，并按F5运行，查看结果：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #F000FF; font-style: italic;">#include</span><span style="font-style: italic; color: #9977BB;"> &lt;Array.au3&gt;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Dim</span> <span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">5</span><span style="color: #FF0000; font-weight: bold;">&#93;</span>
<span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Tom&quot;</span>
<span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Jerry&quot;</span>
<span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">2</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Mike&quot;</span>
<span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">3</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Joey&quot;</span>
<span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">4</span><span style="color: #FF0000; font-weight: bold;">&#93;</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #9977BB;">&quot;Jane&quot;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">4</span>
    <span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$i</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#91;</span><span style="font-weight: bold; color: #AA0000;">$i</span><span style="color: #FF0000; font-weight: bold;">&#93;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">Next</span>
&nbsp;
<span style="color: #0080FF; font-style: italic; font-weight: bold;">_ArrayDisplay</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #0080FF; font-style: italic; font-weight: bold;">_ArrayAdd</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Mary&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #0080FF; font-style: italic; font-weight: bold;">_ArrayDisplay</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Info&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Now the array $name has &quot;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="color: #000080; font-style: italic; font-weight: bold;">UBound</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="font-weight: bold; color: #9977BB;">&quot; elements...&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #0080FF; font-style: italic; font-weight: bold;">_ArraySort</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #0080FF; font-style: italic; font-weight: bold;">_ArrayDisplay</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #0080FF; font-style: italic; font-weight: bold;">_ArrayReverse</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #0080FF; font-style: italic; font-weight: bold;">_ArrayDisplay</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$name</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>注意：当<strong>_ArrayDisplay()</strong>显示对话框的时候，程序会暂停，知道你按关闭按钮关闭该对话框之后，程序将继续运行。</p>
<p><img src="http://www.lixiaolai.com/wp-content/uploads/2010/05/Ch06-02.png" alt="" title="Ch06-02" width="250" height="196" class="aligncenter size-full wp-image-9228" /></p>
<blockquote><p><strong>作业</strong>：</p>
<p>把所有UDF中的关于数组管理的函数都拿出来调用、试验一遍。与此同时，还要“硬着头皮”精读每个函数的帮助文档……</p>
<p>提示：调用每个函数之后，都可以调用<strong>_ArrayDisplay()</strong>来查看数组的内容变化……</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.lixiaolai.com/archives/9223.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>AutoIt教程：5. 函数</title>
		<link>http://www.lixiaolai.com/archives/9194.html</link>
		<comments>http://www.lixiaolai.com/archives/9194.html#comments</comments>
		<pubDate>Fri, 30 Apr 2010 08:02:33 +0000</pubDate>
		<dc:creator>xiaolai</dc:creator>
				<category><![CDATA[Auto-It]]></category>

		<guid isPermaLink="false">http://www.lixiaolai.com/?p=9194</guid>
		<description><![CDATA[新的知识必然与过往的任何知识都不同，否则它就不“新”了。“类比思考”可以帮我们运用已有的知识对比着了解新的知识，然而很多的时候，要更多甚至全然关注于“不同”之处才行。另外，如果遇到的是“仅仅看起来像已知的某样东西而已，可实际上却全然不同的”新知识，那么“类比思考”就不适用了。而思考之时，“类比”无所不在，于是，控制它需要一些挣扎…… 编程语言中的函数（Function）和数学里的函数（你肯定学过一些的）压根就不是一回事儿，它们只是“恰巧”名字相同而已。（这就好像“table”这个概念可以指两个完全没关系的概念一样──“桌子”和“表格”。看，我们又是用类比进行理解……只不过，这次没什么问题。） 在程序里，所谓函数，本质上来看就是一段有特定功能的代码。一个函数由以下几个部分构成： 函数名 参数 返回值 定义 调用 在这里停上三五分钟，把这五个要素牢牢记住──靠死记硬背，在你还不知道它们究竟指什么之前，就先把这几个名称牢牢记住。要做到有人问你“函数构成有几个部分？”的时候，你脱口而出这五个名称……然后再继续阅读下去。 新建一个AutoIt文件，将其命名为Testing12.au3，输入以下代码： Func __AddUpTo&#40;$n&#41; $r = 0 For $i = 1 To $n $r = $r + $i Next Return $r EndFunc 按F5执行，这段代码不输出任何结果。 这段代码只是定义了一个函数，定义函数的语法是：Func&#8230;EndFunc： 它的函数名为__AddUpTo() 调用这个函数时，需要提供一个参数 这个函数的返回值是（1+2+3&#8230;+n）的值 再补上两行，使其变成这个样子： Func __AddUpTo&#40;$n&#41; $r = 0 For $i = 1 To $n $r = $r + $i Next Return $r [...]]]></description>
			<content:encoded><![CDATA[<p></p><blockquote><p>新的知识必然与过往的任何知识都不同，否则它就不“新”了。“类比思考”可以帮我们运用已有的知识对比着了解新的知识，然而很多的时候，要更多甚至全然关注于“不同”之处才行。另外，如果遇到的是“仅仅看起来像已知的某样东西而已，可实际上却全然不同的”新知识，那么“类比思考”就不适用了。而思考之时，“类比”无所不在，于是，控制它需要一些挣扎……</p></blockquote>
<p>编程语言中的函数（Function）和数学里的函数（你肯定学过一些的）压根就不是一回事儿，它们只是“恰巧”名字相同而已。（这就好像“table”这个概念可以指两个完全没关系的概念一样──“桌子”和“表格”。看，我们又是用类比进行理解……只不过，这次没什么问题。）</p>
<p>在程序里，所谓函数，本质上来看就是一段有特定功能的代码。一个函数由以下几个部分构成：</p>
<ul>
<li>函数名</li>
<li>参数</li>
<li>返回值</li>
<li>定义</li>
<li>调用</li>
</ul>
<p>在这里停上三五分钟，把这五个要素牢牢记住──靠死记硬背，在你还不知道它们究竟指什么之前，就先把这几个名称牢牢记住。要做到有人问你“函数构成有几个部分？”的时候，你脱口而出这五个名称……然后再继续阅读下去。</p>
<p>新建一个AutoIt文件，将其命名为<strong>Testing12.au3</strong>，输入以下代码：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Func</span> __AddUpTo<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$n</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
	<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="font-weight: bold; color: #AA0000;">$n</span>
		<span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="font-weight: bold; color: #AA0000;">$i</span>
	<span style="color: #0000FF; font-weight: bold;">Next</span>
	<span style="color: #0000FF; font-weight: bold;">Return</span> <span style="font-weight: bold; color: #AA0000;">$r</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span></pre></div></div>

<p>按F5执行，这段代码不输出任何结果。</p>
<p>这段代码只是定义了一个函数，定义函数的语法是：<strong>Func&#8230;EndFunc</strong>：</p>
<ul>
<li>它的<strong>函数名</strong>为<strong>__AddUpTo()</strong></li>
<li><strong>调用</strong>这个函数时，需要提供一个<strong>参数</strong></li>
<li>这个函数的<strong>返回值</strong>是（1+2+3&#8230;+n）的值</li>
</ul>
<p>再补上两行，使其变成这个样子：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Func</span> __AddUpTo<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$n</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
	<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="font-weight: bold; color: #AA0000;">$n</span>
		<span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="font-weight: bold; color: #AA0000;">$i</span>
	<span style="color: #0000FF; font-weight: bold;">Next</span>
	<span style="color: #0000FF; font-weight: bold;">Return</span> <span style="font-weight: bold; color: #AA0000;">$r</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span>
<span style="font-weight: bold; color: #AA0000;">$result</span> <span style="color: #FF0000; font-weight: bold;">=</span> __AddUpTo<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">100</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Result&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$result</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>按F5查看结果：</p>
<p><img class="aligncenter size-full wp-image-9196" title="ch05-01" src="http://www.lixiaolai.com/wp-content/uploads/2010/04/ch05-01.png" alt="" width="104" height="100" /></p>
<p>当计算机阅读你的指示（即，你写的程序）之时，读到第一行，它看到<strong>Func</strong>，就知道你是在<strong>定义</strong>一个<strong>函数</strong>，它“理解”了，然后找到<strong>EndFun</strong>，看看后面有没有“可执行”的代码……</p>
<p>而后，它在第8行，读到了一个函数“<strong>调用</strong>”，即，你向<strong>__AddUpTo()</strong>这个函数<strong>传递</strong>了一个<strong>参数</strong>（<em>100</em>）。</p>
<p>它在当前文件中去寻找有没有这个函数的<strong>定义</strong>（当然有，在第1行定义的……），找到之后，开始执行__AddUpTo()这个函数区块中（Func &#8230; EndFunc）的内容……</p>
<p>此时，<strong>$n = 100</strong>，因为你刚刚在调用<strong>__AddUpTo()</strong>的时候，在括号里写了<em>100</em>，所以，在<strong>Func __AddUpTo($n)</strong>开始执行的时候，<strong>$n</strong> 被赋值为 <em>100</em>。</p>
<p>而后内部的运算，你都看得懂，最终的结果是变量<strong>$r</strong>的值为<em>5050</em>（1+2+3&#8230;+100)</p>
<p>然后，函数<strong>__AddUpTo()</strong>把根据传递进来的参数(<em>100</em>)计算出来的结果(<em>5050</em>)返回给第8行的代码，“<strong>$result = __AddUpTo(100)</strong>”，即，从此之后，变量<strong>$result</strong>的值就是<em>5050</em>。</p>
<p>再然后，第9行代码呼出一个对话框，显示结果……</p>
<p>现在，新建一个AutoIt文件，将其命名为<strong>Testing13.au3</strong>，输入以下代码：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Func</span> __AddFromUpTo<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$a</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$b</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
	<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$a</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="font-weight: bold; color: #AA0000;">$b</span>
		<span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="font-weight: bold; color: #AA0000;">$i</span>
	<span style="color: #0000FF; font-weight: bold;">Next</span>
	<span style="color: #0000FF; font-weight: bold;">Return</span> <span style="font-weight: bold; color: #AA0000;">$r</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span>
&nbsp;
<span style="font-weight: bold; color: #AA0000;">$result</span> <span style="color: #FF0000; font-weight: bold;">=</span> __AddFromUpTo<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">101</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">200</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Result&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$result</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>这次我们定义的函数名称叫<strong>__AddFromUpTo()</strong>，它需要两个<strong>参数</strong>，<strong>返回值</strong>是从第一个参数加到第二个参数究竟等于多少：</p>
<p><img class="aligncenter size-full wp-image-9201" title="ch05-02" src="http://www.lixiaolai.com/wp-content/uploads/2010/04/ch05-02.png" alt="" width="104" height="100" /></p>
<p>现在，尝试一下，把<strong>Testing13.au3</strong>第9行的代码改为：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$result</span> <span style="color: #FF0000; font-weight: bold;">=</span> __AddFromUpTo<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">200</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>而后按F5执行，结果是跳出一个“出错提示”对话框：</p>
<p><a href="http://www.lixiaolai.com/wp-content/uploads/2010/04/ch05-03.png"><img class="aligncenter size-full wp-image-9203" title="ch05-03-420" src="http://www.lixiaolai.com/wp-content/uploads/2010/04/ch05-03-420.png" alt="" width="420" height="188" /></a></p>
<p>这是因为当初定义<strong>__AddFromUpTo()</strong>就指明<strong>调用</strong>它的时候需要提供两个<strong>参数</strong>，而现在却只提供了一个<strong>参数</strong>，所以，计算机“糊涂”了，弄不明白你究竟想干什么……</p>
<p>新建一个AutoIt文件，将其命名为<strong>Testing14.au3</strong>，输入以下代码（不要按F5查看结果，而是先尝试着用脑演练一番，看看自己能不能想出正确结果……）：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Func</span> __AddFromUpTo<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$a</span><span style="color: #FF0000; font-weight: bold;">=</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$b</span><span style="color: #FF0000; font-weight: bold;">=</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">100</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
	<span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
	<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$a</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="font-weight: bold; color: #AA0000;">$b</span>
		<span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="font-weight: bold; color: #AA0000;">$i</span>
	<span style="color: #0000FF; font-weight: bold;">Next</span>
	<span style="color: #0000FF; font-weight: bold;">Return</span> <span style="font-weight: bold; color: #AA0000;">$r</span>
<span style="color: #0000FF; font-weight: bold;">EndFunc</span>
&nbsp;
<span style="font-weight: bold; color: #AA0000;">$result</span> <span style="color: #FF0000; font-weight: bold;">=</span> __AddFromUpTo<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Result&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$result</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="font-weight: bold; color: #AA0000;">$result</span> <span style="color: #FF0000; font-weight: bold;">=</span> __AddFromUpTo<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">50</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Result&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$result</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="font-weight: bold; color: #AA0000;">$result</span> <span style="color: #FF0000; font-weight: bold;">=</span> __AddFromUpTo<span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">101</span><span style="color: #FF0000; font-weight: bold;">,</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">200</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Result&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #AA0000;">$result</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>这一次，我们在定义<strong>__AddFromUpTo()</strong>之时，为它的两个参数都提供了“默认值”，第一个参数默认是<em>1</em>，第二个参数默认是<em>2</em>。即，如果在调用这个函数的时候并未向其传递参数的话，那么就使用默认值进行运算（并返回相应的结果）。所以，</p>
<ul>
<li>调用<strong>__AddFromUpTo()</strong>，相当于没有提供任何参数，所以它按默认值运算，返回的应该是（<em>5050</em>）；</li>
<li>调用<strong>__AddFromUpTo(50)</strong>，相当于向<strong>__AddFromUpTo()</strong>函数提供了第一个参数（<em>50</em>），而第二个参数由于未被提供，所以采用默认值（<em>100</em>），于是，这个函数调用所返回的应该是<em>3825</em>（50+51+52&#8230;100）；</li>
<li>调用<strong>__AddFromUpTo(101,200)</strong>，向该函数提供了它所需要的全部两个参数，所以，该函数内部的运算不使用任何默认值，而是计算从101加到200的结果，并返回（<em>15050</em>）。</li>
</ul>
<p>现在按F5查看一下执行结果：</p>
<p><img src="http://www.lixiaolai.com/wp-content/uploads/2010/04/ch05-04.png" alt="" title="ch05-04" width="104" height="100" class="aligncenter size-full wp-image-9205" /></p>
<p><img src="http://www.lixiaolai.com/wp-content/uploads/2010/04/ch05-05.png" alt="" title="ch05-05" width="104" height="100" class="aligncenter size-full wp-image-9206" /></p>
<p><img src="http://www.lixiaolai.com/wp-content/uploads/2010/04/ch05-06.png" alt="" title="ch05-06" width="104" height="100" class="aligncenter size-full wp-image-9207" /></p>
<p>现在，再回顾一下我们刚刚遇到的一些概念：</p>
<ul>
<li>函数名</li>
<li>参数、默认参数</li>
<li>返回值</li>
<li>定义</li>
<li>调用</li>
</ul>
<blockquote><p><strong>作业</strong>：</p>
<p>重新去反复阅读若干遍ConsoleWrite()、InputBox()和MsgBox()这三个函数的帮助文档。</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.lixiaolai.com/archives/9194.html/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>AutoIt教程：4. 输入与输出、判断与分支</title>
		<link>http://www.lixiaolai.com/archives/9181.html</link>
		<comments>http://www.lixiaolai.com/archives/9181.html#comments</comments>
		<pubDate>Fri, 30 Apr 2010 07:46:46 +0000</pubDate>
		<dc:creator>xiaolai</dc:creator>
				<category><![CDATA[Auto-It]]></category>

		<guid isPermaLink="false">http://www.lixiaolai.com/?p=9181</guid>
		<description><![CDATA[学什么东西，往往并不需要脑袋聪明。可是需要“头皮很硬”──因为你总是需要“硬着头皮读/做下去……” 新建一个AutoIt文件，将其命名为Testing9.au3，输入以下代码： $n = InputBox&#40;&#34;Input&#34;, &#34;To what number do you want to add up?&#34;&#41; $r = 0 For $i = 1 To $n $r = $r + $i Next MsgBox&#40;0, &#34;Output&#34;, &#34;The sum from 1 to &#34; &#38; $n &#38; &#34; is &#34; &#38; $r&#41; 请按F5查看执行结果。 先是跳出一个含有输入框的对话框，要求你输入一个数字，比如，1024： 而后跳出一个对话框，显示计算结果： Testing9.au3看起来像是个真正的程序了，因为它包含了所谓程序的三个重要组成部分： 输入 处理 输出 所谓程序，就是一系列的操作说明──是你（作为程序的作者）用计算机能够理解的语言编写[1] 的。而后计算机去执行这些操作。在操作过程中，有可能需要你（作为程序的用户）提供一些数据（输入）以便进行进一步的操作（操作），也有可能为了满足你（作为程序的用户）的需求而向你提供一定的反馈（输出）。 将来无论你去学哪一种新的编程语言，记住：第一件事情是去搞清楚它的“输入/输出”的方式，因为你作为程序作者也好，作为程序用户也罢，必须与计算机“互动”。在此之前，我们一致“硬着头皮”用ConsoleWrite()这个函数，现在我们又有了一个看起来比较“现代”的输出方式──MsgBox()…… [...]]]></description>
			<content:encoded><![CDATA[<p></p><blockquote><p>学什么东西，往往并不需要脑袋聪明。可是需要“头皮很硬”──因为你总是需要“硬着头皮读/做下去……”</p></blockquote>
<p>新建一个AutoIt文件，将其命名为<strong>Testing9.au3</strong>，输入以下代码：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$n</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">InputBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;Input&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;To what number do you want to add up?&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="font-weight: bold; color: #AA0000;">$n</span>
     <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="font-weight: bold; color: #AA0000;">$i</span>
<span style="color: #0000FF; font-weight: bold;">Next</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Output&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;The sum from 1 to &quot;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="font-weight: bold; color: #AA0000;">$n</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="font-weight: bold; color: #9977BB;">&quot; is &quot;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="font-weight: bold; color: #AA0000;">$r</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>请按F5查看执行结果。</p>
<p>先是跳出一个含有输入框的对话框，要求你输入一个数字，比如，<em>1024</em>：</p>
<p><img src="http://www.lixiaolai.com/wp-content/uploads/2010/04/ch04-01.png" alt="" title="ch04-01" width="251" height="181" class="aligncenter size-full wp-image-9183" /></p>
<p>而后跳出一个对话框，显示计算结果：</p>
<p><img src="http://www.lixiaolai.com/wp-content/uploads/2010/04/ch04-02.png" alt="" title="ch04-02" width="191" height="100" class="aligncenter size-full wp-image-9184" /></p>
<p><strong>Testing9.au3</strong>看起来像是个真正的程序了，因为它包含了所谓程序的三个重要组成部分：</p>
<ul>
<li>输入</li>
<li>处理</li>
<li>输出</li>
</ul>
<p>所谓程序，就是一系列的操作说明──是你（作为程序的作者）用计算机能够理解的语言编写<sup>[<a href="http://www.lixiaolai.com/archives/9181.html#footnote_0_9181" id="identifier_0_9181" class="footnote-link footnote-identifier-link" title="在这里，当然是AutoIt v3。在你的计算机安装AutoIt v3之前，你的计算机是不可能理解.au3文件中的内容的&hellip;&hellip;">1</a>]</sup> 的。而后计算机去执行这些操作。在操作过程中，有可能需要你（作为程序的用户）提供一些数据（<strong>输入</strong>）以便进行进一步的操作（<strong>操作</strong>），也有可能为了满足你（作为程序的用户）的需求而向你提供一定的反馈（<strong>输出</strong>）。</p>
<p>将来无论你去学哪一种新的编程语言，记住：<strong>第一件事情是去搞清楚它的“输入/输出”的方式</strong>，因为你作为程序作者也好，作为程序用户也罢，必须与计算机“互动”。在此之前，我们一致“硬着头皮”用<strong>ConsoleWrite()</strong>这个函数，现在我们又有了一个看起来比较“现代”的输出方式──<strong>MsgBox()</strong>……</p>
<p><strong>Input()</strong>和<strong>MsgBox()</strong>都是函数（一个你到目前为止还必须“不懂装懂”的概念）。你可以打开AutoIt帮助文件，去看看这两个函数的文档页面，看不懂没关系，但多看几遍，在没有任何人为你讲解的情况下，你都会渐渐明白更多。现在要知道的是这些（<strong>请控制一下自己的好奇心</strong>，暂时只要知道下面这些就够了，以后有的是机会回来弄透──也必须回来弄透……）：</p>
<blockquote><p>在第1行，等号右边的<strong>Input()</strong>这个函数，相当于一个表达式，它的结果就是你在输入框里所输入的内容；于是，这一行代码执行过后，你输入的是什么，变量$n的值就是什么。Input()里面有两个字符串，第一个是用来定义显示在对话框标题的（在这个程序中，我们将其定制为<strong>&#8220;Input&#8221;</strong>；当然，你可以随便改改试试看）；第二个是用来定义对话框面板上的内容的（在这个程序里，<strong>&#8220;To what number do you want to add up to?&#8221;</strong>）。</p>
<p>在第6行，<strong>MsgBox()</strong>这个函数的执行结果是跳出一个对话框；括号里的第一个参数是“<strong>0</strong>”，它定义的是“对话框类型”，第二个参数<strong>&#8220;Output&#8221;</strong>，定义的是对话框的标题；而第三个参数，是一个表达式，把以下几个字符串合并起来，显示在对话框面板中：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #9977BB;">&quot;The sum from 1 to &quot;</span>
<span style="font-weight: bold; color: #AA0000;">$n</span>
<span style="font-weight: bold; color: #9977BB;">&quot; is &quot;</span>
<span style="font-weight: bold; color: #AA0000;">$r</span></pre></div></div>

</blockquote>
<p>现在的问题在于，万一这个程序的用户不是你自己，他在输入框里输入的不是正整数怎么办？比如输入的是“ten&#8221;，或者，“-10”……你自己不妨反复试试看这些“非常规输入”。</p>
<p><img src="http://www.lixiaolai.com/wp-content/uploads/2010/04/ch04-03.png" alt="" title="ch04-03" width="153" height="100" class="aligncenter size-full wp-image-9187" /></p>
<p><img src="http://www.lixiaolai.com/wp-content/uploads/2010/04/ch04-04.png" alt="" title="ch04-04" width="153" height="100" class="aligncenter size-full wp-image-9188" /></p>
<p>至于最终结果为什么是“0”而不是别的什么，可以暂时不去理会──反正结果不是我们所预期的。</p>
<p>现在的问题是，作为程序作者的你，如何确保程序用户输入的是正确的数字呢？</p>
<p>让我们在第一行代码之后补上几行（<strong>Testing10.au3</strong>）：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$n</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">InputBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;Input&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;To what number do you want to add up to?&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #0000FF; font-weight: bold;">Not</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringIsDigit</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$n</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span>
     <span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Warning!&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;You didn't input a valid number. Press OK to quit...&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
     <span style="color: #0000FF; font-weight: bold;">Exit</span>
<span style="color: #0000FF; font-weight: bold;">EndIf</span>
<span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="font-weight: bold; color: #AA0000;">$n</span>
     <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="font-weight: bold; color: #AA0000;">$i</span>
<span style="color: #0000FF; font-weight: bold;">Next</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Output&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;The sum from 1 to &quot;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="font-weight: bold; color: #AA0000;">$n</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="font-weight: bold; color: #9977BB;">&quot; is &quot;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="font-weight: bold; color: #AA0000;">$r</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>第2行里的<strong>StringIsDigit()</strong>又是一个函数。如果作为参数传递给它的字符串全部由数字构成，那么这个函数将返回“True”这个逻辑值；否则的话，它就返回“False”这个逻辑值──暂时了解这些就够了，请你硬着头皮接着往下看……</p>
<p>第2行到第5行代码，是一个<strong>If&#8230;Then</strong>的“判断分支”语句，将其翻译成自然语言就是</p>
<blockquote><p>如果<strong>$n</strong>不是数字的话，就显示对话框，提示用户“You didn&#8217;t input a valid number. Press OK to quit&#8230;”，而后退出程序</p>
<p>否则的话（即，如果<strong>$n</strong>是数字的话），就跳过<strong>Then&#8230;EndIf</strong>之间的代码，继续执行<strong>EndIf</strong>之后的代码……</p></blockquote>
<p>可是，这样还是不令你满意，因为，你要的实际上是“如果用户输入的不是数字，那么就让他重新输入，直到用户输入正确数字，则为用户显示计算结果……”</p>
<p>注意这里的“<strong>直到</strong>……”，用编程语言表达的话，就是“用一个循环，直到某个条件满足跳出循环”。请阅读以下的代码（<strong>Testing11.au3</strong>）：</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Do</span>
     <span style="font-weight: bold; color: #AA0000;">$n</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #000080; font-style: italic; font-weight: bold;">InputBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #9977BB;">&quot;Input&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;To what number do you want to add up to?&quot;</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
     <span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #0000FF; font-weight: bold;">Not</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringIsDigit</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$n</span><span style="color: #FF0000; font-weight: bold;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Warning!&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;You didn't input a valid number. Please try again...&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">Until</span> <span style="color: #000080; font-style: italic; font-weight: bold;">StringIsDigit</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$n</span><span style="color: #FF0000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="font-weight: bold; color: #AA0000;">$n</span>
     <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="font-weight: bold; color: #AA0000;">$i</span>
<span style="color: #0000FF; font-weight: bold;">Next</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">MsgBox</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;Output&quot;</span><span style="color: #FF0000; font-weight: bold;">,</span> <span style="font-weight: bold; color: #9977BB;">&quot;The sum from 1 to &quot;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="font-weight: bold; color: #AA0000;">$n</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="font-weight: bold; color: #9977BB;">&quot; is &quot;</span> <span style="color: #FF0000; font-weight: bold;">&amp;</span> <span style="font-weight: bold; color: #AA0000;">$r</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>至此为止，你已经看到了“循环”（Loop）和“分支”（Branch）的功用。许多年之后的今天，我还清楚地记得当年我学会这两个“简单”概念之时的震惊：本质上来看，所有程序的最重要部分就是这两个概念构成的──以后你会渐渐明白，绝大部分的计算/运算，最终是为了“分支”和“循环”服务而已；也就是说，用“循环”和“分支”可以做任何事情……从逻辑上把所有任务抽象为这么两个简单概念，真是美丽到了极致。</p>
<p>照例，我们又暂时忽略了许多重要的细节──我知到你的“强迫症”快要发作了──不过，我们还要硬着头皮往前走很久才行……</p>
<hr />
<p><span>Footnotes:</span></p><ol class="footnotes"><li id="footnote_0_9181" class="footnote">在这里，当然是AutoIt v3。在你的计算机安装AutoIt v3之前，你的计算机是不可能理解<em>.au3</em>文件中的内容的……</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.lixiaolai.com/archives/9181.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>AutoIt教程：3. 循环</title>
		<link>http://www.lixiaolai.com/archives/9171.html</link>
		<comments>http://www.lixiaolai.com/archives/9171.html#comments</comments>
		<pubDate>Thu, 29 Apr 2010 23:00:20 +0000</pubDate>
		<dc:creator>xiaolai</dc:creator>
				<category><![CDATA[Auto-It]]></category>

		<guid isPermaLink="false">http://www.lixiaolai.com/?p=9171</guid>
		<description><![CDATA[尽管我从不相信速成，但也知道有些事情必须速战速决。事实上，我一直在忽略一些（其实非常重要的）细节，带着读者向前向前向前……这并不是想要囫囵吞枣，而是为了尽快有个“概览”，而后再重新来过。反刍，也是学习的诀窍之一。 计算机的另外一个长项是“不厌其烦”，无论你让它做什么，只要 你要求的是它确实能做的（比如它不大可能每天定时煎两个荷包蛋给你）； 你说得足够清楚（程序正确无误）； 那么它就会百分百照做，并且绝不怕重复——你让它做多少遍它就做多少遍。 我们小时候都计算过1+2+3&#8230;+10等于55；并且那时候我们还学过所谓的“优化算法”， 1+10=11 2+9=11 3+8=11 4+7=11 5+6=11 所以，只需要算5&#215;11等于多少就可以了。 有了编程能力之后，你可以无需任何“优化算法”[1] ，迅速得知1+2+3&#8230;+n等于多少，无论n是多少…… 现在，请分别建立以下三个文件，输入相应的代码，分别执行，查看结果。 Testing6.au3 $r = 0 For $i = 1 To 256 $r = $r + $i Next ConsoleWrite&#40;$r&#41; Testing7.au3 $r = 0 $i = 1 While $i &#60;= 256 $r = $r + $i $i = $i + 1 WEnd ConsoleWrite&#40;$r&#41; [...]]]></description>
			<content:encoded><![CDATA[<p></p><blockquote><p>尽管我从不相信速成，但也知道有些事情必须速战速决。事实上，我一直在忽略一些（其实非常重要的）细节，带着读者向前向前向前……这并不是想要囫囵吞枣，而是为了尽快有个“概览”，而后再重新来过。反刍，也是学习的诀窍之一。</p></blockquote>
<p>计算机的另外一个长项是“不厌其烦”，无论你让它做什么，只要</p>
<ul>
<li> 你要求的是它确实能做的（比如它不大可能每天定时煎两个荷包蛋给你）；</li>
<li> 你说得足够清楚（程序正确无误）；</li>
</ul>
<p>那么它就会百分百照做，并且绝不怕重复——你让它做多少遍它就做多少遍。</p>
<p>我们小时候都计算过<em>1+2+3&#8230;+10</em>等于<em>55</em>；并且那时候我们还学过所谓的“优化算法”，</p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span><span style="color: #FF0000; font-weight: bold;">+</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">10</span><span style="color: #FF0000; font-weight: bold;">=</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">11</span>
<span style="color: #AC00A9; font-style: italic; font-weight: bold;">2</span><span style="color: #FF0000; font-weight: bold;">+</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">9</span><span style="color: #FF0000; font-weight: bold;">=</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">11</span>
<span style="color: #AC00A9; font-style: italic; font-weight: bold;">3</span><span style="color: #FF0000; font-weight: bold;">+</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">8</span><span style="color: #FF0000; font-weight: bold;">=</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">11</span>
<span style="color: #AC00A9; font-style: italic; font-weight: bold;">4</span><span style="color: #FF0000; font-weight: bold;">+</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">7</span><span style="color: #FF0000; font-weight: bold;">=</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">11</span>
<span style="color: #AC00A9; font-style: italic; font-weight: bold;">5</span><span style="color: #FF0000; font-weight: bold;">+</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">6</span><span style="color: #FF0000; font-weight: bold;">=</span><span style="color: #AC00A9; font-style: italic; font-weight: bold;">11</span></pre></div></div>

<p>所以，只需要算<em>5&#215;11</em>等于多少就可以了。</p>
<p>有了编程能力之后，你可以无需任何“优化算法”<sup>[<a href="http://www.lixiaolai.com/archives/9171.html#footnote_0_9171" id="identifier_0_9171" class="footnote-link footnote-identifier-link" title="这个例子中所需要的计算太简单，所以，&ldquo;算法&rdquo;上的优化没有太大作用。而在大型的软件工程中，算法优化非常重要，效果也异常惊人。不过，大多数普通用户的&ldquo;任务&rdquo;，往往用&ldquo;笨方法&rdquo;更划算&hellip;&hellip;">1</a>]</sup> ，迅速得知<em>1+2+3&#8230;+n</em>等于多少，无论<em>n</em>是多少……</p>
<p>现在，请分别建立以下三个文件，输入相应的代码，分别执行，查看结果。</p>
<p><strong>Testing6.au3</strong></p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
<span style="color: #0000FF; font-weight: bold;">For</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span> <span style="color: #0000FF; font-weight: bold;">To</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">256</span>
     <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="font-weight: bold; color: #AA0000;">$i</span>
<span style="color: #0000FF; font-weight: bold;">Next</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">ConsoleWrite</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$r</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p><strong>Testing7.au3</strong></p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
<span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
<span style="color: #0000FF; font-weight: bold;">While</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">&lt;=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">256</span>
     <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="font-weight: bold; color: #AA0000;">$i</span>
     <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
<span style="color: #0000FF; font-weight: bold;">WEnd</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">ConsoleWrite</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$r</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p><strong>Testing8.au3</strong></p>

<div class="wp_syntax"><div class="code"><pre class="autoit" style="font-family:monospace;"><span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">0</span>
<span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
<span style="color: #0000FF; font-weight: bold;">Do</span>
    <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$r</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="font-weight: bold; color: #AA0000;">$i</span>
    <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">=</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">+</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">1</span>
<span style="color: #0000FF; font-weight: bold;">Until</span> <span style="font-weight: bold; color: #AA0000;">$i</span> <span style="color: #FF0000; font-weight: bold;">&gt;</span> <span style="color: #AC00A9; font-style: italic; font-weight: bold;">256</span>
<span style="color: #000080; font-style: italic; font-weight: bold;">ConsoleWrite</span><span style="color: #FF0000; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #AA0000;">$r</span><span style="color: #FF0000; font-weight: bold;">&#41;</span></pre></div></div>

<p>这三个程序的输出结果都是一样的，它们都在计算<strong>从<em>1</em>加到<em>256</em>的结果</strong>是多少：<em>32896</em>（=1+2+3&#8230;+256）。</p>
<p>先让我们看看Testing6.au3的代码。</p>
<ul>
<li>第1行，变量<strong>$r</strong>被设置了一个“初始值”，<em>0</em>。</li>
<li>第2行至第4行，<strong>For&#8230;Next</strong>，是一个循环结构。在循环的过程中，变量<strong>$i</strong>的值从<em>1</em>开始依次递增，变为<em>2</em>、<em>3</em>、<em>4</em>&#8230;直至<em>256</em>为止。此后才会接着执行第5行代码。</li>
<li>第3行代码处于循环结构之中，
<ul>
<li>第一次执行的时候，<strong>$r = $r + 1</strong>（这次<strong>$i的值是1</strong>）；所以，<strong>$r = 0 + 1</strong>，即，在这行代码执行过后，变量<strong>$r</strong>的值变成了<em>1</em>；</li>
<li>第二次执行的时候，<strong>$r = $r + 2</strong>（这次<strong>$i的值是2</strong>）；所以，<strong>$r = 1 + 2</strong>，即，在这行代码执行过后，变量<strong>$r</strong>的值变成了<em>3</em>；</li>
<li>第二次执行的时候，<strong>$r = $r + 3</strong>（这次<strong>$i的值是3</strong>）；所以，<strong>$r = 3 + 3</strong>，即，在这行代码执行过后，变量<strong>$r</strong>的值变成了<em>6</em>；</li>
<li>以此类推……</li>
<li>直至<strong>$i</strong>的值递增为<strong>256</strong>之时，之前的<strong>$r</strong>的值再次被加上<strong>256</strong>之后，循环至此结束</li>
</ul>
</li>
<li>执行Next语句之后的代码，即第5行代码——输出结果，<em>32896</em>。</li>
</ul>
<p>而<strong>Testing7.au3</strong>和<strong>Testing8.au3</strong>，只不过是用了另外两个循环结构而已：<strong>While&#8230;WEnd</strong>和<strong>Do&#8230; Until</strong>。这两个循环结构的语法更加接近自然语言，基本上就是“Plain English”，很容易看懂。</p>
<p>请认真思考为什么<strong>Testing7.au3</strong>中的表达式（$i <= 256）用的是“<=”，而Testing8.au3中的表达式（$i > 256）用的是“>”？</p>
<blockquote><p><strong>作业</strong>：</p>
<ul>
<li> 仔细阅读AutoIt帮助文件中Keyword/Statement Reference中的关于For&#8230;Next、While&#8230;WEnd、以及Do&#8230;Until的内容，把其中的每一个例子都尝试着运行一遍。在运行之前，请用脑演练一遍代码，看看执行结果是否与想象得相同……</li>
<li> 写一个程序，计算“从1乘到某个特定的数字”的结果。</li>
<li> 把上面的程序总共写成三个版本，分别用For&#8230;Next、While&#8230;WEnd、以及Do&#8230;Until的循环结构，完成同样的功能。</li>
</ul>
</blockquote>
<hr />
<p><span>Footnotes:</span></p><ol class="footnotes"><li id="footnote_0_9171" class="footnote">这个例子中所需要的计算太简单，所以，“算法”上的优化没有太大作用。而在大型的软件工程中，算法优化非常重要，效果也异常惊人。不过，大多数普通用户的“任务”，往往用“笨方法”更划算……</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.lixiaolai.com/archives/9171.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  www.lixiaolai.com/archives/category/computer-related/auto-it/feed ) in 8.18963 seconds, on Feb 13th, 2012 at 2:37 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 13th, 2012 at 3:37 am UTC -->
