<?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>Storm Development Blog &#187; Code Snippets</title>
	<atom:link href="http://demo.storm-consultancy.com/blog/development/category/code-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://demo.storm-consultancy.com/blog/development</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Fri, 26 Feb 2010 13:36:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generic HashTable in C#</title>
		<link>http://demo.storm-consultancy.com/blog/development/code-snippets/generic-hashtable-in-c/</link>
		<comments>http://demo.storm-consultancy.com/blog/development/code-snippets/generic-hashtable-in-c/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 18:13:34 +0000</pubDate>
		<dc:creator>Adam Pope</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.izonedesign.co.uk/blog/?p=81</guid>
		<description><![CDATA[I had a bit of a brain fail earlier and couldn't remember how to create a &#60;strong&#62;HashTable&#60;/strong&#62; with generic type arguments. There was a Hashtable and a HashSet&#60;&#62; but neither were what I was looking for.]]></description>
			<content:encoded><![CDATA[<p>I had a bit of a brain fail earlier and couldn&#8217;t remember how to create a <strong>HashTable</strong> with generic type arguments. There was a Hashtable and a HashSet&lt;&gt; but neither were what I was looking for.  The answer was a <strong>Dictionary</strong>:</p>
<pre class="brush: csharp"> var myHashTable = new Dictionary&lt;TKey, TValue&gt;
</pre>
<p>Where TKey is the type of your key (possibly an int to represent the Id of your object or string for a username) and TValue is the type of the value. I hope that saves you some time!</p>


<div class="taggington"><p><!--<img src="/blog/wp-content/themes/stormblog/images/tag.gif" alt="tag"/>&nbsp; -->Tagged in: &nbsp;<a href="http://demo.storm-consultancy.com/blog/development/tag/c/" rel="tag">C#</a></p></div><div class="relatedpostplugin">
<h3>You may also be interested in...</h3>
<div class="relatedlinks">
<a href="http://demo.storm-consultancy.com/blog/development/code-snippets/scheduling-a-process-with-cron-and-crontab/" rel="bookmark">Scheduling a Process with Cron and Cront...</a><!-- (4.1625)--><br /> 
<a href="http://demo.storm-consultancy.com/blog/development/code-snippets/perl-generating-a-random-number-with-rand/" rel="bookmark">Perl: Generating a Random Number with Ra...</a><!-- (3.4016)--></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://demo.storm-consultancy.com/blog/development/code-snippets/generic-hashtable-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl: Sorting an Array of Hashes</title>
		<link>http://demo.storm-consultancy.com/blog/development/code-snippets/perl-sorting-an-array-of-hashes/</link>
		<comments>http://demo.storm-consultancy.com/blog/development/code-snippets/perl-sorting-an-array-of-hashes/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 09:11:11 +0000</pubDate>
		<dc:creator>Adam Pope</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.izonedesign.co.uk/blog/?p=76</guid>
		<description><![CDATA[I recently had a scenario where I had an array of hashes.  Each hash represented a row of data for a table and I wanted to sort the whole array by the value of one element of the hash. As you&#8217;d expect, Perl has a very nice answer to the problem.

Sorting by a Hash [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had a scenario where I had an array of hashes.  Each hash represented a row of data for a table and I wanted to sort the whole array by the value of one element of the hash. As you&#8217;d expect, Perl has a very nice answer to the problem.</p>
<p><span id="more-76"></span></p>
<h2>Sorting by a Hash Key</h2>
<p>The answer is really quite simple and very elegant.  Let&#8217;s build up an example.</p>
<pre class="brush: perl">my @data;
push @data, { name =&gt; "Item A", price =&gt; 9.99 };
push @data, { name =&gt; "Item B", price =&gt; 4.99 };
push @data, { name =&gt; "Item C", price =&gt; 7.5};</pre>
<p>We now have an array with 3 hashes in.  Suppose we want to sort by price, how do we do it?</p>
<pre class="brush: perl">my @sorted =  sort { $a-&gt;{price} &lt;=&gt; $b-&gt;{price} } @data;</pre>
<p>Perl&#8217;s built in <a title="Perl Sort" href="http://perldoc.perl.org/functions/sort.html" target="_blank">sort</a> function allows us to specify a custom sort order.  Within the curly braces Perl gives us 2 variables, $a and $b, which reference 2 items to compare.  In our case, these are hash references so we can access the elements of the hash and sort by any key we want.  A description of the &lt;=&gt; operator can be found on <a title="comparison" href="http://www.perlfect.com/articles/sorting.shtml" target="_blank">Perlfect</a>.  Suppose we want to sort in reverse order?</p>
<pre class="brush: perl">my @sorted =  sort { $b-&gt;{price} &lt;=&gt; $a-&gt;{price} } @data;</pre>
<p>Simple.  You can test this by adding the following line to the bottom of the script.</p>
<pre class="brush: perl">print join "n", map {$_-&gt;{name}." - ".$_-&gt;{price}} @sorted;</pre>
<p>Which gives us:</p>
<pre class="brush: perl">Item B - 4.99
Item C - 7.5
Item A - 9.99</pre>


<div class="taggington"><p><!--<img src="/blog/wp-content/themes/stormblog/images/tag.gif" alt="tag"/>&nbsp; -->Tagged in: &nbsp;<a href="http://demo.storm-consultancy.com/blog/development/tag/perl/" rel="tag">Perl</a>&nbsp;&nbsp; <a href="http://demo.storm-consultancy.com/blog/development/tag/tutorials/" rel="tag">Tutorials</a></p></div><div class="relatedpostplugin">
<h3>You may also be interested in...</h3>
<div class="relatedlinks">
<a href="http://demo.storm-consultancy.com/blog/development/tutorials/perl-basic-unit-testing/" rel="bookmark">Perl: Basic Unit Testing</a><!-- (7.77092)--><br /> 
<a href="http://demo.storm-consultancy.com/blog/development/code-snippets/perl-generating-a-random-number-with-rand/" rel="bookmark">Perl: Generating a Random Number with Ra...</a><!-- (6.75457)--><br /> 
<a href="http://demo.storm-consultancy.com/blog/development/code-snippets/perl-print-internal-server-errors-to-the-browser/" rel="bookmark">Perl: Print Internal Server Errors to th...</a><!-- (6.05413)--></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://demo.storm-consultancy.com/blog/development/code-snippets/perl-sorting-an-array-of-hashes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Perl: Print Internal Server Errors to the Browser</title>
		<link>http://demo.storm-consultancy.com/blog/development/code-snippets/perl-print-internal-server-errors-to-the-browser/</link>
		<comments>http://demo.storm-consultancy.com/blog/development/code-snippets/perl-print-internal-server-errors-to-the-browser/#comments</comments>
		<pubDate>Fri, 18 Apr 2008 20:05:31 +0000</pubDate>
		<dc:creator>Adam Pope</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.izonedesign.co.uk/blog/?p=48</guid>
		<description><![CDATA[So, you&#8217;re a Perl programmer developing a CGI web application, chances are you&#8217;re going to make some mistakes and need to debug your code.  By default, when a Perl program fails to compile or encounters a runtime error you will see your servers default HTTP 500 error message.

Internal Server Error
 The server encountered an [...]]]></description>
			<content:encoded><![CDATA[<p>So, you&#8217;re a Perl programmer developing a CGI web application, chances are you&#8217;re going to make some mistakes and need to debug your code.  By default, when a Perl program fails to compile or encounters a runtime error you will see your servers default HTTP 500 error message.</p>
<p><span id="more-48"></span></p>
<h2>Internal Server Error</h2>
<p><em> The server encountered an internal error or misconfiguration and was unable to complete your request.</em></p>
<p>Not terribly helpful or enlightening &#8211; so what do you do?  Well, you can sit with a terminal window open tailing the http error log as you refresh the page to find out:</p>
<pre>-$ tail -f error.log</pre>
<p>But that&#8217;s a slow and painful experience.  What we really want is to print the error message straight to the browser whilst we are developing.  In steps the <a href="http://perldoc.perl.org/CGI/Carp.html" target="_blank">CGI::Carp</a> module:</p>
<pre class="brush: perl">#!/usr/bin/perl
use strict;
use warnings
use CGI::Carp qw( fatalsToBrowser );

print "Content-type: text/html/n/n";
print "&lt;h1&gt;Hello World &lt;/h1
print "I wont work";</pre>
<p>The <code>fatalsToBrowser</code> function will take over and print the error message straight to the browser window:</p>
<h3>Software error:</h3>
<pre>syntax error at dummy.cgi line 9, near "print
Execution of dummy.cgi aborted due to compilation errors.</pre>
<p>This is a much more useful error message and will help with debugging.  Remember to remove the <code>fatalsToBrowser</code> call before releasing your application to production else you could potentially expose sensitive details to hackers.</p>


<div class="taggington"><p><!--<img src="/blog/wp-content/themes/stormblog/images/tag.gif" alt="tag"/>&nbsp; -->Tagged in: &nbsp;<a href="http://demo.storm-consultancy.com/blog/development/tag/perl/" rel="tag">Perl</a>&nbsp;&nbsp; <a href="http://demo.storm-consultancy.com/blog/development/tag/tutorials/" rel="tag">Tutorials</a></p></div><div class="relatedpostplugin">
<h3>You may also be interested in...</h3>
<div class="relatedlinks">
<a href="http://demo.storm-consultancy.com/blog/development/code-snippets/perl-generating-a-random-number-with-rand/" rel="bookmark">Perl: Generating a Random Number with Ra...</a><!-- (7.45411)--><br /> 
<a href="http://demo.storm-consultancy.com/blog/development/code-snippets/perl-sorting-an-array-of-hashes/" rel="bookmark">Perl: Sorting an Array of Hashes</a><!-- (7.05487)--><br /> 
<a href="http://demo.storm-consultancy.com/blog/development/tips-tricks/no-rule-to-make-target-systemlibraryperl588darwin-thread-multi-2levelcoreconfigh-needed-by-makefile/" rel="bookmark">No rule to make target `/System/Library/...</a><!-- (6.70854)--></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://demo.storm-consultancy.com/blog/development/code-snippets/perl-print-internal-server-errors-to-the-browser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scheduling a Process with Cron and Crontab</title>
		<link>http://demo.storm-consultancy.com/blog/development/code-snippets/scheduling-a-process-with-cron-and-crontab/</link>
		<comments>http://demo.storm-consultancy.com/blog/development/code-snippets/scheduling-a-process-with-cron-and-crontab/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 20:03:23 +0000</pubDate>
		<dc:creator>Adam Pope</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Cron]]></category>
		<category><![CDATA[Sysadmin]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.izonedesign.co.uk/blog/?p=39</guid>
		<description><![CDATA[Cron is a time-based scheduling service in Unix and Unix-like operating systems (including Linux, FreeBSD and Mac OS X).  It makes use of a configuration table called crontab that gives details of the commands to run and the schedule to execute them.  This guide covers the basics of using cron to setup cronjobs [...]]]></description>
			<content:encoded><![CDATA[<p><span>Cron is a time-based scheduling service in Unix and Unix-like operating systems </span>(including Linux, FreeBSD and Mac OS X)<span>.  It makes use of a configuration table called crontab that gives details of the commands to run and the schedule to execute them.  This guide covers the basics of using cron to setup cronjobs with crontab.</span></p>
<p><span id="more-39"></span></p>
<p>You can read the <a title="crontab manual" href="http://www.hmug.org/man/5/crontab.php" target="_blank">manual pages</a> for cron or type &#8216;man crontab&#8217; at the command line for more information.</p>
<h2>Using crontab</h2>
<p>To view the current state of a crontab you need to specify the <code>-l</code> option.  You can view another users crontab by specifying <code>-u username</code> (you&#8217;ll need to be root to do this):</p>
<p><code>user$ crontab -l [-u username]</code></p>
<p>To edit the state of a crontab you need to use the <code>-e</code> flag:</p>
<p><code>user$ crontab -e [-u username]</code></p>
<h2>The Format of Crontab</h2>
<p>When you edit a user&#8217;s crontab for the first time you will presented with an empty file &#8211; this is a daunting prospect for the cron newbie!  Here&#8217;s a minimal example crontab to get you started:</p>
<pre>MAILTO=adam@example.com</pre>
<pre>#minute hour monthday month weekday command
30 12 * * * echo "hello world!"</pre>
<p>The first line sets the MAILTO environment variable.  Any output that would normally have been printed to STDOUT by the processes will be emailed to this address.  We then have a comment so we can remember the order of the variables.  Finally, the 3rd line defines what to run and when.  In this case, at 12:30 every day of every month we will echo &#8220;Hello World!&#8221;.</p>
<h2>Scheduling a Task</h2>
<p>Crontab gives us a very flexible syntax for defining when our commands will run.  For each of the 5 periods we can use any of the following notations:</p>
<ul>
<li>* &#8211; every value</li>
<li># &#8211; one given value e.g. 5</li>
<li>*/# &#8211; every value that divides by the number e.g. */5 will run at 5,10,15,20&#8230;</li>
<li>#,#,# &#8211; we can specify a comma separated list of values e.g. 5,20,24</li>
<li>#-# &#8211; we can also use ranges of values e.g. 2-5</li>
</ul>
<p>For each field the following values are valid:</p>
<ul>
<li> minute        =  0-59</li>
<li>hour          =  0-23</li>
<li>monthday =  1-31</li>
<li>month         =  1-12</li>
<li>weekday   =  0-7 (0 or 7 is Sun)</li>
</ul>
<h2>Installing a Crontab</h2>
<p>When you save and exit from editing the crontab the system will parse the contents and schedule the tasks to run.</p>
<h2>Further Options</h2>
<p>As well as setting the MAILTO flag, you can set any environment variables you need within the crontab file.  For example, if you need to add to the path:</p>
<pre>PATH=$PATH:/my/new/dir</pre>
<p>These declarations are read in order and can be redefined to create sections within the crontab.  For example, if you have 1 job that needs to email sales@example.com and another that needs to email sysadmin@example.com, then you can use the following file structure:</p>
<pre>MAILTO=sales@example.com</pre>
<pre>#minute hour monthday month weekday command
30 12 * * * /path/to/my/command
MAILTO=sales@example.com
1 * 1 * * /path/to/my/othercommand</pre>
<p><!--amm_getMediaID('amm_post_product',24)--><br />
<!--amm_getMediaID('amm_post_product',23)--></p>


<div class="taggington"><p><!--<img src="/blog/wp-content/themes/stormblog/images/tag.gif" alt="tag"/>&nbsp; -->Tagged in: &nbsp;<a href="http://demo.storm-consultancy.com/blog/development/tag/cron/" rel="tag">Cron</a>&nbsp;&nbsp; <a href="http://demo.storm-consultancy.com/blog/development/tag/sysadmin/" rel="tag">Sysadmin</a>&nbsp;&nbsp; <a href="http://demo.storm-consultancy.com/blog/development/tag/tutorials/" rel="tag">Tutorials</a></p></div><div class="relatedpostplugin">
<h3>You may also be interested in...</h3>
<div class="relatedlinks">
<a href="http://demo.storm-consultancy.com/blog/development/tutorials/taking-a-screenshot-in-mac-os-x/" rel="bookmark">Taking a screenshot in Mac OS X</a><!-- (6.05666)--><br /> 
<a href="http://demo.storm-consultancy.com/blog/development/tutorials/burn-iso-disc-images-to-cd-or-dvd-with-mac-os-x/" rel="bookmark">Burn ISO disc images to CD or DVD with M...</a><!-- (3.44183)--><br /> 
<a href="http://demo.storm-consultancy.com/blog/development/tutorials/sine-wave-frequency-sweep-with-audacity/" rel="bookmark">Sine Wave Frequency Sweep with Audacity</a><!-- (3.12563)--></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://demo.storm-consultancy.com/blog/development/code-snippets/scheduling-a-process-with-cron-and-crontab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl: Generating a Random Number with Rand()</title>
		<link>http://demo.storm-consultancy.com/blog/development/code-snippets/perl-generating-a-random-number-with-rand/</link>
		<comments>http://demo.storm-consultancy.com/blog/development/code-snippets/perl-generating-a-random-number-with-rand/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 18:06:56 +0000</pubDate>
		<dc:creator>Adam Pope</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.izonedesign.co.uk/blog/?p=41</guid>
		<description><![CDATA[Generating a psuedo random number is common programming task.  This tutorial will show you how to this in Perl using the rand() function.

The Rand() Function
The simplest use of rand(), with no parameters ( This is analogous to calling rand(1) ), generates a double precision number between 0 and 1.
#!/usr/bin/perl
use strict
use warnings;

my $rndnum = rand()
print [...]]]></description>
			<content:encoded><![CDATA[<p>Generating a psuedo random number is common programming task.  This tutorial will show you how to this in Perl using the <a title="rand()" href="http://perldoc.perl.org/functions/rand.html" target="_blank"><code>rand()</code></a> function.</p>
<p><span id="more-41"></span></p>
<h2>The Rand() Function</h2>
<p>The simplest use of <code>rand()</code>, with no parameters ( This is analogous to calling <code>rand(1)</code> ), generates a double precision number between 0 and 1.</p>
<pre class="brush: perl">#!/usr/bin/perl
use strict
use warnings;

my $rndnum = rand()
print $rndnum;</pre>
<p>Will give you a result like: 0.186135607379587</p>
<h2>Rand() with a Range</h2>
<p>You can generate a bigger random number if you call <code>rand($int)</code> where <code>$int</code> is the maximum value you want.  For example, to generate a number between 0 and 10 we could use:</p>
<pre class="brush: perl">#!/usr/bin/perl
use strict
use warnings;

my $rndnum = rand(10)
print $rndnum;</pre>
<p>This will give a result like: 5.9358180787331.</p>
<p>Alternatively, we could call <code>rand()</code> and multiply by the maximum value:</p>
<pre class="brush: perl">#!/usr/bin/perl
use warnings
use strict;

my $rndnum = rand() * 10
print $rndnum;</pre>
<p>Which returns the result: 1.65611195917339.</p>
<h2>Generating a Random Integer</h2>
<p>If we want to generate a random integer, to use an array index for example, then we need to use another function to round our double.  We have 3 choices: <a title="int()" href="http://perldoc.perl.org/functions/int.html" target="_blank"><code>int()</code></a>, <a title="ceil()" href="http://perldoc.perl.org/POSIX.html#ceil" target="_blank"><code>ceil()</code></a> and <a title="floor()" href="http://perldoc.perl.org/POSIX.html#floor" target="_blank"><code>floor()</code></a>.  <code>int()</code> will return the integer part of an expression, <code>ceil()</code> will round up to the next integer and <code>floor()</code> will round down.</p>
<pre class="brush: perl">#!/usr/bin/perl
use strict
use warnings;

my $rndnum = rand(10)
print "$rndnum =&gt; ". int($rndnum);</pre>
<p>Gives us: 4.03471924205409 =&gt; 4</p>
<p>Ceil and Round require the POSIX module:</p>
<pre class="brush: perl">#!/usr/bin/perl
use strict
use warnings;
use POSIX;

my $rndnum = rand(10)
print "$rndnum =&gt; ". ceil($rndnum);</pre>
<p>Gives us: 4.03471924205409 =&gt;5</p>


<div class="taggington"><p><!--<img src="/blog/wp-content/themes/stormblog/images/tag.gif" alt="tag"/>&nbsp; -->Tagged in: &nbsp;<a href="http://demo.storm-consultancy.com/blog/development/tag/perl/" rel="tag">Perl</a>&nbsp;&nbsp; <a href="http://demo.storm-consultancy.com/blog/development/tag/tutorials/" rel="tag">Tutorials</a></p></div><div class="relatedpostplugin">
<h3>You may also be interested in...</h3>
<div class="relatedlinks">
<a href="http://demo.storm-consultancy.com/blog/development/code-snippets/perl-print-internal-server-errors-to-the-browser/" rel="bookmark">Perl: Print Internal Server Errors to th...</a><!-- (13.1084)--><br /> 
<a href="http://demo.storm-consultancy.com/blog/development/code-snippets/perl-sorting-an-array-of-hashes/" rel="bookmark">Perl: Sorting an Array of Hashes</a><!-- (7.47668)--><br /> 
<a href="http://demo.storm-consultancy.com/blog/development/tips-tricks/no-rule-to-make-target-systemlibraryperl588darwin-thread-multi-2levelcoreconfigh-needed-by-makefile/" rel="bookmark">No rule to make target `/System/Library/...</a><!-- (5.53223)--></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://demo.storm-consultancy.com/blog/development/code-snippets/perl-generating-a-random-number-with-rand/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Archive of Related Entries with Movable Type</title>
		<link>http://demo.storm-consultancy.com/blog/development/code-snippets/archive-of-related-entries-with-movable-type/</link>
		<comments>http://demo.storm-consultancy.com/blog/development/code-snippets/archive-of-related-entries-with-movable-type/#comments</comments>
		<pubDate>Tue, 26 Feb 2008 14:40:25 +0000</pubDate>
		<dc:creator>Adam Pope</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.izonedesign.co.uk/blog/movable-type/archive-of-related-entries-with-movable-type/</guid>
		<description><![CDATA[Recently, I have been working on a Movable Type based website for monthly research data.  We found that we needed to create a sidebar module displaying monthly entries grouped by year.  After some head scratching I came up with the following MT4 code.

&#60;MTSetVarBlock name="category"&#62;&#60;MTEntryCategory&#62;&#60;/MTSetVarBlock&#62;

&#60;h3&#62;Historic Data&#60;/h3&#62;

&#60;$MTSetVar name="year" value="0"$&#62;

&#60;MTEntries sort_order="descend" category="$category"&#62;

&#60;MTIfNotEqual a="[MTEntryDate format='%Y']" b="[MTGetVar name='year']"&#62;
  [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I have been working on a Movable Type based website for monthly research data.  We found that we needed to create a sidebar module displaying monthly entries grouped by year.  After some head scratching I came up with the following MT4 code.</p>
<p><span id="more-25"></span></p>
<pre class="brush: plain">&lt;MTSetVarBlock name="category"&gt;&lt;MTEntryCategory&gt;&lt;/MTSetVarBlock&gt;

&lt;h3&gt;Historic Data&lt;/h3&gt;

&lt;$MTSetVar name="year" value="0"$&gt;

&lt;MTEntries sort_order="descend" category="$category"&gt;

&lt;MTIfNotEqual a="[MTEntryDate format='%Y']" b="[MTGetVar name='year']"&gt;
  &lt;MTIfNotEqual a="[MTGetVar name='year']" b="0"&gt;&lt;/ul&gt;&lt;/MTIfNotEqual&gt;
  &lt;h4&gt;&lt;$MTEntryDate format="%Y"$&gt;&lt;/h4&gt;&lt;ul&gt;
  &lt;MTSetVarBlock name="year"&gt;&lt;$MTEntryDate format="%Y"$&gt;&lt;/MTSetVarBlock&gt;
&lt;/MTIfNotEqual&gt;

&lt;li&gt;&lt;a href="&lt;$MTEntryPermalink$&gt;"&gt;&lt;$MTEntryTitle$&gt;&lt;/a&gt;&lt;/li&gt;

&lt;/MTEntries&gt;</pre>
<p>This code sets a variable, $category, containing the name of the category associated with the current entry.  It then selects all the entries in this category in descending date order.  A $year variable then tracks the year of the last entry, printing out a heading when the year changes.</p>


<div class="taggington"><p><!--<img src="/blog/wp-content/themes/stormblog/images/tag.gif" alt="tag"/>&nbsp; -->Tagged in: &nbsp;<a href="http://demo.storm-consultancy.com/blog/development/tag/code/" rel="tag">Code</a>&nbsp;&nbsp; <a href="http://demo.storm-consultancy.com/blog/development/tag/tutorials/" rel="tag">Tutorials</a></p></div><div class="relatedpostplugin">
<h3>You may also be interested in...</h3>
<div class="relatedlinks">
<a href="http://demo.storm-consultancy.com/blog/development/code-snippets/scheduling-a-process-with-cron-and-crontab/" rel="bookmark">Scheduling a Process with Cron and Cront...</a><!-- (3.89986)--><br /> 
<a href="http://demo.storm-consultancy.com/blog/development/code-snippets/perl-sorting-an-array-of-hashes/" rel="bookmark">Perl: Sorting an Array of Hashes</a><!-- (3.59594)--></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://demo.storm-consultancy.com/blog/development/code-snippets/archive-of-related-entries-with-movable-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 0.649 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-09-07 09:41:54 -->
