<?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>Dabbler &#187; Programming</title>
	<atom:link href="http://sethjust.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://sethjust.com</link>
	<description>If it ain&#039;t broke, fix it!</description>
	<lastBuildDate>Fri, 16 Jul 2010 00:19:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Sexy Alphabet Image Generator</title>
		<link>http://sethjust.com/2010/05/31/erotic-alphabet-image-generator/</link>
		<comments>http://sethjust.com/2010/05/31/erotic-alphabet-image-generator/#comments</comments>
		<pubDate>Mon, 31 May 2010 21:58:57 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[alphabet]]></category>
		<category><![CDATA[erotic]]></category>
		<category><![CDATA[font]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[sex]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=178</guid>
		<description><![CDATA[When my girlfriend came across this erotic alphabet, the first thing she wanted was to be able  to spell out various things with it. Add a pinch of python and php and the result is the Erotic Alphabet Image Generator. The source code of the script used to generate the images is available here.]]></description>
			<content:encoded><![CDATA[<p>When my girlfriend came across <a href="http://sexoteric.com/blog/index.php/__show_article/_a000018-001091.htm">this</a> erotic alphabet, the first thing she wanted was to be able  to spell out various things with it. Add a pinch of python and php and the result is the <a href="http://homepage.sethjust.com/erotic_alphabet/">Erotic Alphabet Image Generator</a>.</p>
<p>The source code of the script used to generate the images is available <a href="http://homepage.sethjust.com/files/erotic_alphabet_image_generator.txt">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2010/05/31/erotic-alphabet-image-generator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>M-Set update</title>
		<link>http://sethjust.com/2010/05/27/m-set-update/</link>
		<comments>http://sethjust.com/2010/05/27/m-set-update/#comments</comments>
		<pubDate>Thu, 27 May 2010 17:16:29 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[mandelbrot]]></category>
		<category><![CDATA[program]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=167</guid>
		<description><![CDATA[So, I know that I&#8217;ve been harping on this a bit, but I&#8217;ve re-written my M-Set renderer in C, so it&#8217;s faster, and I&#8217;ve added a few more command line flags to make it easier to use. Get the code here.]]></description>
			<content:encoded><![CDATA[<p><a href="http://homepage.sethjust.com/files/mandel_py.png"><img class="alignright" src="http://homepage.sethjust.com/files/mandel_py.png" alt="" width="351" height="268" /></a>So, I know that I&#8217;ve been harping on this a bit, but I&#8217;ve re-written my M-Set renderer in C, so it&#8217;s faster, and I&#8217;ve added a few more command line flags to make it easier to use.</p>
<p>Get the code <a href="http://sethjust.com/wp-content/uploads/2010/05/mandel.c">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2010/05/27/m-set-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More Mandelbrot</title>
		<link>http://sethjust.com/2009/12/19/more-mandelbrot/</link>
		<comments>http://sethjust.com/2009/12/19/more-mandelbrot/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 00:27:19 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=122</guid>
		<description><![CDATA[I just recently revisited the M-Set code from my Perl Snippets post. The code I had was pretty ugly, so I decided to rewrite it in Python. The result is not only a lot cleaner and easier to understand, but it&#8217;s also a lot faster: $ time python mandel.py &#62; \dev\null real 0m0.051s user 0m0.036s [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" title="Text Mandelbrot" src="http://homepage.sethjust.com/files/mandel_py.png" alt="" width="351" height="268" />I just recently revisited the M-Set code from my <a href="http://sethjust.com/2009/03/24/perl-snippets/">Perl Snippets</a> post. The code I had was pretty ugly, so I decided to rewrite it in Python. The result is not only a lot cleaner and easier to understand, but it&#8217;s also a lot faster:</p>
<pre>$ time python mandel.py &gt; \dev\null
real	0m0.051s
user	0m0.036s
sys	0m0.010s
$ time perl mandel.pl &gt; \dev\null
real	0m3.518s
user	0m3.463s
sys	0m0.029s
</pre>
<p>You can find the code <a href="http://homepage.sethjust.com/mandel.py">here</a>.</p>
<p>This script works well for zooms, as long as you stay below a few thousand iterations. The following picture was generated with x=-1.1887204, y=-0.3032472, width=0.01 and 150 iterations.</p>
<p style="text-align: center;"><img class="aligncenter" title="ASCII Mandelbrot Zoom" src="http://homepage.sethjust.com/files/mandel_py_zoom.png" alt="" width="351" height="268" /></p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2009/12/19/more-mandelbrot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl Snippets</title>
		<link>http://sethjust.com/2009/03/24/perl-snippets/</link>
		<comments>http://sethjust.com/2009/03/24/perl-snippets/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 02:39:05 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[japh]]></category>
		<category><![CDATA[m-set]]></category>
		<category><![CDATA[mandlebrot]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[obfuscation]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://sethjust.wordpress.com/?p=65</guid>
		<description><![CDATA[I&#8217;ve been getting into a mood lately that makes me fiddle around with fun Perl stuff, but sadly school&#8217;s picking up to the point that writing anything up isn&#8217;t going to happen. However, I have a couple short scripts that I&#8217;m just dying to share. Just Another Perl Hacker I figured that it was about [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been getting into a mood lately that makes me fiddle around with fun Perl stuff, but sadly school&#8217;s picking up to the point that writing anything up isn&#8217;t going to happen. However, I have a couple short scripts that I&#8217;m just dying to share.</p>
<h2>Just Another Perl Hacker</h2>
<p>I figured that it was about time in my hacking career (read: I was bored enough) that I should make a <a href="http://en.wikipedia.org/wiki/Just_another_Perl_hacker">japh script</a>. After a couple attempts I came up with this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code2'); return false;">View Code</a> PERL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p652"><td class="code" id="p65code2"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009999;">&lt;DATA&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">map</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">33</span><span style="color: #009900;">&#125;</span><span style="color: #000066;">split</span> <span style="color: #ff0000;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #0000ff;">$__</span><span style="color: #339933;">++;</span>
		<span style="color: #0000ff;">$_</span><span style="color: #339933;">||</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$__</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">19</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$__</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">__END__</span>
                   <span style="color: #339933;">/|</span>                        <span style="color: #339933;">|</span>\
      <span style="color: #339933;">!</span>            <span style="color: #339933;">;</span> <span style="color: #339933;">:</span>                        <span style="color: #339933;">:</span> <span style="color: #339933;">:</span>
                  <span style="color: #339933;">|</span> Y<span style="color: #339933;">,</span>                      <span style="color: #339933;">,</span>P <span style="color: #339933;">|</span>
     <span style="color: #339933;">!</span>             <span style="color: #339933;">|</span>  Yb<span style="color: #339933;">.</span>        __        <span style="color: #339933;">,</span>dP  <span style="color: #339933;">|</span>
                  l\  YMMb<span style="color: #339933;">,</span>_ _<span style="color: #339933;">,/</span>  \<span style="color: #339933;">,</span>_ _<span style="color: #339933;">,</span>dMMP  <span style="color: #339933;">/</span>f
  <span style="color: #339933;">!</span>                 j<span style="color: #339933;">;</span>  <span style="color: #ff0000;">`YMMP'  `</span><span style="color: #339933;">--</span><span style="color: #ff0000;">'  `YMMP'</span>  <span style="color: #339933;">;</span>j
                   <span style="color: #339933;">:</span> \   YP<span style="color: #ff0000;">`-._    _.-'YP   / ;
  !            !      <span style="color: #000099; font-weight: bold;">\ </span>`</span>\<span style="color: #339933;">,</span>  _<span style="color: #339933;">,</span><span style="color: #0000ff;">\_</span>    _<span style="color: #339933;">/,</span>_  <span style="color: #339933;">,/</span><span style="color: #ff0000;">' /
                     `,_,   \`o&gt;  &lt;o'</span><span style="color: #339933;">/</span>   <span style="color: #339933;">,</span>_<span style="color: #339933;">,!</span><span style="color: #ff0000;">'
                         `\            /'</span>
                           <span style="color: #339933;">|</span>  _    _  <span style="color: #339933;">|</span>
      <span style="color: #339933;">!</span>                     <span style="color: #339933;">/</span> 88b  d88 \
                           \ <span style="color: #ff0000;">`8P  Y8' /`</span><span style="color: #ff0000;">`-.
             !               `</span>\<span style="color: #339933;">,</span>    <span style="color: #339933;">,/</span>      \
                             _<span style="color: #ff0000;">`----'        <span style="color: #000099; font-weight: bold;">\</span>
                  !        _.'/ '          `</span><span style="color: #339933;">,</span> \
                        <span style="color: #339933;">.</span><span style="color: #ff0000;">'_,'</span>_<span style="color: #339933;">...</span>_          \ <span style="color: #339933;">;</span>
       <span style="color: #339933;">!</span>               <span style="color: #339933;">.</span><span style="color: #ff0000;">',/.dMMMMMMb. |      /  \
                    .'</span><span style="color: #339933;">./.</span>MMMMMMMMMMM <span style="color: #339933;">;</span> <span style="color: #339933;">!</span>    <span style="color: #339933;">/</span>    <span style="color: #339933;">;</span>
                   <span style="color: #009966; font-style: italic;">/ /</span><span style="color: #339933;">.</span>MMMMMMMMMMMMM<span style="color: #339933;">/</span>     <span style="color: #339933;">/</span>     <span style="color: #339933;">;</span>
                  <span style="color: #339933;">|</span> <span style="color: #339933;">;</span>dMMMMMMMMMMM<span style="color: #339933;">!</span>P<span style="color: #339933;">/</span><span style="color: #ff0000;">'   ,/'</span>   <span style="color: #339933;">!</span>    <span style="color: #339933;">;</span>
                  <span style="color: #339933;">|/</span>dMMMMMMMMMP<span style="color: #ff0000;">'     .'</span>          <span style="color: #339933;">|</span>
   <span style="color: #339933;">!</span>               <span style="color: #339933;">;</span>dMMMMMMMMM<span style="color: #339933;">|</span>     _<span style="color: #339933;">/</span>            <span style="color: #339933;">|</span>
                 <span style="color: #339933;">;</span>dMMMMMMMMMMb_<span style="color: #339933;">|</span>_<span style="color: #339933;">!|</span> <span style="color: #339933;">/;</span>            <span style="color: #339933;">;</span>\<span style="color: #339933;">,</span>
                 dMMMMMMMMMMMM<span style="color: #ff0000;">`M`</span>M<span style="color: #ff0000;">`;.---..      '   `</span>\<span style="color: #339933;">,</span>
                <span style="color: #339933;">:</span>M<span style="color: #339933;">!</span>MMMMMMMMMMMMMMMP<span style="color: #339933;">/</span><span style="color: #ff0000;">'     `\            \
               /|MMMMMMMMMMMMMMP/          \    _!_      `.
  !            / |MMMMMMMMMMMMMM|            |.-'</span>  <span style="color: #ff0000;">`-._    <span style="color: #000099; font-weight: bold;">\ </span>        .'<span style="color: #000099; font-weight: bold;">\</span>
             ;!  ;MMMMMMMMMMMMMM|            :         `</span><span style="color: #339933;">-.</span>_ <span style="color: #ff0000;">`-.     /  _<span style="color: #000099; font-weight: bold;">\</span>
             |   ;!MMMMMMMMMMMMM;            |             `</span><span style="color: #339933;">-.</span> <span style="color: #ff0000;">`-.._.'/
              <span style="color: #000099; font-weight: bold;">\ </span>  `</span><span style="color: #0000ff;">\YMMMMM</span><span style="color: #339933;">!</span>MMMP<span style="color: #339933;">/</span><span style="color: #ff0000;">'\           |                `~~----'</span>
               <span style="color: #ff0000;">`-._, `</span><span style="color: #ff0000;">`YMMMP'_.-'<span style="color: #000099; font-weight: bold;">\ </span>        ! ;_
                 |      /         `</span><span style="color: #339933;">-.,</span>_<span style="color: #339933;">./</span>     <span style="color: #cc66cc;">7</span>
                _<span style="color: #339933;">/</span>    <span style="color: #ff0000;">`<span style="color: #000099; font-weight: bold;">\,</span>    !        /      _/
             ,-' ,  ,   /         _.'      /
            (_(_(__(__.'       !,-'        /
                              (__(__(_(_/'</span></pre></td></tr></table></div>

<p><a href="http://people.reed.edu/~justs/japh.pl">Download</a>.</p>
<p>I&#8217;ll let you go ahead and figure it out on your own. It&#8217;s not super-hard, but it&#8217;s fun.</p>
<h2>Mandelbrot</h2>
<p>In another fit of boredom I decided that it was finally time to create a mandelbrot set renderer. I originally tried to make one of these in basic, long before I had the math to do so. I was proud that I got the real axis to render, and figured it was time to complexify it. To keep things simple I decided to make it render an ASCII-art version of the set that would fit in a terminal window. The output looks like this:</p>
<pre>                        ...............................:::::oo@@@@o::::..........
                      ...............................::::::O@@@@@@@@o:::::.......
                     .............................::::::::oO@@@@@@@@o::::::::....
                   ...........................::::OOO8ooO@O88@@@@@@8@O8o::::Oo:..
                  .......................:::::::::o8@@@@@@@@@@@@@@@@@@@@OO@@@@::.
                 ...................:::::::::::::oOO@@@@@@@@@@@@@@@@@@@@@@@@@o:::
                ................:::::::::::::::o@@@@@@@@@@@@@@@@@@@@@@@@@@@@Oo:::
               ..............::::@oo::oOoo:::ooo@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8o:
               ...........:::::::oO@@@O@@8@OooO8@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@O::
              ..........:::::::::oO@@@@@@@@@@88@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@::
              ........:::::::8ooO8@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@o::
              .:::::::::::::oO@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@o:::
              @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@Oo::::
              .:::::::::::::oO@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@o:::
              ........:::::::8ooO8@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@o::
              ..........:::::::::oO@@@@@@@@@@88@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@::
               ...........:::::::oO@@@O@@8@OooO8@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@O::
               ..............::::@oo::oOoo:::ooo@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8o:
                ................:::::::::::::::o@@@@@@@@@@@@@@@@@@@@@@@@@@@@Oo:::
                 ...................:::::::::::::oOO@@@@@@@@@@@@@@@@@@@@@@@@@o:::
                  .......................:::::::::o8@@@@@@@@@@@@@@@@@@@@OO@@@@::.
                   ...........................::::OOO8ooO@O88@@@@@@8@O8o::::Oo:..
                     .............................::::::::oO@@@@@@@@o::::::::....
                      ...............................::::::O@@@@@@@@o:::::.......
                        ...............................:::::oo@@@@o::::..........</pre>
<p>The code&#8217;s far from polished and not what I like to publish, but it&#8217;s a fun thing to look at and offers you some neat abilities to poke things around and fix some pesky problems that just need clear thinking applied to them. It&#8217;s available <a href="http://people.reed.edu/~justs/mandel.pl">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2009/03/24/perl-snippets/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Genetic Algorithms in Perl</title>
		<link>http://sethjust.com/2008/12/11/genetic-algorithms-in-perl/</link>
		<comments>http://sethjust.com/2008/12/11/genetic-algorithms-in-perl/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 09:20:19 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[evolution]]></category>
		<category><![CDATA[genetic]]></category>
		<category><![CDATA[genetic algorithm]]></category>

		<guid isPermaLink="false">http://sethjust.wordpress.com/?p=38</guid>
		<description><![CDATA[Inspired by recent genetic algorithms floating around, I decided to try my hand at implementing one in perl. I&#8217;d thought for a long time that it would be quite difficult, but really it&#8217;s quite easy. My biggest hangup was dealing with data structures, but once I did that, it turns out that all you really [...]]]></description>
			<content:encoded><![CDATA[<p>Inspired by <a href="http://hackaday.com/2008/12/08/genetic-programming/">recent</a> <a href="http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/">genetic</a> <a href="http://www.wreck.devisland.net/ga/">algorithms</a> floating around, I decided to try my hand at implementing one in perl. I&#8217;d thought for a long time that it would be quite difficult, but really it&#8217;s quite easy. My biggest hangup was dealing with data structures, but once I did that, it turns out that all you really need is a few functions:</p>
<ul>
<li>A fitness function, that determines which individuals are most fit to reproduce</li>
<li>A mutate function, that will add random chance into each generation</li>
<li>A breed function that allows the best individuals to reproduce.</li>
</ul>
<p>I ended up implementing a very simple algorithm, but it&#8217;s fairly fast and very generic – it can be easily adapted to just about any task. Sadly, I have no fascinating application just yet, but if I stumble across one, I&#8217;ll be sure to post about it.</p>
<p>After the jump, I&#8217;ll put up some of the code I used and a link to the script, all for your viewing pleasure.<br />
<span id="more-38"></span></p>
<p>The heart of the algorithm is the mutate and breed functions:</p>
<p>[sourcecode language='php']sub mutate{     # Mutates each allele of an individual by a random, weighted amount,<br />
                #    controlled by certain parameters. It re-initializes if the allele<br />
                #    is zero, and takes abs()<br />
    my $ind = shift;</p>
<p>    for (@$ind) {<br />
        $_ = eval($init_string) unless ($_);<br />
        $_ += (rand(2) &#8211; 1)*($mut_weight*($_)+$mut_offset);<br />
        $_ = -$_ unless ($_>0);<br />
    }</p>
<p>    return 0;<br />
}</p>
<p>sub breed{      # Breeds each individual with the top 20 percent of the population randomly<br />
                #    by averaging each allele of the individual with the corresponding allele<br />
                #    of a random individual in the top quintile. Note that this means that<br />
                #    each individual breeds with, at most (and ideally), as many individuals<br />
                #    as it has alleles, which breaks down the parent / child model slightly.<br />
    my $indvs = shift;<br />
    my @list;<br />
    ($minimize) || (@list = sort {fitness($b)  fitness($a)} @$indvs);  # Sort asc. vs. desc.<br />
    ($minimize) &#038;&#038; (@list = sort {fitness($a)  fitness($b)} @$indvs);</p>
<p>    @list = @list[1..(int(scalar(@list)/5)+1)];</p>
<p>    for (@$indvs) {              # Iterate through individuals<br />
        my $ind_ref = $_;<br />
        my $i = 0;<br />
        for (@$ind_ref) {        # Iterate through alleles<br />
            $_ = ((@list[int(rand(length(@list)-1))])->[$i] + $_)/2;      # Average given allele with<br />
                                                                          #    a random, fit,<br />
                                                                          #    corresponding allele<br />
            $_ = (rand(1)<$reset_prob?eval($init_string):$_);             # Re-init the allele some<br />
                                                                          #    percent of the time<br />
            $i++;<br />
        }<br />
    }</p>
<p>    return 0;<br />
}[/sourcecode]</p>
<p>Between the comments and the code, it should be pretty clear what's going on in here, although some of the data structures are kinda hard to get from this section. As I explain in the <a href="http://people.reed.edu/~justs/genes.pl">code</a>:</p>
<blockquote><p>A randomized population is created. An array holds references to the specified number of &#8220;individuals&#8221;. Each individual is an array of alleles (scalar values) that are chosen by evaluating a specified string. These are currently positive real numbers.</p></blockquote>
<p>These references (or references to the population array) are passed as arguments to the functions each iteration.</p>
<p>If you&#8217;re interested, be sure to check out the <a href="http://people.reed.edu/~justs/genes.pl">code</a> and please give me some feedback!</p>
<p>UPDATE: Ivan in the comments made the recommendation of using an anonymous function to intialize individuals. I made that change he recommended and updated the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2008/12/11/genetic-algorithms-in-perl/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Fast and Simple Stock Quotes Using Perl</title>
		<link>http://sethjust.com/2008/10/23/fast-and-simple-stock-quotes-using-perl/</link>
		<comments>http://sethjust.com/2008/10/23/fast-and-simple-stock-quotes-using-perl/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 06:18:05 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[CPAN]]></category>
		<category><![CDATA[exchange]]></category>
		<category><![CDATA[finance]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[stocks]]></category>
		<category><![CDATA[ticker]]></category>

		<guid isPermaLink="false">http://sethjust.wordpress.com/?p=21</guid>
		<description><![CDATA[One of the things that makes perl so powerful and fascinating is the huge number of modules that are available online, especially through the CPAN repository. Today I stumbled upon one called Finance::Quote, which does one thing, very simply: it retrieves stock (or mutual fund) quotes. You feed it a ticker symbol and it gives [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that makes perl so powerful and fascinating is the huge number of modules that are available online, especially through the CPAN repository. Today I stumbled upon one called Finance::Quote, which does one thing, very simply: it retrieves stock (or mutual fund) quotes. You feed it a ticker symbol and it gives back a hash with all sorts of information, but most importantly, the price. I&#8217;m going to show how to use this to create a command line tool that will grab an up-to-the minute stock quote for any ticker symbol you give it.</p>
<p><span id="more-21"></span><br />
To start, installing Finance::Quote is a breeze. As long as you have CPAN installed, just type at the command line:</p>
<pre>$perl -MCPAN -e shell
&gt;install Finance::Quote</pre>
<p>From there it&#8217;s easy to get start, it&#8217;s just a matter of importing the module and creating and instance. This means the beginning of our script will look like:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p21code6'); return false;">View Code</a> PERL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p216"><td class="code" id="p21code6"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># import module</span>
<span style="color: #000000; font-weight: bold;">use</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># create object</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The instance that we created has one main method: fetch(). It takes a list of arguments, with the first being a the exchange to look for quotes on, and the remaining being a list or array of ticker symbols. The <a href="http://search.cpan.org/~ecocode/Finance-Quote-1.14/lib/Finance/Quote.pm">Finance::Quote documentation</a> provides the following list of valid exchanges:</p>
<pre>australia           Australan Stock Exchange
dwsfunds            Deutsche Bank Gruppe funds
fidelity            Fidelity Investments
tiaacref            TIAA-CREF
troweprice          T. Rowe Price
europe              European Markets
canada              Canadian Markets
usa                 USA Markets
nyse                New York Stock Exchange
nasdaq              NASDAQ
uk_unit_trusts      UK Unit Trusts
vanguard            Vanguard Investments
vwd                 Vereinigte Wirtschaftsdienste GmbH</pre>
<p>For the purposes of this script, I&#8217;m going to stick to &#8220;usa&#8221; because it covers both NYSE and NASDAQ, and those are the stocks I&#8217;m interested in.<br />
Fetch() returns a two-dimensional hash of results. The first index (or dimension) is the ticker symbol of the stock and the second is the label for the specific piece of information. For now, the labels that we&#8217;re interested are the name of the company (&#8216;name&#8217;) and the price (&#8216;price&#8217;). A full listing of labels is available in the <a href="http://search.cpan.org/~ecocode/Finance-Quote-1.14/lib/Finance/Quote.pm#LABELS">Finance::Quote documentation</a>.<br />
Using this information we can now build a simple script to fetch a stock quote:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p21code7'); return false;">View Code</a> PERL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p217"><td class="code" id="p21code7"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># import module</span>
<span style="color: #000000; font-weight: bold;">use</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># create object</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># retrieve stock quote</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'usa'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'GOOG'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># print price</span>
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$data</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'GOOG'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'price'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;n&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This script, although it does what it&#8217;s supposed to, isn&#8217;t very pretty. By adding a little code to read ticker symbols from the command line we can check on multiple stocks at the same time, as well as remove the need to hard-code the ticker symbols. We can also make it produce much prettier results by adding more information about the company and add some basic error checking, which yields the following script:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p21code8'); return false;">View Code</a> PERL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p218"><td class="code" id="p21code8"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># import module</span>
<span style="color: #000000; font-weight: bold;">use</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># create object</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#print usage information</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Usage: $0 ticker1, ticker2, ... , tickerNn&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># get stock symbols from the command line and</span>
<span style="color: #666666; font-style: italic;"># format them correctly (uppercase)</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #0000ff;">$_</span> <span style="color: #339933;">=</span> <span style="color: #000066;">uc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># retrieve stock quote</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'usa'</span><span style="color: #339933;">,</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># print result for each stock</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$data</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'success'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>		<span style="color: #666666; font-style: italic;"># if getting the quote succeeded</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$data</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'name'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;"># build a report</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$price</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$data</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'price'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$message</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$message</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">$name</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">' ('</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$_</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">')'</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$message</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">' '</span> x<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">25</span> <span style="color: #339933;">-</span> <span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$message</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">&quot;$$pricen&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000066;">print</span> <span style="color: #0000ff;">$message</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Failed to retrieve quote for $_: $data{$_, 'errormsg'}n&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is very simple, but it does its job well:</p>
<pre>$ quote goog msft aapl dell java
GOOGLE (GOOG)            $352.32
MICROSOFT CP (MSFT)      $22.32
APPLE INC (AAPL)         $98.23
DELL INC (DELL)          $11.99
SUN MICROSYSTEMS (JAVA)  $4.54</pre>
<p><a href="http://people.reed.edu/~justs/quote.pl">Download the script here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2008/10/23/fast-and-simple-stock-quotes-using-perl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
