<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-20900696</id><updated>2008-03-07T23:07:45.285-08:00</updated><title type='text'>Effective Perl by Joseph N. Hall</title><link rel='alternate' type='text/html' href='http://effectiveperl.blogspot.com/'/><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/posts/default'/><author><name>Joseph N. Hall</name><uri>http://www.blogger.com/profile/08997289553387618189</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-20900696.post-3013049582828562332</id><published>2007-10-11T13:58:00.000-07:00</published><updated>2007-10-11T14:05:14.068-07:00</updated><title type='text'>Your Phone Screen Homework Question</title><content type='html'>Before or after the phone screen, depending on how things work out, you may be asked to complete a short programming exercise. It won't be something with a lengthy answer but it will require you to demonstrate an understanding of a few key Perl skills. Here is the one that I have used recently. It is a kind of "beginning intermediate" question about a type of problem that comes up reasonably often in serious Perl programming and that has a distinctive Perl solution. Ordinarily you would be allowed access to (and encouraged to use) the Internet and/or other references if necessary.&lt;br /&gt;&lt;br /&gt;The problem:&lt;br /&gt;&lt;br /&gt;Create a hash that represents a set of items, and another hash that represents another set of items.&lt;br /&gt;&lt;br /&gt;Compute a hash that represents the union of both sets.&lt;br /&gt;&lt;br /&gt;Compute a hash that represents the intersection of both sets.&lt;br /&gt;&lt;br /&gt;Print out the contents of the union and intersection sets sorted in alphabetic order.</content><link rel='alternate' type='text/html' href='http://effectiveperl.blogspot.com/2007/10/your-phone-screen-homework-question.html' title='Your Phone Screen Homework Question'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20900696&amp;postID=3013049582828562332' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/3013049582828562332/comments/default' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/posts/default/3013049582828562332'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20900696/posts/default/3013049582828562332'/><author><name>Joseph N. Hall</name><uri>http://www.blogger.com/profile/08997289553387618189</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-20900696.post-5906908807489661446</id><published>2007-10-10T14:20:00.000-07:00</published><updated>2007-10-10T14:51:19.693-07:00</updated><title type='text'>Preparing for the Phone Screen, Part 1</title><content type='html'>It has been a while since I last blogged here! Let's put that aside for a bit, though, in favor of a general-interest topic.&lt;br /&gt;&lt;br /&gt;Nowadays I am happily working at VMware. I will probably be doing interviews and phone screens from time to time, and if you are being considered for a position in my team, I will be the "Perl Person" you have to speak with. If you are doing a little research ahead of time, I have a treat for you, because here are some of the questions I am going to ask. If I have space and/or energy, I will eventually post &lt;span style="font-style: italic;"&gt;all&lt;/span&gt; the questions I will ask, but that will have to wait for later.&lt;br /&gt;&lt;br /&gt;These questions aren't too complicated, but I don't necessarily expect you to get them all correct. In fact, you can get a number of them incorrect as long as, during the process, you convince me that you know which ones you don't know, and you know how to find the answer quickly.&lt;br /&gt;&lt;br /&gt;Introduction&lt;br /&gt;&lt;ul&gt;&lt;li&gt;When have you used Perl? What version(s)? What platform(s)?&lt;/li&gt;&lt;li&gt;What Perl book(s) have you read? What class(es) have you taken? Have you taught yourself? How much? What about?&lt;/li&gt;&lt;/ul&gt;Basics&lt;br /&gt;&lt;ul&gt;&lt;li&gt;What is &lt;span style="font-family:courier new;"&gt;strict&lt;/span&gt;? What is &lt;span style="font-family:courier new;"&gt;warnings&lt;/span&gt;?&lt;/li&gt;&lt;li&gt;Name a special variable. One other than &lt;span style="font-family:courier new;"&gt;$_&lt;/span&gt;. What is &lt;span style="font-family:courier new;"&gt;$_&lt;/span&gt;?&lt;br /&gt;&lt;/li&gt;&lt;li&gt;What is &lt;span style="font-family: courier new;"&gt;perldoc&lt;/span&gt;?&lt;/li&gt;&lt;li&gt;What is a Perl module?&lt;/li&gt;&lt;li&gt;What is the difference between &lt;span style="font-family:courier new;"&gt;print&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;printf&lt;/span&gt;?&lt;/li&gt;&lt;li&gt;What does &lt;span style="font-family:courier new;"&gt;local&lt;/span&gt; mean? Where could you use it?&lt;/li&gt;&lt;li&gt;How do you open a file? What is the return value from &lt;span style="font-family:courier new;"&gt;open&lt;/span&gt;? What is &lt;span style="font-family:courier new;"&gt;$!&lt;/span&gt;?&lt;/li&gt;&lt;li&gt;What is "null" in Perl? How do you tell if something is "null"?&lt;/li&gt;&lt;li&gt;How do you find the size of an array?&lt;/li&gt;&lt;li&gt;How do you write an infinite loop?&lt;/li&gt;&lt;li&gt;What is a regular expression that matches any character?&lt;/li&gt;&lt;li&gt;How do you remove a key-value pair from a hash?&lt;/li&gt;&lt;li&gt;What is a "pointer" in Perl? What kinds are there? How do you make one? What can you do with them?&lt;/li&gt;&lt;/ul&gt;That's all for now. Answers, and more questions, later.</content><link rel='alternate' type='text/html' href='http://effectiveperl.blogspot.com/2007/10/preparing-for-phone-screen-part-1.html' title='Preparing for the Phone Screen, Part 1'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20900696&amp;postID=5906908807489661446' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/5906908807489661446/comments/default' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/posts/default/5906908807489661446'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20900696/posts/default/5906908807489661446'/><author><name>Joseph N. Hall</name><uri>http://www.blogger.com/profile/08997289553387618189</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-20900696.post-113895291658527996</id><published>2006-02-02T23:20:00.000-08:00</published><updated>2007-04-12T05:27:43.186-07:00</updated><title type='text'>Tip: Regular Expression Precedence Made Simple (as Arithmetic)</title><content type='html'>&lt;p&gt;The basic operations in Perl &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+regular+expressions"&gt;regular expressions&lt;/a&gt; are &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+repetition"&gt;repetition&lt;/a&gt;, &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+sequence"&gt;sequence&lt;/a&gt;, and &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+alternation"&gt;alternation&lt;/a&gt;. That is also - from highest to lowest (tightest-binding to loosest-binding) - their &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+precedence"&gt;precedence&lt;/a&gt;. A super-quick review first:&lt;/p&gt;&lt;pre&gt;a*     &lt;i&gt;# repetition - the &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+character"&gt;character&lt;/a&gt; a repeated zero or more times&lt;/i&gt;&lt;br /&gt;b+     &lt;i&gt;# repetition - the character b repeated one or more times&lt;/i&gt;&lt;br /&gt;x{1,3} &lt;i&gt;# repetition - the character x repeated one to three times&lt;/i&gt;&lt;br /&gt;abc    &lt;i&gt;# sequence - the character a, then the character b, then the character c&lt;/i&gt;&lt;br /&gt;a|b|c  &lt;i&gt;# alternation - the character a, &lt;b&gt;or&lt;/b&gt; the character b, &lt;b&gt;or&lt;/b&gt; the character c&lt;/i&gt;&lt;/pre&gt;&lt;p&gt;It's important to understand precedence in regular expressions. For example:&lt;/p&gt;&lt;pre&gt;abc{3}&lt;/pre&gt;&lt;p&gt;means the characters &lt;code&gt;'ab'&lt;/code&gt; followed by &lt;i&gt;three&lt;/i&gt; instances of the character &lt;code&gt;'c'&lt;/code&gt;. When I see something like &lt;code&gt;abc{3}&lt;/code&gt; I usually think that the author really meant "three instances of the characters &lt;code&gt;'abc'&lt;/code&gt;" - which is written differently:&lt;/p&gt;&lt;pre&gt;(abc){3}&lt;/pre&gt;&lt;p&gt;As you can see, you can use &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+parentheses"&gt;parentheses&lt;/a&gt; to control the order in which the bits of a regular expression are interpreted. I like to make an analogy to mathematical (algebraic) expressions. Even though a regular expression isn't a mathematical expression, the syntax is at least somewhat similar, especially where precedence is concerned. From the standpoint of precedence, you can think of &lt;code&gt;a{3}&lt;/code&gt; as being something like &lt;i&gt;x&lt;span style="vertical-align: super;"&gt;10&lt;/span&gt;&lt;/i&gt; - exponentation, the highest-precedence operation in algebraic notation. &lt;code&gt;abc&lt;/code&gt; is like &lt;i&gt;xyz&lt;/i&gt; (the variables &lt;i&gt;x&lt;/i&gt;, &lt;i&gt;y&lt;/i&gt;, and &lt;i&gt;z&lt;/i&gt; multiplied together) - multiplication having intermediate precedence - and &lt;code&gt;a|b|c&lt;/code&gt; is like &lt;i&gt;x + y + z&lt;/i&gt; - addition having low precedence. This becomes useful when you try to figure out things like:&lt;/p&gt;&lt;pre&gt;a|b|c      &lt;i&gt;# the character a, or the character b, or the character c&lt;/i&gt;&lt;br /&gt;a|b|c{2}   &lt;i&gt;# the character a, the character b, or two c's in a row&lt;/i&gt;&lt;br /&gt;           &lt;i&gt;# like a + b + c&lt;span style="vertical-align: super"&gt;2&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;(a|b|c){2} &lt;i&gt;# one of a or b or c followed by one of a or b or c&lt;/i&gt;&lt;br /&gt;           &lt;i&gt;# like (a + b + c)&lt;span style="vertical-align: super"&gt;2&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;(a|b|c)+   &lt;i&gt;# one or more a or b or c&lt;/i&gt;&lt;br /&gt;(abc)+     &lt;i&gt;# abc one or more times in a row (abc, abcabc, abcabcabc, etc.)&lt;/i&gt;&lt;/pre&gt;&lt;p&gt;So, think:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Repetition: exponentiation (highest)&lt;/li&gt;&lt;li&gt;Sequence: multiplication (middle)&lt;/li&gt;&lt;li&gt;Alternation: addition (lowest)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Now, the usefulness of all this depends on arithmetic (or algebra) being easy, which may be something else altogether.&lt;/p&gt;</content><link rel='alternate' type='text/html' href='http://effectiveperl.blogspot.com/2006/02/tip-regular-expression-precedence-made.html' title='Tip: Regular Expression Precedence Made Simple (as Arithmetic)'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20900696&amp;postID=113895291658527996' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/113895291658527996/comments/default' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/posts/default/113895291658527996'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20900696/posts/default/113895291658527996'/><author><name>Joseph N. Hall</name><uri>http://www.blogger.com/profile/08997289553387618189</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-20900696.post-113865660965740631</id><published>2006-01-30T12:58:00.000-08:00</published><updated>2007-04-10T01:59:07.500-07:00</updated><title type='text'>Tip: Creating a String of Random Hex Digits</title><content type='html'>&lt;p&gt;Every once in a while a string of random &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+hex+digits"&gt;hex digits&lt;/a&gt; comes in handy*, perhaps when you need a &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+random"&gt;random&lt;/a&gt; alphanumeric name for something. The &lt;code&gt;&lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+unpack"&gt;unpack&lt;/a&gt;&lt;/code&gt; operator seems like it should come in handy for this purpose, given that it can translate character values into hex strings, and so it does. Here's a snippet that creates a string of 32 random hex digits:&lt;/p&gt;&lt;pre&gt;&lt;em&gt;my $rand_hex = join "", map { unpack "H*", chr(rand(256)) } 1..16;&lt;/em&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;&lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+rand"&gt;rand&lt;/a&gt;(256)&lt;/code&gt; produces &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+floating-point"&gt;floating-point&lt;/a&gt; numbers &lt;code&gt;0.0 &amp;lt;= n &amp;lt; 256.0&lt;/code&gt;. &lt;code&gt;unpack "H2"&lt;/code&gt; turns a character (a single-character string to be precise) into a 2-character MSB-first hex string - the &lt;code&gt;&lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+chr"&gt;chr&lt;/a&gt;&lt;/code&gt; is needed to turn the numeric value from &lt;code&gt;rand&lt;/code&gt; into a string. The &lt;code&gt;&lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+map"&gt;map&lt;/a&gt; { ... } 1..16&lt;/code&gt; creates a list of 16 different 2-character strings, and &lt;code&gt;&lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+join"&gt;join&lt;/a&gt;&lt;/code&gt; gloms them all together.&lt;/p&gt;&lt;p&gt;Here's another - simpler - way to generate 32 random hex digits:&lt;/p&gt;&lt;pre&gt;use Digest::&lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+md5"&gt;MD5&lt;/a&gt; qw(md5_hex);&lt;br /&gt;&lt;em&gt;my $rand_hex2 = md5_hex(rand);&lt;/em&gt;&lt;/pre&gt;&lt;p&gt;*Or maybe you think that Perl looks like random hex digits all on its own. Most people, however, think it looks like modem line noise $$817 d3#@ ss NO CARRIER&lt;/p&gt;</content><link rel='alternate' type='text/html' href='http://effectiveperl.blogspot.com/2006/01/tip-creating-string-of-random-hex.html' title='Tip: Creating a String of Random Hex Digits'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20900696&amp;postID=113865660965740631' title='3 Comments'/><link rel='replies' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/113865660965740631/comments/default' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/posts/default/113865660965740631'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20900696/posts/default/113865660965740631'/><author><name>Joseph N. Hall</name><uri>http://www.blogger.com/profile/08997289553387618189</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-20900696.post-113804858997183135</id><published>2006-01-23T08:11:00.000-08:00</published><updated>2006-10-15T10:59:40.810-07:00</updated><title type='text'>Tip: Reading a Few Lines from a File</title><content type='html'>&lt;p&gt;How would you read a specific number of &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+lines"&gt;lines&lt;/a&gt; - say, 10 - from &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+standard+input"&gt;standard input&lt;/a&gt;? There's a plain old way:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;my @lines;&lt;br /&gt;for (my $i = 0; $i &lt; 10; $i++) {&lt;br /&gt;  my $line = &amp;lt;STDIN&amp;gt;;&lt;br /&gt;  push @lines, $line;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This reads in 10 lines, one at a time, and &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+append"&gt;append&lt;/a&gt;s them in order to the &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+array"&gt;array&lt;/a&gt; &lt;code&gt;@lines&lt;/code&gt;. Another perhaps cleverer way &lt;i&gt;might&lt;/i&gt; be:&lt;/p&gt;&lt;pre&gt;&lt;strong&gt;my @lines = map &amp;lt;STDIN&amp;gt;, 1..10;&lt;/strong&gt;&lt;/pre&gt;&lt;p&gt;I say "might" because this doesn't actually work. It looks as if you are using the ten numbers &lt;code&gt;1&lt;/code&gt; through &lt;code&gt;10&lt;/code&gt; to read 10 lines from standard input, which seems reasonable at first, because the &lt;code&gt;..&lt;/code&gt; ("&lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+dot%20dot"&gt;dot-dot&lt;/a&gt;") operator returns a list of numbers when used in a &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+list+context"&gt;list context&lt;/a&gt;. But the left hand expression you pass to &lt;code&gt;&lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+map"&gt;map&lt;/a&gt;&lt;/code&gt; is &lt;i&gt;also&lt;/i&gt; evaluated in a list context, and so the &lt;code&gt;&amp;lt;&lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+STDIN"&gt;STDIN&lt;/a&gt;&amp;gt;&lt;/code&gt; operator returns a list of &lt;i&gt;all&lt;/i&gt; the remaining lines in the file. In other words, you ask for 10 lines but you get them all. You need to force a &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+scalar+context"&gt;scalar context&lt;/a&gt; on the first argument in order to make &lt;code&gt;&amp;lt;STDIN&amp;gt;&lt;/code&gt; to return one line at a time:&lt;/p&gt;&lt;pre&gt;&lt;em&gt;my @lines = map scalar(&amp;lt;STDIN&amp;gt;), 1..10;&lt;/em&gt;&lt;/pre&gt;&lt;p&gt;Should there be fewer than 10 lines in your input, the array &lt;code&gt;@lines&lt;/code&gt; will be padded with enough &lt;code&gt;&lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+undef"&gt;undef&lt;/a&gt;&lt;/code&gt;s to make up the difference because your attempts to read past end of file will return &lt;code&gt;undef&lt;/code&gt;.</content><link rel='alternate' type='text/html' href='http://effectiveperl.blogspot.com/2006/01/tip-reading-few-lines-from-file_23.html' title='Tip: Reading a Few Lines from a File'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20900696&amp;postID=113804858997183135' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/113804858997183135/comments/default' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/posts/default/113804858997183135'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20900696/posts/default/113804858997183135'/><author><name>Joseph N. Hall</name><uri>http://www.blogger.com/profile/08997289553387618189</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-20900696.post-113761533288473312</id><published>2006-01-18T21:01:00.000-08:00</published><updated>2007-04-04T00:43:15.070-07:00</updated><title type='text'>Plans for a New Edition of Effective Perl Programming?</title><content type='html'>I've been really pleased with all the great reviews that &lt;span style="font-style:italic;"&gt;Effective Perl Programming&lt;/span&gt; has received over the years. The good sales are nice too - I'm still getting reasonable royalty checks.&lt;br /&gt;&lt;br /&gt;I've been thinking of writing another edition for some time. At present I'm negotiating  to do just that. Hopefully my work can be done by the end of the year, or maybe a little sooner.&lt;br /&gt;&lt;br /&gt;Once I get going, I'll be posting bits of work in progress here. I will also, at some point, post the entire PDF of the current version. I may also ask for ideas about what to include. I think the rewrite will be somewhat different in its focus than the original.</content><link rel='alternate' type='text/html' href='http://effectiveperl.blogspot.com/2006/01/plans-for-new-edition-of-effective.html' title='Plans for a New Edition of Effective Perl Programming?'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20900696&amp;postID=113761533288473312' title='5 Comments'/><link rel='replies' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/113761533288473312/comments/default' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/posts/default/113761533288473312'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20900696/posts/default/113761533288473312'/><author><name>Joseph N. Hall</name><uri>http://www.blogger.com/profile/08997289553387618189</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-20900696.post-113746701209912112</id><published>2006-01-16T18:38:00.000-08:00</published><updated>2006-08-17T11:12:33.106-07:00</updated><title type='text'>Idiomatic Perl: Counting the Number of Times a Character Occurs in a String</title><content type='html'>From time to time you may want to &lt;a href="http://www.google.com/search?q=count+site%3Aeffectiveperl.blogspot.com"&gt;count&lt;/a&gt; the number of &lt;a href="http://www.google.com/search?q=occurrences+site%3Aeffectiveperl.blogspot.com"&gt;occurrences&lt;/a&gt; of a &lt;a href="http://www.google.com/search?q=character+site%3Aeffectiveperl.blogspot.com"&gt;character&lt;/a&gt; inside a string. You could loop from the beginning of the string to the end:&lt;pre&gt;my $s = "Testing one two three.";&lt;br /&gt;&lt;br /&gt;my $m = 0;&lt;br /&gt;for (my $i = 0; $i &amp;lt; length($s); $i++) {&lt;br /&gt;  $m++ if (substr($s, $i, 1) eq 'e');&lt;br /&gt;}&lt;/pre&gt;Ugly. This is going to take all day with all those calls to &lt;a href="http://www.google.com/search?q=substr+site%3Aeffectiveperl.blogspot.com"&gt;&lt;code&gt;substr&lt;/code&gt;&lt;/a&gt;.* You could &lt;a href="http://www.google.com/search?q=split+site%3Aeffectiveperl.blogspot.com"&gt;&lt;code&gt;split&lt;/code&gt;&lt;/a&gt; the string. There's a  special &lt;code&gt;split&lt;/code&gt; pattern &lt;code&gt;//&lt;/code&gt; that divides the input string into characters. So perhaps:&lt;pre&gt;my $n = 0;&lt;br /&gt;for (split //, $s) {&lt;br /&gt;  $n++ if $_ eq 'e';&lt;br /&gt;}&lt;/pre&gt;Maybe this doesn't seem quite right to you yet. There &lt;span style="font-style: italic;"&gt;must&lt;/span&gt; be a shorter way to do this in Perl, right? Yes. The &lt;code&gt;&lt;a href="http://www.google.com/search?q=tr+site%3Aeffectiveperl.blogspot.com"&gt;tr&lt;/a&gt;///&lt;/code&gt; (transliterate characters) operator can count characters for you.  The &lt;code&gt;tr///&lt;/code&gt; operator returns the number of characters it changed. For example:&lt;pre&gt;my $example = "Mixed Case";&lt;br /&gt;my $changed = $example =~ tr/a-z/A-Z/;  # make everything uppercase&lt;br /&gt;print $changed, "\n";  # 9&lt;/pre&gt;The value returned from &lt;code&gt;tr///&lt;/code&gt; above is 9, which is the number of characters changed to &lt;a href="http://www.google.com/search?q=uppercase+site%3Aeffectiveperl.blogspot.com"&gt;uppercase&lt;/a&gt;. Armed with this knowledge, you might think of:&lt;pre&gt;my $c = $example =~ tr/e/e/;  # change 'e' to 'e' ...&lt;/pre&gt;which will work just fine. This is perfectly reasonable, but there's a shortcut. If you omit the second argument to &lt;code&gt;tr///&lt;/code&gt;, no characters are changed, but &lt;code&gt;tr///&lt;/code&gt; still returns a count of the character(s) in its first argument.&lt;pre&gt;&lt;em&gt;my $c = $example =~ tr/e//;&lt;/em&gt;  # counts the number of 'e' just as above&lt;br /&gt;&lt;br /&gt;my $c_lower = $example =~ tr/a-z//; # counts lowercase letters in $example&lt;/pre&gt;&lt;code&gt;tr///&lt;/code&gt; is so smart, in fact, that it knows when you aren't changing the string, meaning that you can write things like this:&lt;pre&gt;my $count_ignore_case = lc($example) =~ tr/t//;&lt;/pre&gt;Normally, &lt;code&gt;tr///&lt;/code&gt; requires a modifiable value, but if it won't be making changes, you can supply a constant argument like a result from an expression.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;-joseph&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;*Not really, but ouch.</content><link rel='alternate' type='text/html' href='http://effectiveperl.blogspot.com/2006/01/idiomatic-perl-counting-number-of.html' title='Idiomatic Perl: Counting the Number of Times a Character Occurs in a String'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20900696&amp;postID=113746701209912112' title='6 Comments'/><link rel='replies' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/113746701209912112/comments/default' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/posts/default/113746701209912112'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20900696/posts/default/113746701209912112'/><author><name>Joseph N. Hall</name><uri>http://www.blogger.com/profile/08997289553387618189</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-20900696.post-113725801997326859</id><published>2006-01-14T08:48:00.000-08:00</published><updated>2007-10-11T14:08:44.233-07:00</updated><title type='text'>Idiomatic Perl: The Hash as a Set</title><content type='html'>It's often useful to know whether a particular element or value exists in a &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+set"&gt;set&lt;/a&gt; of items. For example, you might want to know whether the color &lt;code&gt;'red'&lt;/code&gt; exists in a &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+list"&gt;list&lt;/a&gt; of colors. If you have this list in an &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+array"&gt;array&lt;/a&gt;, you have to iterate over the contents of the array with a loop:&lt;br /&gt;&lt;pre&gt;my @color = qw(green blue yellow cyan violet puce taupe lilac);&lt;br /&gt;for my $item @color (&lt;br /&gt;if ($item eq 'red') {&lt;br /&gt;  print "Found red.\n";&lt;br /&gt;  exit;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;print "Found no red.\n";&lt;/pre&gt;This isn't terribly exciting and it is horribly inefficient with larger arrays. The idiomatic way of looking for something in a set in Perl is seeing whether it &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+exists"&gt;exists&lt;/a&gt; in a &lt;a href="http://www.google.com/search?q=site%3Aeffectiveperl.blogspot.com+hash"&gt;hash&lt;/a&gt;:&lt;br /&gt;&lt;pre&gt;# retaining @color from above&lt;br /&gt;&lt;code&gt;my %color;&lt;br /&gt;for my $item (@color) {&lt;br /&gt;$color{$item} = 1;  # 1 or any "true" value&lt;br /&gt;}&lt;/code&gt;&lt;br /&gt;if ($color{'red'}) {&lt;br /&gt;print "Red found!\n";&lt;br /&gt;}&lt;/pre&gt;Here I loaded the hash &lt;code&gt;%color&lt;/code&gt; with the items in the original array &lt;code&gt;@color&lt;/code&gt;. The items themselves are the keys. The values are something "true" - it doesn't matter what, just as long as it's "true," so I use &lt;code&gt;1&lt;/code&gt;. Now, &lt;code&gt;$color{$foo}&lt;/code&gt; is true if the hash &lt;code&gt;%color&lt;/code&gt; contains the item &lt;code&gt;$foo&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;There's a succinct way to create the hash above:&lt;pre&gt;&lt;em&gt;my %color = map { $_, 1 } @color;&lt;/em&gt;&lt;/pre&gt;This is essentially the same as the &lt;code&gt;for&lt;/code&gt; loop, but shorter and more idiomatic.</content><link rel='alternate' type='text/html' href='http://effectiveperl.blogspot.com/2006/01/idiomatic-perl-hash-as-set.html' title='Idiomatic Perl: The Hash as a Set'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20900696&amp;postID=113725801997326859' title='2 Comments'/><link rel='replies' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/113725801997326859/comments/default' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://effectiveperl.blogspot.com/feeds/posts/default/113725801997326859'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20900696/posts/default/113725801997326859'/><author><name>Joseph N. Hall</name><uri>http://www.blogger.com/profile/08997289553387618189</uri><email>noreply@blogger.com</email></author></entry></feed>