<?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>#! &#187; Perl</title>
	<atom:link href="http://hash-bang.net/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://hash-bang.net</link>
	<description>The start of all wonderful things</description>
	<lastBuildDate>Sun, 15 May 2011 03:36:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Converting Null</title>
		<link>http://hash-bang.net/2009/04/converting-null/</link>
		<comments>http://hash-bang.net/2009/04/converting-null/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 03:49:01 +0000</pubDate>
		<dc:creator>mc</dc:creator>
				<category><![CDATA[Fixes]]></category>
		<category><![CDATA[HowTo's]]></category>
		<category><![CDATA[Filters]]></category>
		<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://hash-bang.net/?p=136</guid>
		<description><![CDATA[Simple shell script fix this week &#8211; convert all newlines to null characters. Since most Linux shells have a major problem with doing this in a sensible way, the following (on my box called simply &#8217;0&#8242;) takes any input (either from files or via a pipe) and converts the characters. ?View Code PERL1 2 3 [...]]]></description>
			<content:encoded><![CDATA[<p>Simple shell script fix this week &#8211; convert all newlines to null characters.</p>
<p>Since most Linux shells have a major problem with doing this in a sensible way, the following (on my box called simply &#8217;0&#8242;) takes any input (either from files or via a pipe) and converts the characters.</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('p136code5'); return false;">View Code</a> PERL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1365"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p136code5"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
<span style="color: #666666; font-style: italic;"># From: http://hash-bang.net/2009/04/converting-nullconverting-null/</span>
<span style="color: #666666; font-style: italic;"># Author: Matt Carter &lt;m@ttcarter.com&gt;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009966; font-style: italic;">s/\n/\x00/g</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">print</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So now commands 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('p136code6'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1366"><td class="code" id="p136code6"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-type</span> f <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">mplayer</span></pre></td></tr></table></div>

<p>work perfectly (in the above case to play all files in directories recursively.</p>
<p>You may well ask why I don&#8217;t use the simple &#8216;-print0&#8242; argument for the find program &#8211; because it doesn&#8217;t work when using later pipes.</p>
<p>For example if i wanted to sort the above, &#8216;sort&#8217; would see the null symbol as a regular character.</p>
<p>So &#8217;0&#8242; works nicely in this case:</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('p136code7'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1367"><td class="code" id="p136code7"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-type</span> f <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">mplayer</span></pre></td></tr></table></div>

<p>Or even:</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('p136code8'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1368"><td class="code" id="p136code8"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-type</span> f <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">|</span> shuffle <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">mplayer</span></pre></td></tr></table></div>

<p>To play all files recursively, in random order. See the article on <a href="http://hash-bang.net/2009/04/shuffling-shell-input/">shuffle</a> for the source code of that filter.</p>
]]></content:encoded>
			<wfw:commentRss>http://hash-bang.net/2009/04/converting-null/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Shuffling shell input</title>
		<link>http://hash-bang.net/2009/04/shuffling-shell-input/</link>
		<comments>http://hash-bang.net/2009/04/shuffling-shell-input/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 04:10:02 +0000</pubDate>
		<dc:creator>mc</dc:creator>
				<category><![CDATA[HowTo's]]></category>
		<category><![CDATA[Filters]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://hash-bang.net/?p=144</guid>
		<description><![CDATA[While the Fisher-Yates shuffle sounds like 1920&#8242;s dance move, it is the standard method for shuffling larger arrays of data. The below operates like a simple Unix filter taking an array of data (separated by newlines) and returning a quickly sorted output. ?Download shuffle1 2 3 4 5 6 7 8 9 10 11 12 [...]]]></description>
			<content:encoded><![CDATA[<p>While the <a href="http://en.wikipedia.org/wiki/Fisher-Yates_shuffle">Fisher-Yates shuffle</a> sounds like 1920&#8242;s dance move, it is the standard method for shuffling larger arrays of data.</p>
<p>The below operates like a simple Unix filter taking an array of data (separated by newlines) and returning a quickly sorted output.</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="left2">Download <a href="http://hash-bang.net/wp-content/plugins/wp-codebox/wp-codebox.php?p=144&amp;download=shuffle">shuffle</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14411"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p144code11"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
<span style="color: #666666; font-style: italic;"># Usage: cat file | shuffle</span>
<span style="color: #666666; font-style: italic;"># From: http://hash-bang.net/2009/04/shuffling-shell-inputshuffling-shell-input/</span>
<span style="color: #666666; font-style: italic;"># Return a shuffled version of the input stream</span>
<span style="color: #666666; font-style: italic;"># Author: Matt Carter &lt;m@ttcarter.com&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> fisher_yates_shuffle <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$array</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@$array</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$j</span> <span style="color: #339933;">=</span> <span style="color: #000066;">int</span> <span style="color: #000066;">rand</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">next</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">$j</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">@$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$j</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">chomp</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">push</span> <span style="color: #0000ff;">@input</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">&amp;fisher_yates_shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\@input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #000066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">@input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>For example:</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('p144code12'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14412"><td class="code" id="p144code12"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">|</span> shuffle</pre></td></tr></table></div>

<p>Returns a randomized list of files in the current directory.</p>
]]></content:encoded>
			<wfw:commentRss>http://hash-bang.net/2009/04/shuffling-shell-input/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

