<?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>nklein software &#187; perl</title>
	<atom:link href="http://nklein.com/tags/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Tue, 22 May 2012 03:48:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Programming When Clarity Counts</title>
		<link>http://nklein.com/2009/05/programming-when-clarity-counts/</link>
		<comments>http://nklein.com/2009/05/programming-when-clarity-counts/#comments</comments>
		<pubDate>Wed, 06 May 2009 04:10:25 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=285</guid>
		<description><![CDATA[In my previous post, I wrote some code in Perl because I wanted the code to be clear and obvious. Wait? What? Who uses Perl when they want clarity? Perl I admit: I was surprised myself. Seriously, I tried several other languages first. Let&#8217;s just focus on the first loop here. Here&#8217;s the Perl again [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://nklein.com/2009/05/how-to-make-a-weighted-random-choice/">my previous post</a>,  I wrote some code in Perl because I wanted the code to be clear and obvious.  Wait?  What?  Who uses Perl when they want clarity?</p>
<h3>Perl</h3>
<p>I admit:  I was surprised myself.  Seriously, I tried several other languages first.  Let&#8217;s just focus on the first loop here.  Here&#8217;s the Perl again for reference:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">@list_of_items</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$total</span> <span style="color: #339933;">+=</span> weight<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div>
<h3>Objective C</h3>
<p>The actual code that I was attempting to explain was in Objective C with the NeXTStep foundation classes.  I couldn&#8217;t imagine that anyone would want to try to understand it:</p>
<div class="codecolorer-container objc blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">double</span> total <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>;
<span style="color: #400080;">NSEnumerator</span><span style="color: #002200;">*</span> ee <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>itemList objectEnumerator<span style="color: #002200;">&#93;</span>;
Item<span style="color: #002200;">*</span> item;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span> item <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>Item<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>ee nextObject<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    total <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>item weight<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div>
<h3>C</h3>
<p>My first attempt for the article was to write it in C using an array of items.</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">double</span> total <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> ii<span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> ii <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> ii <span style="color: #339933;">&lt;</span> itemCount<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>ii <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    total <span style="color: #339933;">+=</span> weight<span style="color: #009900;">&#40;</span> itemArray<span style="color: #009900;">&#91;</span> ii <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div>
<p>That&#8217;s not too terrible.  There is, however, a great deal of syntax and code devoted to handling the array including an extra variable to track its length.  Beyond that, the loop counter is of little use to someone trying to understand the concept.</p>
<h3>C++</h3>
<p>My next go was C++.  My hope was that iterators would make the looping less painful.  C++ iterators, however, are the very model of pain.</p>
<div class="codecolorer-container cpp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">double</span> total <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">list</span><span style="color: #000080;">&lt;</span> Item<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> it<span style="color: #008080;">;</span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> it <span style="color: #000080;">=</span> itemList.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> itemList.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>it <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> weight<span style="color: #008000;">&#40;</span> <span style="color: #000040;">*</span>it <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div>
<p>This requires the reader to wade through a syntactic thicket to get the iterator into the picture at all.  It also requires a certain comfort with pointers that one wouldn&#8217;t have coming from another language.</p>
<h3>Java</h3>
<p>My next attempt was Java.  I hate Java.  It may not be terrible for this code with the new looping constructs that came in Java 1.5.  Alas, I quit coding it before I made it through this first loop.</p>
<h3>Lisp</h3>
<p>Of course, I wanted to code this in Lisp:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>total <span style="color: #66cc66;">&#40;</span>reduce #'+ list-of-items <span style="color: #66cc66;">:</span><span style="color: #555;">key</span> #'weight<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>That would be perfectly clear to someone who has used Lisp or Scheme.  Alas, not everyone has.</p>
<h3>So&#8230; Perl&#8230;</h3>
<p>If you want to write the Perl code, you have to understand the difference between scalars and arrays.  To read the code, however, you don&#8217;t have to grok that distinction to see what&#8217;s going on.  I didn&#8217;t try it in Python.  Maybe it would be clear to a wider audience.  I may have to try it next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/programming-when-clarity-counts/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>How to Make a Weighted Random Choice</title>
		<link>http://nklein.com/2009/05/how-to-make-a-weighted-random-choice/</link>
		<comments>http://nklein.com/2009/05/how-to-make-a-weighted-random-choice/#comments</comments>
		<pubDate>Wed, 06 May 2009 03:27:15 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=280</guid>
		<description><![CDATA[Today seemed like a dull day at the keyboard. I beat my head against Objective C a lot. And, I implemented a weighted random choice which I have done a thousand times before. Dull, right? I needed a weighted random choice in two different places, and I implemented it differently in each place. Weird, right? [...]]]></description>
			<content:encoded><![CDATA[<p>Today seemed like a dull day at the keyboard.  I beat my head against Objective C a lot.  And, I implemented a weighted random choice which I have done a thousand times before.  Dull, right?</p>
<p>I needed a weighted random choice in two different places, and I implemented it differently in each place.  Weird, right?  Even weirder, I think I did the right thing in each place.</p>
<h3>Picking a card from a deck</h3>
<p>In the first case, I needed to choose a flash card from a deck.  I want to favor cards that have given the player trouble in the past.  I zip through the list once to sum up the weights.  Then, I pick a random number greater or equal to zero and less than the sum of the weights.  Then, I run through the list again summing the weights until my running sum exceeds my random number.  (Here in Perl, um, for clarity?):</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">@list_of_items</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$total</span> <span style="color: #339933;">+=</span> weight<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$thresh</span> <span style="color: #339933;">=</span> <span style="color: #000066;">rand</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$total</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$found</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">@list_of_items</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$thresh</span> <span style="color: #339933;">-=</span> weight<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$thresh</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$found</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$item</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">last</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div>
<p>This requires that the <em>weight()</em> of a given item remains constant for the duration of the function.  The <em>weight()</em> can change between choices, however.  If the <em>weight()</em> will remain constant for large portions of the program then you can cache the total and only bother with the second half of the loop.</p>
<h3>Choosing a letter of the alphabet</h3>
<p>In a different portion of the program, I need to make random letter tiles.  I feel like there should be more <em>E</em>&#8216;s than <em>Q</em>&#8216;s.  I also want to leave a pathway to use the same artwork for different languages.  Because of all of this, I opted to have the weights to come in from a file.</p>
<p>My data file is <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> encoded.  I could, I suppose, do something like this (given the <a href="http://en.wikipedia.org/wiki/Letter_frequencies">English letter frequencies</a>):</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">{
    &quot;english&quot; : {
        &quot;A&quot;: 8.167,
        &quot;B&quot;: 1.492,
        &quot;C&quot;: 2.782,
        ...
    }
}</pre></div>
<p>But, that hurts to even think about.  Instead, I opted to round them off to integer proportions.  Then, I could just do this:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">{
    &quot;english&quot;: &quot;AAAAAAAABBCCC...&quot;
}</pre></div>
<p>Now, making a random choice simply involves selecting one character from the string.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/how-to-make-a-weighted-random-choice/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

