<?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>PoundBangWhack.com &#187; Programming</title>
	<atom:link href="http://www.poundbangwhack.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.poundbangwhack.com</link>
	<description>A web development/programming blog providing info, tips, and tricks on programming languages, scripting, Linux, MySQL and more</description>
	<lastBuildDate>Sat, 17 Jul 2010 05:17:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>What do three &#8220;less than&#8221; (</title>
		<link>http://www.poundbangwhack.com/2010/07/02/what-is-heredoc/</link>
		<comments>http://www.poundbangwhack.com/2010/07/02/what-is-heredoc/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 20:47:24 +0000</pubDate>
		<dc:creator>Mark Stoecker</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SSH/Command Line]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Heredoc]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.poundbangwhack.com/?p=1349</guid>
		<description><![CDATA[To teach myself PHP, I started out reading <a href="http://www.amazon.com/gp/product/0764543644?ie=UTF8&#038;tag=httpwwwdese09-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0764543644">Beginning PHP4 (Programmer to Programmer)</a><img src="http://www.assoc-amazon.com/e/ir?t=httpwwwdese09-20&#038;l=as2&#038;o=1&#038;a=0764543644" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />.  After that, I begin studying others' code.  One of the first things I learned from other peoples' code was <a href="http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc">heredoc syntax.</a>  It took me a while to fully understand what it was and how to use it, but now I use it all the time and it is a fantastic tool.]]></description>
			<content:encoded><![CDATA[<p>I am a true believer in that the best ways to learn how to do anything is by studying work of those who have done the same thing previously.  When programming, that means looking at others&#8217; code and learning from what they do.  However, <strong>don&#8217;t assume that everything someone else does is correct!</strong>  What this means is that not only should you learn what <strong>to</strong> do from other peoples&#8217; code, but you should also learn what <strong>not</strong> to do.<br />
<span id="more-1349"></span><br />
To teach myself PHP, I started out reading <a href="http://www.amazon.com/gp/product/0764543644?ie=UTF8&#038;tag=httpwwwdese09-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0764543644">Beginning PHP4 (Programmer to Programmer)</a><img src="http://www.assoc-amazon.com/e/ir?t=httpwwwdese09-20&#038;l=as2&#038;o=1&#038;a=0764543644" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />.  After that, I begin studying others&#8217; code.  One of the first things I learned from other peoples&#8217; code was <a href="http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc">heredoc syntax.</a>  It took me a while to fully understand what it was and how to use it, but now I use it all the time and it is a fantastic tool.</p>
<h3>What is heredoc?</h3>
<p>Heredoc syntax is a way to delimit strings in PHP (and other languages) with <strong>3 &#8220;less than&#8221; symbols: &lt;&lt;&lt;</strong>.  </p>
<blockquote><p>
After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.</p>
<p>The closing identifier <strong>must </strong> begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.<br />
<cite><a href="http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc">PHP: Strings &#8211; Manual</a></cite>
</p></blockquote>
<p>An example may help:</p>
<pre name="code" class="php">
&lt;?php
echo &lt;&lt;&lt;EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
?>
</pre>
<p>This will output:<br />
&#8220;Example of string<br />
spanning multiple lines<br />
using heredoc syntax&#8221;</p>
<p>Not very impressive.  However, where the handiness comes in is in the following:</p>
<blockquote><p>
Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.<br />
<cite><a href="http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc">PHP: Strings &#8211; Manual</a></cite>
</p></blockquote>
<p>This allows you to use variables and quotes in your heredocs without having to escape every single quote you want to use.  It makes it very useful for printing out extremely long strings of text.  </p>
<h3>Things to remember about using heredoc</h3>
<p>Heredocs are a great way to simplify your code and print out long strings.  Here is just a few things to keep in mind when using them:</p>
<ul>
<li>Heredocs start with &#8220;&lt;&lt;&lt;<em>IDENTIFIER</em> where <em>IDENTIFIER</em> must follow the same naming conventions of PHP variables.</li>
<li>The closing identifier <strong>must</strong> be the same as the opening identifier</li>
<li>The line with the closing identifier <strong>must contain no other characters</strong>, except possibly  a semicolon (;). That means especially that <strong>the identifier may not be indented</strong>, and <strong>there may not be any spaces or tabs before or after the semicolon</strong></li>
<li>Quotes and double quotes do not need to be escaped</li>
<li>Variables are expanded</li>
</ul>
<p>For more information, refer to the <a href="http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc">PHP: Strings Manual on php.net</a></p>
<h3>Bonus: Heredocs for bash scripting</h3>
<p>Heredocs for bash work pretty much the exact same way as they do for PHP but are delimited by <strong>2 &#8220;less than&#8221; symbols:&#8221; &lt;&lt;</strong> instead of 3.  Here is a handy way to output large fields of text into a file through a bash script:</p>
<pre name="code" class="php">
#!/bin/bash
cat &lt;&lt;OUTPUT > /etc/my.cnf
[mysqld]
socket=/tmp/mysql.sock
port=3306
user=mysql
password=supersecretpassword
log-bin=mysql-bin
server-id=1
OUTPUT
</pre>
<p>What about you? What new things have you learned from reading other peoples&#8217; code? Share with us and we&#8217;ll all learn something new.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.poundbangwhack.com/2010/07/02/what-is-heredoc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to append values to an array in bash</title>
		<link>http://www.poundbangwhack.com/2010/02/04/how-to-append-values-to-an-array-in-bash/</link>
		<comments>http://www.poundbangwhack.com/2010/02/04/how-to-append-values-to-an-array-in-bash/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 20:14:08 +0000</pubDate>
		<dc:creator>Mark Stoecker</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[SSH/Command Line]]></category>
		<category><![CDATA[Shell Scripts]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SSH]]></category>

		<guid isPermaLink="false">http://www.poundbangwhack.com/?p=1257</guid>
		<description><![CDATA[Last week I was working on a bash script for a project at work.  The script parsed through a log file with server load and disk usage statistics at regular intervals.  The script was calculating the average CPU idle time, disk utilization, and disk usage for servers.  After calculating the averages for [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I was working on a bash script for a project at work.  The script parsed through a log file with server load and disk usage statistics at regular intervals.  The script was calculating the average CPU idle time, disk utilization, and disk usage for servers.  After calculating the averages for each of these three metrics, I then proceeded to loop through all the lines in the file and create an array of all the times when the CPU idle time was below average, or the disk utilization or usage was above average.<br />
<span id="more-1257"></span><br />
The code that I used was originally taken from the article <a href="http://fvue.nl/wiki/Bash:_Append_to_array_using_while-loop" target="_blank">Bash: Append to array using while-loop</a> by Freddy Vulto. The concept that Freddy used is essentially the same as what I was doing.  I was using a <span class="pre">for</span> loop to scan a file, and if certain values in the file met a condition, the date was added to an array which was printed at the end of the script execution.  The code to achieve this is as follows:</p>
<p><code>array=( ${array[@]-} $(echo "$variable") )</code></p>
<p>The code above reads as follows:</p>
<p><span class="pre">array=( &#8230;.. )</span> : Set your array variable named, appropriately enough: <em>array</em>.</p>
<p><span class="pre">${array[@]-}</span> : Read all variables from the array <em>array</em>.  The hyphen at the end prevents an &#8220;unbound variable&#8221; error when <span class="code">-u</span> or <span class="code">-o nounset</span> is set and <em>array</em> is an empty array.  This is useful for appending the first value to an empty array in the event that one of the aforementioned flags is set.</p>
<p><span class="pre">$(echo &#8220;$variable&#8221;)</span> : Add the variable <em>$variable</em> to the array through the use of command substitution <span class="code">$(&#8230;)</span> by reading the output of <span class="code">echo &#8220;$variable&#8221;</span> as the input value for the array.  The reason for command substitution is that if you try it without the <span class="code">$(&#8230;)</span>, bash will read <span class="code">echo</span> and <span class="code">&#8220;$variable&#8221;</span> as separate entries into the array instead of just the value of <em>$variable</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.poundbangwhack.com/2010/02/04/how-to-append-values-to-an-array-in-bash/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
