<?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>Erics Tech Blog &#187; grep</title>
	<atom:link href="http://eric.lubow.org/tag/grep/feed/" rel="self" type="application/rss+xml" />
	<link>http://eric.lubow.org</link>
	<description>Thoughts, musings, and other idealistic (sometimes useful) systems and development hoopla.</description>
	<lastBuildDate>Fri, 18 Nov 2011 14:56:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Cleaning Up Long Conditionals With Grep</title>
		<link>http://eric.lubow.org/2009/perl/cleaning-up-long-conditionals-with-grep/</link>
		<comments>http://eric.lubow.org/2009/perl/cleaning-up-long-conditionals-with-grep/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 14:00:22 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[grep]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=95</guid>
		<description><![CDATA[Every so often I am faced with testing a few conditionals before dropping into another control structure. If you have to test out a few conditionals, then its likely a dispatch table won&#8217;t be useful. If you have a lot of conditionals to test, you&#8217;ll likely not want to deal with an ugly expression like [...]]]></description>
			<content:encoded><![CDATA[<p>Every so often I am faced with testing a few conditionals before dropping into another control structure.  If you have to test out a few conditionals, then its likely a dispatch table won&#8217;t be useful.  If you have a lot of conditionals to test, you&#8217;ll likely not want to deal with an ugly expression like the following:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">if ( (defined $SITE{$partner}{'foo'} &amp;&amp; ($SITE{$partner}{'foo'} &gt; 0) ) and<br />
&nbsp; &nbsp; &nbsp; &nbsp; ( (defined $assoc{'foo'} &amp;&amp; ($assoc{'foo'} &gt; 0)) or<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (defined $assoc{'bar'} &amp;&amp; ($assoc{'bar'} &gt; 0)))<br />
&nbsp; &nbsp;) {<br />
print &quot;We're here!\n&quot;;<br />
}</div></div>
<p>One of the ways to deal with it to to use <strong>grep</strong>.  Since <strong>grep</strong> returns the number of elements in the array that evaluate to true (when called in a scalar context), I can do the following to make it work:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">my @foo = [ $SITE{$partner}{'foo'}, $assoc{'foo'}, $assoc{'bar'} ];<br />
if ( (grep {defined($_) and $_ &gt; 0} @foo) &gt; 2) { print &quot;We're here!\n&quot;; }</div></div>
<p>The reason this works is that when called in the scalar context, <strong>grep</strong> only returns the number of elements that evaluate to true.  Since there are 3 elements in the array, the return value is greater than 2, then the entire expression evaluates to true.  This is not only a lot easier to read and a lot cleaner to write, but it makes for easy additions to the conditional testing if necessary.  Although changing the context in which functions are called is common, it is easily forgotten.  <strong>Grep</strong> called in scalar context can be easily manipulated (as above) to add readability to your program.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/perl/testing-for-a-number/' rel='bookmark' title='Testing For A Number'>Testing For A Number</a></li>
<li><a href='http://eric.lubow.org/2007/perl/creating-a-process-table-hash-in-perl/' rel='bookmark' title='Creating a Process Table hash in Perl'>Creating a Process Table hash in Perl</a></li>
<li><a href='http://eric.lubow.org/2009/perl/mac-perl-problems-after-feb-update/' rel='bookmark' title='Mac Perl Problems After Feb Update'>Mac Perl Problems After Feb Update</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/perl/cleaning-up-long-conditionals-with-grep/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

