<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss 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" version="2.0">

<channel>
	<title>Ecommerce Ninja - The Zachary Fox Blog</title>
	
	<link>http://www.zacharyfox.com/blog</link>
	<description />
	<pubDate>Wed, 25 Jun 2008 17:18:42 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/ecommerceninja" type="application/rss+xml" /><item>
		<title>Using Selenium RC With Multiple Users</title>
		<link>http://www.zacharyfox.com/blog/testing/selenium-rc-multiple-users</link>
		<comments>http://www.zacharyfox.com/blog/testing/selenium-rc-multiple-users#comments</comments>
		<pubDate>Thu, 13 Mar 2008 17:33:31 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
		
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/testing/selenium-rc-multiple-users</guid>
		<description><![CDATA[So you may be testing with selenium already, you may even be using selenium RC to automate your testing and integrate it with your other unit tests, and you may already be doing so in a shared development environment, but if you aren&#8217;t using selenium RC with multiple users, you may not know that you [...]]]></description>
			<content:encoded><![CDATA[<p>So you may be <a href="http://pessoal.org/blog/2008/03/07/web-app-testing-with-selenium/">testing with selenium</a> already, you may even be using selenium RC to automate your testing and integrate it with your other unit tests, and you may already be doing so in a shared development environment, but if you aren&#8217;t using selenium RC with multiple users, you may not know that you can start the RC server on any port you wish, allowing multiple clients to connect at the same time.</p>
<p>To start the selenium RC server, you would typically run this command:</p>
<pre class="code">
java -jar selenium-server.jar</pre>
<p>Which will start it on the default port of 4444. If you want to start it on another port, simply add -port &lt;portnumber&gt; to the command like this:</p>
<pre class="code">
java -jar selenium-server.jar -port 1234</pre>
<p>This command would start the server on port 1234.</p>
<p><strong>Great, now I can  start the server on multiple ports, so what?</strong></p>
<p>So here is the good news for those of us in shared development environments. Say you have a windows virtual machine running selenium with your test browser installed. Things get complicated when multiple clients are testing at the same time. Tests can fail unexpectedly, developers begin fighting for time on the test server, and all hell breaks loose. By running multiple servers on different ports, we can avoid the third world war. Everyone can connect at the same time to their personal server, and we have peace and harmony in our testing environment.</p>
<p><strong>Wow, setting up a different port everytime must suck.</strong></p>
<p>Not really. There are many ways that you can automate the process so that it&#8217;s completely transparent to your development team. A lot will depend on your environment, but in our case, we created a .bat file that starts the servers in our virtual machine and placed it in the start-up folder.</p>
<pre class="vci_code">
<span style="color:#00ffff"><b>start</b></span> <span style="color:#ff40ff"><b>&quot;Selenium </b></span><span style="color:#ff40ff"><b>4444</b></span><span style="color:#ff40ff"><b>&quot;</b></span> <span style="color:#ff6060"><b>/min</b></span> java.exe -jar selenium-server.jar -port <span style="color:#ff40ff"><b>4444</b></span>
<span style="color:#00ffff"><b>start</b></span> <span style="color:#ff40ff"><b>&quot;Selenium </b></span><span style="color:#ff40ff"><b>4445</b></span><span style="color:#ff40ff"><b>&quot;</b></span> <span style="color:#ff6060"><b>/min</b></span> java.exe -jar selenium-server.jar -port <span style="color:#ff40ff"><b>4445</b></span>
<span style="color:#00ffff"><b>start</b></span> <span style="color:#ff40ff"><b>&quot;Selenium </b></span><span style="color:#ff40ff"><b>4446</b></span><span style="color:#ff40ff"><b>&quot;</b></span> <span style="color:#ff6060"><b>/min</b></span> java.exe -jar selenium-server.jar -port <span style="color:#ff40ff"><b>4446</b></span>
<span style="color:#00ffff"><b>start</b></span> <span style="color:#ff40ff"><b>&quot;Selenium </b></span><span style="color:#ff40ff"><b>4447</b></span><span style="color:#ff40ff"><b>&quot;</b></span> <span style="color:#ff6060"><b>/min</b></span> java.exe -jar selenium-server.jar -port <span style="color:#ff40ff"><b>4447</b></span>
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a></p>
<p>With this in place, we now have 4 selenium RC servers running at all times. If one or more crashes for any reason, running the batch file again will start any that are missing. The selenium RC server will not start if its port is in use, so only the ones missing will run, and you&#8217;ll have them all up and running again.</p>
<p><strong>Ok, what about the client side? I don&#8217;t want to have to edit another file in my development environment. </strong></p>
<p>On the client side, we are using phpunit with <a href="http://pear.php.net/package/Testing_Selenium/">Testing_Selenium</a>. Of course all of our selenium test classes extend a parent class, so it was easy enough for us to create some dynamic logic there to decide which port to use.</p>
<pre class="vci_code">
<span style="color:#ff6060"><b>&lt;?php</b></span>
<span style="color:#00ff00"><b>class</b></span> SeleniumTestHelper
<span style="color:#ff6060"><b>{</b></span>
    public <span style="color:#8080ff"><b>function</b></span> setup<span style="color:#ff6060"><b>()</b></span>
    <span style="color:#ff6060"><b>{</b></span>
        <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>ports</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#00ff00"><b>array</b></span><span style="color:#ff6060"><b>(</b></span>&#8216;<span style="color:#ff40ff"><b>user1</b></span>&#8216; <span style="color:#ffff00"><b>=</b></span><span style="color:#ffff00"><b>&gt;</b></span> <span style="color:#ff40ff"><b>4444</b></span>,
                       &#8216;<span style="color:#ff40ff"><b>user2</b></span>&#8216; <span style="color:#ffff00"><b>=</b></span><span style="color:#ffff00"><b>&gt;</b></span> <span style="color:#ff40ff"><b>4445</b></span>,
                       &#8216;<span style="color:#ff40ff"><b>user3</b></span>&#8216; <span style="color:#ffff00"><b>=</b></span><span style="color:#ffff00"><b>&gt;</b></span> <span style="color:#ff40ff"><b>4446</b></span>,
                       &#8216;<span style="color:#ff40ff"><b>user4</b></span>&#8216; <span style="color:#ffff00"><b>=</b></span><span style="color:#ffff00"><b>&gt;</b></span> <span style="color:#ff40ff"><b>4447</b></span><span style="color:#ff6060"><b>)</b></span>;
        <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>user</b></span> <span style="color:#ffff00"><b>=</b></span> someFunctionThatDeterminesUser<span style="color:#ff6060"><b>()</b></span>;
        <span style="color:#00ffff"><b>define</b></span><span style="color:#ff6060"><b>(</b></span>&#8216;<span style="color:#ff40ff"><b>BROWSER</b></span>&#8216;, &#8216;<span style="color:#ff40ff"><b>*firefox</b></span>&#8216;<span style="color:#ff6060"><b>)</b></span>;
        <span style="color:#00ffff"><b>define</b></span><span style="color:#ff6060"><b>(</b></span>&#8216;<span style="color:#ff40ff"><b>URL</b></span>&#8216;, &#8216;<span style="color:#ff40ff"><b>http://www.example.com</b></span>&#8216;<span style="color:#ff6060"><b>)</b></span>;
        <span style="color:#00ffff"><b>define</b></span><span style="color:#ff6060"><b>(</b></span>&#8216;<span style="color:#ff40ff"><b>SERVER</b></span>&#8216;, &#8216;<span style="color:#ff40ff"><b>192.168.0.2</b></span>&#8216;<span style="color:#ff6060"><b>)</b></span>;
        <span style="color:#00ffff"><b>define</b></span><span style="color:#ff6060"><b>(</b></span>&#8216;<span style="color:#ff40ff"><b>PORT</b></span>&#8216;, <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>ports</b></span><span style="color:#ff6060"><b>[</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>user</b></span><span style="color:#ff6060"><b>])</b></span>;
        <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>selenium <span style="color:#ffff00"><b>=</b></span> <span style="color:#8080ff"><b>new</b></span> Testing_Selenium<span style="color:#ff6060"><b>(</b></span>BROWSER, URL, SERVER, PORT<span style="color:#ff6060"><b>)</b></span>;
    <span style="color:#ff6060"><b>}</b></span>
<span style="color:#ff6060"><b>}</b></span>
<span style="color:#ff6060"><b>?&gt;</b></span>
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/testing/selenium-rc-multiple-users/feed</wfw:commentRss>
		</item>
		<item>
		<title>PHP Mail Class</title>
		<link>http://www.zacharyfox.com/blog/php/simple-mail-class</link>
		<comments>http://www.zacharyfox.com/blog/php/simple-mail-class#comments</comments>
		<pubDate>Sun, 03 Feb 2008 15:56:36 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/php/simple-mail-class</guid>
		<description><![CDATA[This is a very simple mailer class that is also easy to use.

&#60;?php
/**
 * mail.php
 *
 * A (very) simple mailer class written in PHP.
 *
 * @author Zachary Fox
 * @version 1.0
 */

class ZFmail{
    var $to = null;
    var $from = null;
    var $subject = [...]]]></description>
			<content:encoded><![CDATA[<p>This is a very simple mailer class that is also easy to use.</p>
<pre class="vci_code">
<span style="color:#ff6060"><b>&lt;?php</b></span>
<span style="color:#00ffff"><b>/**</b></span>
<span style="color:#00ffff"><b> * mail.php</b></span>
<span style="color:#00ffff"><b> *</b></span>
<span style="color:#00ffff"><b> * A (very) simple mailer class written in PHP.</b></span>
<span style="color:#00ffff"><b> *</b></span>
<span style="color:#00ffff"><b> * @author Zachary Fox</b></span>
<span style="color:#00ffff"><b> * @version 1.0</b></span>
<span style="color:#00ffff"><b> */</b></span>

<span style="color:#00ff00"><b>class</b></span> ZFmail<span style="color:#ff6060"><b>{</b></span>
    <span style="color:#ffff00"><b>var</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>to</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#00ff00"><b>null</b></span>;
    <span style="color:#ffff00"><b>var</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>from</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#00ff00"><b>null</b></span>;
    <span style="color:#ffff00"><b>var</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>subject</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#00ff00"><b>null</b></span>;
    <span style="color:#ffff00"><b>var</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>body</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#00ff00"><b>null</b></span>;
    <span style="color:#ffff00"><b>var</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>headers</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#00ff00"><b>null</b></span>;

     <span style="color:#8080ff"><b>function</b></span> ZFmail<span style="color:#ff6060"><b>(</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>to</b></span>,<span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>from</b></span>,<span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>subject</b></span>,<span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>body</b></span><span style="color:#ff6060"><b>){</b></span>
        <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>to      <span style="color:#ffff00"><b>=</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>to</b></span>;
        <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>from    <span style="color:#ffff00"><b>=</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>from</b></span>;
        <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>subject <span style="color:#ffff00"><b>=</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>subject</b></span>;
        <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>body    <span style="color:#ffff00"><b>=</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>body</b></span>;
    <span style="color:#ff6060"><b>}</b></span>

    <span style="color:#8080ff"><b>function</b></span> send<span style="color:#ff6060"><b>(){</b></span>
      <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>addHeader<span style="color:#ff6060"><b>(</b></span>&#8216;<span style="color:#ff40ff"><b>From: </b></span>&#8216;<span style="color:#ffff00"><b>.</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>from<span style="color:#ffff00"><b>.</b></span>&quot;<span style="color:#ff6060"><b>\r\n</b></span>&quot;<span style="color:#ff6060"><b>)</b></span>;
        <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>addHeader<span style="color:#ff6060"><b>(</b></span>&#8216;<span style="color:#ff40ff"><b>Reply-To: </b></span>&#8216;<span style="color:#ffff00"><b>.</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>from<span style="color:#ffff00"><b>.</b></span>&quot;<span style="color:#ff6060"><b>\r\n</b></span>&quot;<span style="color:#ff6060"><b>)</b></span>;
        <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>addHeader<span style="color:#ff6060"><b>(</b></span>&#8216;<span style="color:#ff40ff"><b>Return-Path: </b></span>&#8216;<span style="color:#ffff00"><b>.</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>from<span style="color:#ffff00"><b>.</b></span>&quot;<span style="color:#ff6060"><b>\r\n</b></span>&quot;<span style="color:#ff6060"><b>)</b></span>;
        <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>addHeader<span style="color:#ff6060"><b>(</b></span>&#8216;<span style="color:#ff40ff"><b>X-mailer: ZFmail 1.0</b></span>&#8216;<span style="color:#ffff00"><b>.</b></span>&quot;<span style="color:#ff6060"><b>\r\n</b></span>&quot;<span style="color:#ff6060"><b>)</b></span>;
        <span style="color:#00ffff"><b>mail</b></span><span style="color:#ff6060"><b>(</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>to,<span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>subject,<span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>body,<span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>headers<span style="color:#ff6060"><b>)</b></span>;
    <span style="color:#ff6060"><b>}</b></span>

    <span style="color:#8080ff"><b>function</b></span> addHeader<span style="color:#ff6060"><b>(</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>header</b></span><span style="color:#ff6060"><b>){</b></span>
        <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>this</b></span><span style="color:#00ff00"><b>-&gt;</b></span>headers <span style="color:#ffff00"><b>.=</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>header</b></span>;
    <span style="color:#ff6060"><b>}</b></span>

<span style="color:#ff6060"><b>}</b></span>
<span style="color:#ff6060"><b>?&gt;</b></span>
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a></p>
<p><strong>Usage</strong></p>
<p>Using the mail class is easy. Simply create a new ZFmail object, passing the parameters <em>$to</em>,<em>$from</em>,<em>$subject</em>, and <em>$body</em>, then call the method send on the object that you created. It&#8217;s as easy as pie. The following example is for a simple form mail script.</p>
<p><strong>Example</strong></p>
<pre class="vci_code">
<span style="color:#ff6060"><b>&lt;?php</b></span>
<span style="color:#00ffff"><b>/**</b></span>
<span style="color:#00ffff"><b> * example/mail.php</b></span>
<span style="color:#00ffff"><b> *</b></span>
<span style="color:#00ffff"><b> * An example script to accept a post and send an email using ZFmail.</b></span>
<span style="color:#00ffff"><b> *</b></span>
<span style="color:#00ffff"><b> * @author Zachary Fox</b></span>
<span style="color:#00ffff"><b> */</b></span>

 <span style="color:#00ffff"><b>// Include the mail.php file that holds the class definition</b></span>
 <span style="color:#8080ff"><b>require_once</b></span><span style="color:#ff6060"><b>(</b></span>&#8216;<span style="color:#ff40ff"><b>mail.php</b></span>&#8216;<span style="color:#ff6060"><b>)</b></span>;

 <span style="color:#00ffff"><b>// First we set the to address. I would not let anyone put in a to </b></span>
 <span style="color:#00ffff"><b>// address in a web form, and neither should you.</b></span>
 <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>to</b></span> <span style="color:#ffff00"><b>=</b></span> &#8216;<span style="color:#ff40ff"><b>me@example.com</b></span>&#8216;;

 <span style="color:#00ffff"><b>// Then we get the information we need from the $_POST array.</b></span>
 <span style="color:#00ffff"><b>// This step is not necessary, but in a production environment, </b></span>
 <span style="color:#00ffff"><b>// we would process and sanitize this data here, rather than </b></span>
 <span style="color:#00ffff"><b>// passing raw post data to the class.</b></span>
 <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>from</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>_POST</b></span><span style="color:#ff6060"><b>[</b></span>'<span style="color:#ff40ff"><b>from</b></span>'<span style="color:#ff6060"><b>]</b></span>;
 <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>subject</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>_POST</b></span><span style="color:#ff6060"><b>[</b></span>'<span style="color:#ff40ff"><b>subject</b></span>'<span style="color:#ff6060"><b>]</b></span>;
 <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>body</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>_POST</b></span><span style="color:#ff6060"><b>[</b></span>'<span style="color:#ff40ff"><b>body</b></span>'<span style="color:#ff6060"><b>]</b></span>;

 <span style="color:#00ffff"><b>// Then create the ZFmail object using the information from above</b></span>
 <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>mail</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#8080ff"><b>new</b></span> ZFmail<span style="color:#ff6060"><b>(</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>to</b></span>,<span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>from</b></span>,<span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>subject</b></span>,<span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>body</b></span><span style="color:#ff6060"><b>)</b></span>;

 <span style="color:#00ffff"><b>// Finally, call the object&#8217;s send method to deliver the mail.</b></span>
 <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>mail</b></span><span style="color:#00ff00"><b>-&gt;</b></span>send<span style="color:#ff6060"><b>()</b></span>;
<span style="color:#ff6060"><b>?&gt;</b></span>
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/php/simple-mail-class/feed</wfw:commentRss>
		</item>
		<item>
		<title>Vim Color Improved - Syntax Highlighting for WordPress</title>
		<link>http://www.zacharyfox.com/blog/free-tools/vim-color-improved</link>
		<comments>http://www.zacharyfox.com/blog/free-tools/vim-color-improved#comments</comments>
		<pubDate>Wed, 09 Jan 2008 00:31:01 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
		
		<category><![CDATA[Free Tools]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/free-tools/vim-color-improved</guid>
		<description><![CDATA[Vim Color Improved is a syntax highlighting plugin that allows you to include code from local or remote files in your Wordpress posts. I started using vim to syntax highlight my code samples based on a couple of searches that turned up this page here. However, since I started my new project that involves posting [...]]]></description>
			<content:encoded><![CDATA[<p>Vim Color Improved is a syntax highlighting plugin that allows you to include code from local or remote files in your Wordpress posts. I started using vim to syntax highlight my code samples based on a couple of searches that turned up this <a href="http://o.mengue.free.fr/blog/2007/08/25/39-syntax-highlighting-on-this-blog-using-semantic-tags-and-vim">page here</a>. However, since I started my new project that involves posting even more code, I thought it&#8217;s about time to come up with another solution. The only other vim highlighting plugin that I found was old, required a Perl library from  cpan, and didn&#8217;t seem to work in the version of WordPress I&#8217;m running here. So I set out to create my own.</p>
<p style="border: 1px solid #666666; padding: 10px; background: #eeeeee none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-size: 150%; font-weight: bold; text-align: center"><a href="http://www.zacharyfox.com/downloads/wordpress-plugins/vim-color-improved-0.3.2.zip">Download Vim Color Improved</a></p>
<p>It uses the same tag and parameter parsing as the popular codeviewer 1.4, and should be compatible with it&#8217;s options. In addition, any of the optional parameters from codeviewer 1.4 can be set as defaults, which can then be overriden by parameters in the tag. Vim Color Improved outputs code in &lt;pre&gt; formatted blocks, rather than ordered lists, which can be difficult to copy and paste, and can syntax highlight any language which vim supports.</p>
<p>Vim Color Improved contains a sophisticated caching system that stores the generated html to the filesystem. This greatly reduces the time required to display the code. In addition, it checks the modified time on both local and remote files to ensure that cached information is up-to-date. If it is unable to access the source code, and there is a cached version available, it will display the cached version with a notice.</p>
<p><strong>Using Vim Color Improved</strong></p>
<p>(These instructions basically parallel those for <a href="http://www.familjencarlstrom.se/WordPress/2006/08/17/code-viewer/">CodeViewer 1.4</a>)</p>
<p>Vim Color Improved searches your post for a custom tag named [viewcode ] [/viewcode], that tells the server to look at an external file and parse it into syntax higihlighted html. It can be placed anywhere a block-level tag is valid but the tag must be properly closed.</p>
<p>Note that there should not be a white space character after viewcode and before ].</p>
<p><strong>Parameters</strong><br />
[viewcode ] src=&#8221;URI or path to local file&#8221; link=yes|no lines= scroll=yes|no scrollheight=valid css height showsyntax=yes|no cache=yes|no[/viewcode]</p>
<p>Default values for all of these parameters, other than src can be set in the options page.</p>
<p>The src attribute is required.<br />
src - string - The URI or path to a local file of the code to display. Note that relative paths are in relation to the default_path set in the options page. This default value is set to the directory your blog is installed in.</p>
<p>The link attribute is optional.<br />
link - string - Should the link to the code be displayed (yes), or not be displayed (no). If the link attribute is left out of the tag completely, the value defaults to no.</p>
<p>The lines attribute is optional.<br />
lines - string - Which line numbers shall be visible in the output. Use , and - to separate line numbers. Example: lines=1,3-5,10-12,16-18,22.</p>
<p>The scroll attribute is optional.<br />
scroll - string - Should the scrollbar be displayed (yes), or not be displayed (no).</p>
<p>The scrollheight attribute is optional.<br />
height- string - Height of the scrollbar. Any valid css height declaration can be used. Example: 100px or 50em</p>
<p>The showsyntax attribute is optional.<br />
showsyntax - string - Should the syntax used of [viewcode ]  be displayed (yes), or not be displayed (no).</p>
<p>All attribute values can optionally be surrounded with double quotes (&#8221;) or single quotes(&#8217;).</p>
<p><strong>Installation </strong></p>
<ol>
<li>Download <a href="http://www.zacharyfox.com/downloads/wordpress-plugins/vim-color-improved-0.3.2.zip">vim-color-improved-0.3.2.zip</a>.</li>
<li>Unzip the archive and copy the entire vim-color-improved folder to the wp-content/plugins directory</li>
<li>Vim Color Improved needs a directory to store the cached files and to use as a temp directory. Please make sure that your web server can write to and read from the vim-color-improved/tmp directory.</li>
<li>Activate the plugin from the Plugins page in your WordPress administration console.</li>
<li>Vim Color Improved also provides an options page for you to set the default options. While the plugin will work without any intervention, you may wish to review these at (Options-&gt;Vim Color Improved. You may also see a list of cached files and clear the cache there.</li>
</ol>
<p><strong>Frequently Asked Questions</strong></p>
<p>Why are there no FAQs?</p>
<p>This is the first release.</p>
<p><strong>Example </strong></p>
<p>Here is an example of Vim Color Improved in action. We can see here the parameters that were passed in the tag by looking at the showsyntax block above the html code block.</p>
<p class="vci_info">[viewcode]src=http://www.zacharyfox.com/blog/wp-content/plugins/vim-color-improved/style.css cache=yes showsyntax=yes[/viewcode]</p>
<pre class="vci_code">
<span style="color:#ffff00"><b>pre</b></span>.vci_code<span style="color:#00ffff"><b>{</b></span>
        <span style="color:#00ff00"><b>background</b></span>:<span style="color:#ff40ff"><b>#333</b></span>;
        <span style="color:#00ff00"><b>color</b></span>:<span style="color:#ff40ff"><b>#fff</b></span>;
        <span style="color:#00ff00"><b>padding</b></span>:<span style="color:#ff40ff"><b>5px</b></span>;
        <span style="color:#00ff00"><b>font-size</b></span>:<span style="color:#ff40ff"><b>12px</b></span>;
        <span style="color:#00ff00"><b>margin-bottom</b></span>:<span style="color:#ff40ff"><b>0</b></span>;
        <span style="color:#00ff00"><b>margin-top</b></span>:<span style="color:#ff40ff"><b>0</b></span>;
<span style="color:#00ffff"><b>}</b></span>

<span style="color:#ffff00"><b>p</b></span>.vci_info, <span style="color:#ffff00"><b>p</b></span>.vci_warning<span style="color:#00ffff"><b>{</b></span>
        <span style="color:#00ff00"><b>background</b></span>:<span style="color:#ff40ff"><b>#eee</b></span>;
        <span style="color:#00ff00"><b>color</b></span>:<span style="color:#ff40ff"><b>#666</b></span>;
        <span style="color:#00ff00"><b>font-size</b></span>:<span style="color:#ff40ff"><b>80%</b></span>;
        <span style="color:#00ff00"><b>padding</b></span>:<span style="color:#ff40ff"><b>3px</b></span>;
        <span style="color:#00ff00"><b>margin-top</b></span>:<span style="color:#ff40ff"><b>0</b></span>;
        <span style="color:#00ff00"><b>margin-bottom</b></span>:<span style="color:#ff40ff"><b>0</b></span>;
        <span style="color:#00ff00"><b>text-align</b></span>:<span style="color:#00ff00"><b>right</b></span>;
<span style="color:#00ffff"><b>}</b></span>

<span style="color:#ffff00"><b>p</b></span>.vci_warning<span style="color:#00ffff"><b>{</b></span>
        <span style="color:#00ff00"><b>background</b></span>:<span style="color:#ff40ff"><b>#fee</b></span>;
        <span style="color:#00ff00"><b>font-weight</b></span>:<span style="color:#00ff00"><b>bold</b></span>;
        <span style="color:#00ff00"><b>text-align</b></span>:<span style="color:#00ff00"><b>left</b></span>;
<span style="color:#00ffff"><b>}</b></span>
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a></p>
<p><strong>Requirements </strong></p>
<p>This plugin may not work on all php installations. Specifically, there are some access needs that may be locked down on your web server.</p>
<ol>
<li>Your web server must be able to exec(vim) through php</li>
<li>If you want to use remote files, your web server must be able to open the files through http using file()</li>
</ol>
<p><strong>To Do List</strong></p>
<ol>
<li>Add the ability to use vim&#8217;s options, such as using css, using xhtml, etc&#8230;</li>
<li>Add the ability to use WYSIWYG editor for posts, including file selection box for local files.</li>
</ol>
<p><strong>Version History</strong></p>
<p>v.0.3.2 First Public Version</p>
<p><strong>License</strong></p>
<p>Copyright 2008  Zachary Fox  (email : ecommerceninja at gmail dot com)</p>
<p>This program is free software; you can redistribute it and/or modify<br />
it under the terms of the GNU General Public License as published by<br />
the Free Software Foundation; either version 2 of the License, or<br />
(at your option) any later version.</p>
<p>This program is distributed in the hope that it will be useful,<br />
but WITHOUT ANY WARRANTY; without even the implied warranty of<br />
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br />
GNU General Public License for more details.</p>
<p>You should have received a copy of the GNU General Public License<br />
along with this program; if not, write to the Free Software<br />
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/free-tools/vim-color-improved/feed</wfw:commentRss>
		</item>
		<item>
		<title>Fibonacci in Java</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-java</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-java#comments</comments>
		<pubDate>Sun, 06 Jan 2008 17:53:59 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
		
		<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-java</guid>
		<description><![CDATA[So we&#8217;ve seen lots of examples of our Fibonacci program in scripting languages, but other than C, I haven&#8217;t touched on many compiled languages. So here is a version in Java, probably one of the most common and popular languages in use today. Considered by advocates to be the among the best enterprise application development [...]]]></description>
			<content:encoded><![CDATA[<p>So we&#8217;ve seen lots of examples of our Fibonacci program in scripting languages, but other than C, I haven&#8217;t touched on many compiled languages. So here is a version in <a href="http://www.java.com/en/">Java</a>, probably one of the most common and popular languages in use today. Considered by advocates to be the among the best enterprise application development languages, we&#8217;ll see here how to write our simple program.</p>
<pre class="vci_code">
<span style="color:#00ffff"><b>/*</b></span>
<span style="color:#00ffff"><b> * Much like C&#8217;s stdio.h, the java.io.* classes will let us access stdin and stdout.</b></span>
<span style="color:#00ffff"><b> */</b></span>

<span style="color:#8080ff"><b>import</b></span> java.io.*;

<span style="color:#00ffff"><b>/*</b></span>
<span style="color:#00ffff"><b> * Like Ruby, in java, everything is an object. To write a program, you&#8217;ll need to</b></span>
<span style="color:#00ffff"><b> * start by declaring a class. As in C, our program execution starts with the main</b></span>
<span style="color:#00ffff"><b> * function. Also like C, Java is a compiled language, so you&#8217;ll need to compile </b></span>
<span style="color:#00ffff"><b> * the code and then run the class using the java interpreter.</b></span>
<span style="color:#00ffff"><b> */</b></span>

<span style="color:#00ff00"><b>class</b></span> Fibonacci {

  <span style="color:#00ffff"><b>/*</b></span>
<span style="color:#00ffff"><b>   * So here we are defining the main function. Remember that this is supposed to</b></span>
<span style="color:#00ffff"><b>   * actually run this program, so the function needs to be `public`, in addition,</b></span>
<span style="color:#00ffff"><b>   * it&#8217;s `static`, meaning we can call this method without an object of the Fibonacci</b></span>
<span style="color:#00ffff"><b>   * class being instantiated, and it doesn&#8217;t need to return anything, as it will</b></span>
<span style="color:#00ffff"><b>   * run by the interpreter, which will handle the exit status. </b></span>
<span style="color:#00ffff"><b>   */</b></span>

  <span style="color:#00ff00"><b>public</b></span> <span style="color:#00ff00"><b>static</b></span> <span style="color:#00ff00"><b>void</b></span> main(String args[]) {

    <span style="color:#00ffff"><b>/* </b></span>
<span style="color:#00ffff"><b>     * We are using System.out.println here, but newer versions of Java have the printf method.</b></span>
<span style="color:#00ffff"><b>     */</b></span>

    System.out.println(<span style="color:#ff40ff"><b>&quot;How many numbers of the sequence would you like?&quot;</b></span>);

    <span style="color:#00ffff"><b>/*</b></span>
<span style="color:#00ffff"><b>     * I&#8217;m sure there&#8217;s more than one way to skin a cat, but to read stdin here, we</b></span>
<span style="color:#00ffff"><b>     * are creating a new BufferedReader, which will read one line of input.</b></span>
<span style="color:#00ffff"><b>     */</b></span>

    InputStreamReader sr = <span style="color:#ffff00"><b>new</b></span> InputStreamReader(System.in);
    BufferedReader br    = <span style="color:#ffff00"><b>new</b></span> BufferedReader(sr);

    <span style="color:#00ffff"><b>/*</b></span>
<span style="color:#00ffff"><b>     * Now here is a concept we haven&#8217;t addressed yet. The java compiler complains if</b></span>
<span style="color:#00ffff"><b>     * you try to call a method that could throw an exception (error), so I&#8217;ve included</b></span>
<span style="color:#00ffff"><b>     * an example here of how to handle the exceptions that could be thrown. Also, like</b></span>
<span style="color:#00ffff"><b>     * in our previous examples, we are casting the input to an integer.</b></span>
<span style="color:#00ffff"><b>     */</b></span>

    <span style="color:#ffff00"><b>try</b></span> {
      String input = br.readLine();
      <span style="color:#00ff00"><b>int</b></span> n = Integer.valueOf(input).intValue();
      fibonacci(n);
    } <span style="color:#ffff00"><b>catch</b></span> (NumberFormatException e){
      System.out.println(<span style="color:#ff40ff"><b>&quot;That is not an integer. Please enter an integer value&quot;</b></span>);
    } <span style="color:#ffff00"><b>catch</b></span> (IOException e) {
      System.out.println(<span style="color:#ff40ff"><b>&quot;I did not recieve an input&quot;</b></span>);
    }
  }

  <span style="color:#00ffff"><b>/*</b></span>
<span style="color:#00ffff"><b>   * So here is our Fibonacci function. like the main function, it is public and can be</b></span>
<span style="color:#00ffff"><b>   * called without creating a Fibonacci object. We&#8217;ve also introduced a new method of </b></span>
<span style="color:#00ffff"><b>   * calculating the sequence without using a temporary variable. In a later post, I will</b></span>
<span style="color:#00ffff"><b>   * examine the different algorithms used to calculate the Fibonacci sequence, and compare</b></span>
<span style="color:#00ffff"><b>   * performance in multiple languages.</b></span>
<span style="color:#00ffff"><b>   */</b></span>

  <span style="color:#00ff00"><b>public</b></span> <span style="color:#00ff00"><b>static</b></span> <span style="color:#00ff00"><b>void</b></span> fibonacci(<span style="color:#00ff00"><b>int</b></span> n){
    <span style="color:#00ff00"><b>int</b></span> a=<span style="color:#ff40ff"><b>0</b></span>,b=<span style="color:#ff40ff"><b>1</b></span>;

    <span style="color:#ffff00"><b>for</b></span> (<span style="color:#00ff00"><b>int</b></span> i=<span style="color:#ff40ff"><b>0</b></span>;i&lt;n;i++){
      System.out.println(a);
      a=a+b;
      b=a-b;
    }
  }
}
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.java">fibonacci.java</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-java/feed</wfw:commentRss>
		</item>
		<item>
		<title>Fibonacci in Ruby</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-ruby</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-ruby#comments</comments>
		<pubDate>Sun, 06 Jan 2008 01:01:42 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
		
		<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-ruby</guid>
		<description><![CDATA[So finally we get to Ruby. Is it a ghetto? I don&#8217;t know about that, but I do know that writing our simple Fibonacci program in Ruby was a piece of cake. Before we get to the good stuff, though, I&#8217;d like to recap where we are, and what we&#8217;ve done so far. Here&#8217;s a [...]]]></description>
			<content:encoded><![CDATA[<p align="left">So finally we get to <a href="http://www.ruby-lang.org/en/">Ruby</a>. Is it a ghetto? I don&#8217;t know about that, but I do know that writing our simple Fibonacci program in Ruby was a piece of cake. Before we get to the good stuff, though, I&#8217;d like to recap where we are, and what we&#8217;ve done so far. Here&#8217;s a list of the Fibonacci project programs so far:</p>
<ul>
<li><a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">Fibonacci in C</a></li>
<li><a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php">Fibonacci in PHP</a></li>
<li><a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-bash">Fibonacci in BASH</a></li>
<li><a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl">Fibonacci in Perl</a></li>
<li><a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-python">Fibonacci in Python</a></li>
</ul>
<p>So this is our sixth version of the simple sequence generator, this time in Ruby</p>
<pre class="vci_code">
<span style="color:#8080ff"><b>#!/usr/local/bin/ruby</b></span>

<span style="color:#00ffff"><b># In Ruby, we define a function with def&#8230;end functions can accept parameters,</b></span>
<span style="color:#00ffff"><b># but don&#8217;t have to, and you can leave off the parenthesis if your function does</b></span>
<span style="color:#00ffff"><b># not need them.</b></span>

<span style="color:#8080ff"><b>def </b></span><span style="color:#00ffff"><b>main</b></span>
  printf <span style="color:#ff6060"><b>&quot;</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ff40ff"><b>How many numbers of the sequence would you like?</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ff6060"><b>&quot;</b></span>

  <span style="color:#00ffff"><b># Here, STDIN is a constant, but like everything in Ruby, it&#8217;s also a class, so</b></span>
  <span style="color:#00ffff"><b># we use the readline method to get our input. to_i casts our input to an integer</b></span>

  n = <span style="color:#ff6060"><b>STDIN</b></span>.readline.to_i
  fibonacci(n)
<span style="color:#8080ff"><b>end</b></span>

<span style="color:#00ffff"><b># Here is a good example of something that is cool about Ruby. The times method</b></span>
<span style="color:#00ffff"><b># works just like it sounds, it will do something n times. As in Perl and Python,</b></span>
<span style="color:#00ffff"><b># we don&#8217;t need a temp variable here to swap the values for a and b.</b></span>

<span style="color:#8080ff"><b>def </b></span><span style="color:#00ffff"><b>fibonacci</b></span>(n)
  a,b = <span style="color:#ff40ff"><b>0</b></span>,<span style="color:#ff40ff"><b>1</b></span>
  n.times <span style="color:#ffff00"><b>do</b></span>
    printf(<span style="color:#ff6060"><b>&quot;</b></span><span style="color:#ff40ff"><b>%d</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ff6060"><b>&quot;</b></span>, a)
    a,b = b,a+b
  <span style="color:#ffff00"><b>end</b></span>
<span style="color:#8080ff"><b>end</b></span>

main
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.rb">fibonacci.rb</a></p>
<p>The next installment will be another popular language. Stay tuned to see <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-java">Fibonacci in Java</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-ruby/feed</wfw:commentRss>
		</item>
		<item>
		<title>Fibonacci in Python</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-python</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-python#comments</comments>
		<pubDate>Sun, 06 Jan 2008 00:34:05 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
		
		<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-python</guid>
		<description><![CDATA[In our latest installment of the Fibonacci project, we&#8217;ll write our simple program in Python. For those of you who are unfamiliar with what we are doing here, please read the first post here. Python is a newer, but increasingly popular scripting language. I suppose the most interesting difference here is that Python is dependent [...]]]></description>
			<content:encoded><![CDATA[<p>In our latest installment of the Fibonacci project, we&#8217;ll write our simple program in <a href="http://www.python.org/">Python</a>. For those of you who are unfamiliar with what we are doing here, please read the <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">first post here</a>. Python is a newer, but increasingly popular scripting language. I suppose the most interesting difference here is that Python is dependent on indentation to define blocks, rather than braces or other language constructs.</p>
<pre class="vci_code">
<span style="color:#00ffff"><b>#!/usr/bin/python</b></span>

<span style="color:#00ffff"><b># Import the system library. This allows us to access stdin later.</b></span>
<span style="color:#8080ff"><b>import</b></span> sys

<span style="color:#00ffff"><b># Here&#8217;s our main function. Python is pretty efficient here. You</b></span>
<span style="color:#00ffff"><b># should notice that there are no braces. Python is dependant on</b></span>
<span style="color:#00ffff"><b># whitespace to define blocks.</b></span>

<span style="color:#ffff00"><b>def</b></span> <span style="color:#00ffff"><b>main</b></span>():
  <span style="color:#ffff00"><b>print</b></span> &quot;<span style="color:#ff6060"><b>\n</b></span><span style="color:#ff40ff"><b>How many numbers of the sequence would you like?</b></span>&quot;
  n = int(sys.stdin.readline())
  fibonacci(n)

<span style="color:#00ffff"><b># Here&#8217;s the fibonacci function. Like in Perl, you can assign multiple</b></span>
<span style="color:#00ffff"><b># variables on a line without using a temporary variable. Also, the for </b></span>
<span style="color:#00ffff"><b># loop here works more like a foreach loop by setting a range from 0 to n.</b></span>

<span style="color:#ffff00"><b>def</b></span> <span style="color:#00ffff"><b>fibonacci</b></span>(n):
  a,b = 0,1
  <span style="color:#ffff00"><b>for</b></span> i <span style="color:#ffff00"><b>in</b></span> range(0,n):
    <span style="color:#ffff00"><b>print</b></span> a
    a,b, = b,a+b

main()
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-python/feed</wfw:commentRss>
		</item>
		<item>
		<title>Fibonacci in Perl</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl#comments</comments>
		<pubDate>Thu, 03 Jan 2008 21:47:11 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
		
		<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl</guid>
		<description><![CDATA[So this is our fourth post of the Fibonacci project, and in this installment, we are going to take on Perl. For those of you who don&#8217;t know about the Fibonacci project, you should read the first post, Fibonacci in C. Also, you may want to check out the other posts in the series. The [...]]]></description>
			<content:encoded><![CDATA[<p>So this is our fourth post of the Fibonacci project, and in this installment, we are going to take on Perl. For those of you who don&#8217;t know about the Fibonacci project, you should read the first post, <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">Fibonacci in C</a>. Also, you may want to check out the other posts in the series. The comments in the code will make more sense if you have seen the other examples.</p>
<pre class="vci_code">
<span style="color:#8080ff"><b>#!/usr/bin/perl</b></span>

<span style="color:#00ffff"><b># Functions in Perl are called subroutines. Like bash, you don&#8217;t define the </b></span>
<span style="color:#00ffff"><b># parameters in the function declaration. We&#8217;ll see how to access the parameters</b></span>
<span style="color:#00ffff"><b># when we look at the Fibonacci subroutine below.</b></span>

<span style="color:#ffff00"><b>sub</b></span><span style="color:#00ffff"><b> </b></span><span style="color:#00ffff"><b>main</b></span><span style="color:#00ffff"><b> </b></span>{

<span style="color:#00ffff"><b># Again, the ubiquitous printf function. Apparently a staple of programming</b></span>
<span style="color:#ffff00"><b>printf</b></span> <span style="color:#ff40ff"><b>&quot;</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ff40ff"><b>How many numbers of the sequence would you like?</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ff40ff"><b>&quot;</b></span>;

<span style="color:#00ffff"><b># Once again, we don&#8217;t need to declare variables before using them.</b></span>
<span style="color:#00ffff"><b># Perl&#8217;s scalar variables, prefaced with $, can be either strings or numbers</b></span>
<span style="color:#00ffff"><b># We use &lt;STDIN&gt; to get the data from stdin here</b></span>

<span style="color:#00ffff"><b>$n</b></span> = <span style="color:#00ffff"><b>&lt;STDIN&gt;</b></span>;

<span style="color:#00ffff"><b># As in PHP, we need to remove the newline at the end</b></span>

<span style="color:#ffff00"><b>chop</b></span> <span style="color:#00ffff"><b>$n</b></span>;

<span style="color:#00ffff"><b>&amp;fibonacci</b></span>(<span style="color:#00ffff"><b>$n</b></span>);

<span style="color:#ffff00"><b>exit</b></span> <span style="color:#ff40ff"><b>0</b></span>;
}

<span style="color:#00ffff"><b># Except for the first line declaring the subroutine, and the different way that</b></span>
<span style="color:#00ffff"><b># parameters passed to the subroutine are passed, this is identical to the PHP version</b></span>

<span style="color:#ffff00"><b>sub</b></span><span style="color:#00ffff"><b> </b></span><span style="color:#00ffff"><b>fibonacci</b></span><span style="color:#00ffff"><b> </b></span>{
  <span style="color:#00ffff"><b>$a</b></span> = <span style="color:#ff40ff"><b>0</b></span>;
  <span style="color:#00ffff"><b>$b</b></span> = <span style="color:#ff40ff"><b>1</b></span>;

  <span style="color:#00ffff"><b># So now we see the parameter being used here. For clarity, I have written the for</b></span>
  <span style="color:#00ffff"><b># loop using $n like the other examples. To set $n using the first parameter passed</b></span>
  <span style="color:#00ffff"><b># to the subroutine, I access the scalar variable $_[0], which is the first element</b></span>
  <span style="color:#00ffff"><b># of the parameter array @_</b></span>

  <span style="color:#00ffff"><b>$n</b></span> = <span style="color:#00ffff"><b>$_</b></span>[<span style="color:#ff40ff"><b>0</b></span>];

  <span style="color:#ffff00"><b>for</b></span> (<span style="color:#00ffff"><b>$i</b></span>=<span style="color:#ff40ff"><b>0</b></span>;<span style="color:#00ffff"><b>$i</b></span>&lt;<span style="color:#00ffff"><b>$n</b></span>;<span style="color:#00ffff"><b>$i</b></span>++){
    <span style="color:#ffff00"><b>printf</b></span> <span style="color:#ff40ff"><b>&quot;</b></span><span style="color:#00ffff"><b>%d</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ff40ff"><b>&quot;</b></span>, <span style="color:#00ffff"><b>$a</b></span>;
    <span style="color:#00ffff"><b>$sum</b></span> = <span style="color:#00ffff"><b>$a</b></span> + <span style="color:#00ffff"><b>$b</b></span>;
    <span style="color:#00ffff"><b>$a</b></span> = <span style="color:#00ffff"><b>$b</b></span>;
    <span style="color:#00ffff"><b>$b</b></span> = <span style="color:#00ffff"><b>$sum</b></span>;
  }
}

<span style="color:#00ffff"><b>&amp;main</b></span>;
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.pl">fibonacci.pl</a></p>
<p>Any Perl gurus want to critique my work here? Like most of the examples, this is a simplified version, but one that works.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl/feed</wfw:commentRss>
		</item>
		<item>
		<title>Fibonacci in BASH</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-bash</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-bash#comments</comments>
		<pubDate>Thu, 03 Jan 2008 19:59:05 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
		
		<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-bash</guid>
		<description><![CDATA[So here&#8217;s the big surprise. Shell scripting! It&#8217;s not what most people think of when they think of programming, but systems administrators are still doing lots of scripting work to automate tasks. Shell scripting can tackle some pretty serious and complicated problems, so running our Fibonacci program is no sweat. If you haven&#8217;t seen the [...]]]></description>
			<content:encoded><![CDATA[<p>So here&#8217;s the big surprise. Shell scripting! It&#8217;s not what most people think of when they think of programming, but systems administrators are still doing lots of scripting work to automate tasks. Shell scripting can tackle some pretty serious and complicated problems, so running our Fibonacci program is no sweat. If you haven&#8217;t seen the <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">Fibonacci in C</a> and the <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php">Fibonacci in PHP</a>, now might be a good time to review them.</p>
<pre class="vci_code">
<span style="color:#00ffff"><b>#!/bin/bash</b></span>

<span style="color:#00ffff"><b>#</b></span>
<span style="color:#00ffff"><b># Here is our main function. Like PHP, I could have simply placed this in the script</b></span>
<span style="color:#00ffff"><b># rather than having a function. You notice that like C and PHP, the printf function</b></span>
<span style="color:#00ffff"><b># still exists. Unlike C and PHP function calls do not enclose the parameters with </b></span>
<span style="color:#00ffff"><b># paraenthsis.</b></span>

<span style="color:#00ffff"><b>function</b></span> main <span style="color:#ff6060"><b>{</b></span>
  <span style="color:#ffff00"><b>printf</b></span> <span style="color:#ffff00"><b>&quot;</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ff40ff"><b>How many numbers of the sequence would you like?</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ffff00"><b>&quot;</b></span>

  <span style="color:#00ffff"><b># Bash lets us use a function &quot;read&quot; to get input from stdin</b></span>
  <span style="color:#00ffff"><b># Notice that we are not using a dollar sign in front of the variables when</b></span>
  <span style="color:#00ffff"><b># we set them, but we need to when we use them.</b></span>

  <span style="color:#ffff00"><b>read</b></span> n
  fibonacci <span style="color:#8080ff"><b>$n</b></span>

  <span style="color:#00ffff"><b># Again, I&#8217;m exiting with status 0</b></span>

  <span style="color:#ffff00"><b>exit</b></span> <span style="color:#ff40ff"><b>0</b></span>
<span style="color:#ff6060"><b>}</b></span>

<span style="color:#00ffff"><b># Starting to look familiar? This function is actually a little different than the </b></span>
<span style="color:#00ffff"><b># C style for loop that we&#8217;ve used before. Instead we use while..do..done to accomplish</b></span>
<span style="color:#00ffff"><b># the same thing. You&#8217;ll also see that rather than specifying parameters in the </b></span>
<span style="color:#00ffff"><b># function;s declaration, we simply reference the variable &quot;$1&quot; to get the first parameter</b></span>
<span style="color:#00ffff"><b># passed when the function was called.</b></span>

<span style="color:#00ffff"><b>function</b></span> fibonacci <span style="color:#ff6060"><b>{</b></span>
  <span style="color:#00ffff"><b>a</b></span>=<span style="color:#ff40ff"><b>0</b></span>
  <span style="color:#00ffff"><b>b</b></span>=<span style="color:#ff40ff"><b>1</b></span>
  <span style="color:#00ffff"><b>i</b></span>=<span style="color:#ff40ff"><b>0</b></span>

  <span style="color:#ffff00"><b>while</b></span><span style="color:#ffff00"><b> </b></span><span style="color:#ffff00"><b>[</b></span><span style="color:#ffff00"><b> </b></span><span style="color:#8080ff"><b>$i</b></span><span style="color:#ffff00"><b> </b></span><span style="color:#ffff00"><b>-lt</b></span><span style="color:#ffff00"><b> </b></span><span style="color:#8080ff"><b>$1</b></span><span style="color:#ffff00"><b> </b></span><span style="color:#ffff00"><b>]</b></span>
<span style="color:#ffff00"><b>  </b></span><span style="color:#ffff00"><b>do</b></span>
    <span style="color:#ffff00"><b>printf</b></span> <span style="color:#ffff00"><b>&quot;</b></span><span style="color:#ff40ff"><b>%d</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ffff00"><b>&quot;</b></span> <span style="color:#8080ff"><b>$a</b></span>
    <span style="color:#ffff00"><b>let</b></span> <span style="color:#00ffff"><b>sum</b></span>=<span style="color:#8080ff"><b>$a</b></span>+<span style="color:#8080ff"><b>$b</b></span>
    <span style="color:#ffff00"><b>let</b></span> <span style="color:#00ffff"><b>a</b></span>=<span style="color:#8080ff"><b>$b</b></span>
    <span style="color:#ffff00"><b>let</b></span> <span style="color:#00ffff"><b>b</b></span>=<span style="color:#8080ff"><b>$sum</b></span>
    <span style="color:#ffff00"><b>let</b></span> <span style="color:#00ffff"><b>i</b></span>=<span style="color:#8080ff"><b>$i</b></span>+<span style="color:#ff40ff"><b>1</b></span>
  <span style="color:#ffff00"><b>done</b></span>
<span style="color:#ff6060"><b>}</b></span>

main
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.sh">fibonacci.sh</a></p>
<p>So what are the <a href="http://tldp.org/LDP/abs/html/gotchas.html">gotchas for scripting in BASH</a>? What killed me is whitespace! (a=1 is not the same as a = 1). Once you get the hang of it, it&#8217;s not a big deal, but if you are used to working in PHP or another language that ignores whitespace, you&#8217;ll run through a few syntax errors before figuring it out. Using an editor like Vim that has syntax highlighting will help a lot there.So there is the third version of our Fibonacci project. Coming soon, I&#8217;ll take on the popular scripting languages perl, python, and ruby.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-bash/feed</wfw:commentRss>
		</item>
		<item>
		<title>Fibonacci in PHP</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php#comments</comments>
		<pubDate>Thu, 03 Jan 2008 19:49:11 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
		
		<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php</guid>
		<description><![CDATA[So here is my second post in the Fibonacci project. For those of you that have not seen the first post here, the Fibonacci project is a learning experiment designed to highlight the similarities and differences between programming languages. In our C Fibonacci example, we learned that we need to declare variables and types, even [...]]]></description>
			<content:encoded><![CDATA[<p>So here is my second post in the Fibonacci project. For those of you that have not seen the <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">first post here</a>, the Fibonacci project is a learning experiment designed to highlight the similarities and differences between programming languages. In our <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">C Fibonacci example</a>, we learned that we need to declare variables and types, even the return type of a function. In this PHP example, we&#8217;ll see that PHP, while different, is very similar to C in syntax.</p>
<pre class="vci_code">
#!/usr/bin/php -q

<span style="color:#ff6060"><b>&lt;?php</b></span>
<span style="color:#00ffff"><b>/**</b></span>
<span style="color:#00ffff"><b> * This is our main function. In PHP, there isn&#8217;t really a need for this to be</b></span>
<span style="color:#00ffff"><b> * a function, but to keep consistant, I&#8217;ve placed it there anyway, and called</b></span>
<span style="color:#00ffff"><b> * it from the script. In keeping with good programming practices, this script</b></span>
<span style="color:#00ffff"><b> * exits with a status 0. Any other status would indicate an error condition.</b></span>
<span style="color:#00ffff"><b> */</b></span>

<span style="color:#8080ff"><b>function</b></span> main<span style="color:#ff6060"><b>(){</b></span>

  <span style="color:#00ffff"><b>printf</b></span><span style="color:#ff6060"><b>(</b></span>&quot;<span style="color:#ff40ff"><b>How many numbers of the sequence would you like?</b></span><span style="color:#ff6060"><b>\n</b></span>&quot;<span style="color:#ff6060"><b>)</b></span>;

  <span style="color:#00ffff"><b>/*</b></span>
<span style="color:#00ffff"><b>   * To read from stdin in php, you open it as a file handle, and use</b></span>
<span style="color:#00ffff"><b>   * the standard file reading functions</b></span>
<span style="color:#00ffff"><b>   */</b></span>

  <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>fr</b></span><span style="color:#ffff00"><b>=</b></span><span style="color:#00ffff"><b>fopen</b></span><span style="color:#ff6060"><b>(</b></span>&quot;<span style="color:#ff40ff"><b>php://stdin</b></span>&quot;,&quot;<span style="color:#ff40ff"><b>r</b></span>&quot;<span style="color:#ff6060"><b>)</b></span>;
  <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>n</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#00ffff"><b>rtrim</b></span><span style="color:#ff6060"><b>(</b></span><span style="color:#00ffff"><b>fgets</b></span><span style="color:#ff6060"><b>(</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>fr</b></span>,<span style="color:#ff40ff"><b>128</b></span><span style="color:#ff6060"><b>))</b></span>;
  <span style="color:#00ffff"><b>fclose</b></span> <span style="color:#ff6060"><b>(</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>fr</b></span><span style="color:#ff6060"><b>)</b></span>;

  <span style="color:#00ffff"><b>/* Then we call the fibonacci function */</b></span>
  fibonacci<span style="color:#ff6060"><b>(</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>n</b></span><span style="color:#ff6060"><b>)</b></span>;
  <span style="color:#ffff00"><b>exit</b></span><span style="color:#ff6060"><b>(</b></span><span style="color:#ff40ff"><b>0</b></span><span style="color:#ff6060"><b>)</b></span>;
<span style="color:#ff6060"><b>}</b></span>

<span style="color:#00ffff"><b>/**</b></span>
<span style="color:#00ffff"><b> * The fibonacci function should look familiar.</b></span>
<span style="color:#00ffff"><b> */</b></span>

<span style="color:#8080ff"><b>function</b></span> fibonacci<span style="color:#ff6060"><b>(</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>n</b></span><span style="color:#ff6060"><b>){</b></span>
  <span style="color:#00ffff"><b>/* </b></span>
<span style="color:#00ffff"><b>   * Notice that in PHP, we do not have to declare our variables, nor must we</b></span>
<span style="color:#00ffff"><b>   * declare their types. Also see how variables in PHP are prefaced with the </b></span>
<span style="color:#00ffff"><b>   * dollar sign. Other than that, this is identical to the C version of this function.</b></span>
<span style="color:#00ffff"><b>   */</b></span>
  <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>a</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#ff40ff"><b>0</b></span>;
  <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>b</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#ff40ff"><b>1</b></span>;
  <span style="color:#ffff00"><b>for</b></span> <span style="color:#ff6060"><b>(</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>i</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#ff40ff"><b>0</b></span>; <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>i</b></span> <span style="color:#ffff00"><b>&lt;</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>n</b></span>; <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>i</b></span><span style="color:#ffff00"><b>++</b></span><span style="color:#ff6060"><b>){</b></span>
    <span style="color:#00ffff"><b>printf</b></span><span style="color:#ff6060"><b>(</b></span>&quot;<span style="color:#ff40ff"><b>%d</b></span><span style="color:#ff6060"><b>\n</b></span>&quot;,<span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>a</b></span><span style="color:#ff6060"><b>)</b></span>;
    <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>sum</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>a</b></span><span style="color:#ffff00"><b>+</b></span><span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>b</b></span>;
    <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>a</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>b</b></span>;
    <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>b</b></span> <span style="color:#ffff00"><b>=</b></span> <span style="color:#ffff00"><b>$</b></span><span style="color:#00ffff"><b>sum</b></span>;
  <span style="color:#ff6060"><b>}</b></span>
<span style="color:#ff6060"><b>}</b></span>

<span style="color:#00ffff"><b>/**</b></span>
<span style="color:#00ffff"><b> * In PHP, program execution starts here. It isn&#8217;t neccessary to place functions </b></span>
<span style="color:#00ffff"><b> * ahead of the program, but it&#8217;s a good practice. In a larger application, these</b></span>
<span style="color:#00ffff"><b> * functions would be in seperate files. Again, since PHP is a procedural language,</b></span>
<span style="color:#00ffff"><b> * I could have simply included the contents of the main function here, rather than</b></span>
<span style="color:#00ffff"><b> * defining a function and calling it.</b></span>
<span style="color:#00ffff"><b> */</b></span>

main<span style="color:#ff6060"><b>()</b></span>;

<span style="color:#ff6060"><b>?&gt;</b></span>
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.php">fibonacci.php</a></p>
<p>You&#8217;ll notice a couple of things about this version of the Fibonacci program. Because it&#8217;s intended to be run on the command line, the first line tells the shell which interpretor to use. In addition, I have been careful here to return an exit code so that the shell knows that the program completed successfully.Stay tuned for our next installment, I&#8217;ll keep it a surprise, and even though everyone thinks it will be Ruby, I&#8217;m going to save that one for later.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php/feed</wfw:commentRss>
		</item>
		<item>
		<title>Fibonacci in C</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c#comments</comments>
		<pubDate>Thu, 03 Jan 2008 18:20:45 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
		
		<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c</guid>
		<description><![CDATA[Welcome to the first post of the Fibonacci project. In the following weeks, I am going to write this simple program in as many languages as possible, to highlight the similarities and differences between them. Today I will start with C, a classic language that is still in use today, and a good foundation for [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the first post of the Fibonacci project. In the following weeks, I am going to write this simple program in as many languages as possible, to highlight the similarities and differences between them. Today I will start with C, a classic language that is still in use today, and a good foundation for everything else that we will see. Rather than writing a lengthy post with each language, I&#8217;ll try to comment the code to point out the important things. Hopefully, this learning experiment will teach me something, and hopefully it will provide a handy resource for people to compare languages, even if it&#8217;s in a limited capacity.</p>
<p>So before I started, I gave myself the following &#8220;spec sheet&#8221; for writing the code:</p>
<blockquote><p>The purpose of the Fibonacci project is to create a simple programming example in as many languages as possible, to provide a clear demonstration of the differences and similarities between them.</p>
<p>The sample code should:</p>
<ul>
<li>Accept an input &#8220;x&#8221; (A value to progress up to)</li>
<li>Starting at 0 and 1, output the Fibonacci sequence up to &#8220;x&#8221; permutations.</li>
<li>There should be two functions, for clarity.
<ul>
<li>(main) Accepts the input and calls the fibonacci function.</li>
<li>(fibonacci) One that accepts the value of x and outputs the sequence.</li>
</ul>
</li>
</ul>
<p>The code should accept input from the console (stdin), with a prompt, and output to the console (stdout). This is so we keep everything simple. All of the code should be thoroughly commented.</p></blockquote>
<pre class="vci_code">
<span style="color:#00ffff"><b>/*</b></span><span style="color:#00ffff"><b>*</b></span>
<span style="color:#00ffff"><b> * You&#8217;ll notice that we need to include a header file that</b></span>
<span style="color:#00ffff"><b> * contains functions we need to use. Being a compiled language,</b></span>
<span style="color:#00ffff"><b> * it&#8217;s inefficient to include functions that aren&#8217;t needed.</b></span>
<span style="color:#00ffff"><b> * stdio.h contains functions for reading from and writing to the console</b></span>
<span style="color:#00ffff"><b> */</b></span>

<span style="color:#8080ff"><b>#include </b></span><span style="color:#ff40ff"><b>&lt;stdio.h&gt;</b></span>

<span style="color:#00ffff"><b>/*</b></span><span style="color:#00ffff"><b>*</b></span>
<span style="color:#00ffff"><b> * In C, the program executes the main function. You should also take note</b></span>
<span style="color:#00ffff"><b> * that we must declare a return type for the function. In this case, it&#8217;s</b></span>
<span style="color:#00ffff"><b> * an integer, and we return 0 to indicate successful completion of the </b></span>
<span style="color:#00ffff"><b> * program.</b></span>
<span style="color:#00ffff"><b> */</b></span>

<span style="color:#00ff00"><b>int</b></span> main ()
{
  <span style="color:#00ffff"><b>/*</b></span><span style="color:#00ffff"><b> Notice that we need to declare our variables, and their type */</b></span>

  <span style="color:#00ff00"><b>int</b></span> n;

  <span style="color:#00ffff"><b>/*</b></span><span style="color:#00ffff"><b> printf prints a formated string to the stdout */</b></span>

  printf(<span style="color:#ff40ff"><b>&quot;</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ff40ff"><b>How many numbers of the sequence would you like?</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ff40ff"><b>&quot;</b></span>);

  <span style="color:#00ffff"><b>/*</b></span><span style="color:#00ffff"><b> scanf reads a formated string from the stdin. We are expecting an integer here. */</b></span>

  scanf(<span style="color:#ff40ff"><b>&quot;</b></span><span style="color:#ff6060"><b>%d</b></span><span style="color:#ff40ff"><b>&quot;</b></span>,&amp;n);

  <span style="color:#00ffff"><b>/*</b></span><span style="color:#00ffff"><b> Here we call the fibonacci function */</b></span>

  fibonacci(n);

  <span style="color:#00ffff"><b>/*</b></span><span style="color:#00ffff"><b> Finally, return 0 */</b></span>

  <span style="color:#ffff00"><b>return</b></span> <span style="color:#ff40ff"><b>0</b></span>;
}

<span style="color:#00ffff"><b>/*</b></span><span style="color:#00ffff"><b>*</b></span>
<span style="color:#00ffff"><b> * This is the simple fibonacci sequence generator. Notice also, we</b></span>
<span style="color:#00ffff"><b> * declare the type of variable we expect to be passed to the function.</b></span>
<span style="color:#00ffff"><b> */</b></span>

<span style="color:#00ff00"><b>int</b></span> fibonacci(<span style="color:#00ff00"><b>int</b></span> n)
{
  <span style="color:#00ffff"><b>/*</b></span><span style="color:#00ffff"><b>*</b></span>
<span style="color:#00ffff"><b>   * Here we declare and set our variables.</b></span>
<span style="color:#00ffff"><b>   */</b></span>
  <span style="color:#00ff00"><b>int</b></span> a = <span style="color:#ff40ff"><b>0</b></span>;
  <span style="color:#00ff00"><b>int</b></span> b = <span style="color:#ff40ff"><b>1</b></span>;
  <span style="color:#00ff00"><b>int</b></span> sum;
  <span style="color:#00ff00"><b>int</b></span> i;

  <span style="color:#00ffff"><b>/*</b></span><span style="color:#00ffff"><b>*</b></span>
<span style="color:#00ffff"><b>   * Here is the standard for loop. This will step through, performing the code</b></span>
<span style="color:#00ffff"><b>   * inside the braces until i is equal to n.</b></span>
<span style="color:#00ffff"><b>   */</b></span>
  <span style="color:#ffff00"><b>for</b></span> (i=<span style="color:#ff40ff"><b>0</b></span>;i&lt;n;i++)
  {
    printf(<span style="color:#ff40ff"><b>&quot;</b></span><span style="color:#ff6060"><b>%d</b></span><span style="color:#ff6060"><b>\n</b></span><span style="color:#ff40ff"><b>&quot;</b></span>,a);
    sum = a + b;
    a = b;
    b = sum;
  }
  <span style="color:#ffff00"><b>return</b></span> <span style="color:#ff40ff"><b>0</b></span>;
}
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.3.2.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.c">fibonacci.c</a></p>
<p>So there&#8217;s my version of the Fibonacci sequence generator in C. I&#8217;m sure there are some problems with it, but the above code works. What would you change to make it better? Stay tuned for the next installment &#8230; PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
