Friday, December 24, 2010

Alpha(bet) Geek


#/usr/bin/perl
use strict;
use warnings;
my $str = $ARGV[0];
my @alpha_arr = ('a' .. 'z');
my %str_tok_hash = map { lc($_) => undef } 
                   grep { /[a-z]/i } 
                   split(//, $str);
my @diff = grep { not exists $str_tok_hash{$_} } @alpha_arr;
$str = "'The string ($str) contains all the letters of the alphabet";
$str .= " except (".join(' ', sort @diff).").' - contains all the letters"; 
$str .= " of the alphabet. :)";
print "$str\n";

Sunday, December 12, 2010

The "Read More" plugin and how blogger beat me to it

For those who are searching for a way to add a "Read More" link to their blogspot blogs - click here

The rest of the post discusses the other "Read More" plugin that I found on the net, its limitations and how I wrote one myself (similar in its functionality to one in the above link) and its source code. Its a good study on how to preprocess your post after you click "PUBLISH POST" button but before blogger actually publishes it. The geeks may continue reading.

Quite some time ago, I was writing a humor article on my blog - Barberic Creatures It badly needed a "Read More" link because as per the the setting I had, I display 10 posts on a page and if one of them is too long (this one was) it dissuades the reader from scrolling all the way down to see the other ones. I couldn't find any plugins at that time (2008/11) and left it at that.

But it came back to haunt me when I wrote Schwartzian Transform Explained. This time I got a hit - Snaphow read more plugin

The instructions were clear and it works. But it didn't address my needs because:

Schwartzian transform used in a C Program

If you are looking for an in depth explanation of the Schwartzian transform with an example, please refer to my earlier post - Schwartzian Transform Explained

I was getting bored and asked a friend to give me something interesting to code in C. He being who he is, told me to write an implementation of the Schwartzian Transform (henceforth referred to as ST) in C. During the process of writing that program, I realized how Perl makes us focus on the problem at hand rather than the nuances of the language. This post aims provide the source and explain it.

The source is uploaded here - schwartz.c

Now that the (fo|sou)rce is with you, I shall explain the main() function because all the other functions are called from main() and their body is easy to understand once you know their purpose in the main(). Or rather, their main() purpose. :)