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. :)

Sunday, November 28, 2010

Schwartzian transform explained

Did you google for "explain schwartzian transform"?

If yes, then you've come to the right place. If no, then you've come to the right place (I wouldn't want to loose out on a prospective reader now, would I?). Please read on - you will find this worth your time.

This post aims to dissect the Schwartzian Transform, and explains it through an example. And while doing this, it shows how cool Perl is.

But what is the Schwartzian Transform? - A way to efficiently sort a list of items.

Why the name? - Legend has it that Randal "Merlyn" Schwartz (wiki page) demonstrated a Perlish version of a Lisp idiom to speed up sorting. And since that day, every time you use the word "Schwartzian Transform", somewhere far beyond the distant seas, the Randal smiles.

How does it speed up sorting? - Patience my child. The blooming of a flower is an analog process, revealing each petal gracefully unlike the open-throw-catch-roll-close process of a frog's tongue. (Yummy?)

Why do I need to know it? - Because it's fun.

Will each line be preceded by a question in the bold font? - Not from now on.

Suppose you have an array of salesman objects - @salesmen, each of which consists of the following attributes:

1. name           # salesman name
2. base_salary    # base salary
3. n_cust_conned  # number of customers conned
4. comm_per_sale  # commission per sale

And the sub routine which calculates the total salary of the salesman is:

sub get_total_sal {
    my ($obj) = @_;
    my $total_salary = $obj->{base_salary} + 
                       $obj->{n_cust_conned} * $obj->{comm_per_sale};
    return $total_salary;
}

If you were told to sort this array based on the total salary, what would be your code snippet which does the sort?

Without prior knowledge of the transform, maybe something like this:

my @sorted_salesmen = sort { $a->get_total_sal() 
                             <=>
                             $b->get_total_sal() } @salesmen;

This would do the job. But what limitation does it induce in your code?

Think about it.

Perl program to simulate tail -f

Created a github account to share my bin with the rest of the world.

Had written a perl program to simulate tail -f quite some time ago. Put it up there. Enjoyed writing it. Hope you feel the same while playing with it.

Here it is: simtail.pl

Bounced back to blogspot

#!/usr/bin/perl
use strict;
use warnings;
print "Because you cannot install plugins on wordpress.com blogs";
print "Which means I cannot install this cool plugin which";
print "lets me indent and syntax highlight my Perl code\n";
exit(0);

Wordpress.com has the <code> tag - which for the average programmer means that I can use this tag to copy paste my code on the blog and it will show up all nice and indented. But from the usage, code tag just "encodes" HTML i.e. you don't need to escape < tag in #include <stdio.h>

The code tag doesn't indent. It doesn't highlight. It just converts the text to code font. The forums tell you to post your code in pre tags. Pre tags work but not always. As per Shuvam's comment below - the quirk might be with the specific template I am working with. So I created a test page on wordpress -

Test page to check code layout on wordpress.com

and tried it out with a 10-15 initial templates. None of them did the trick. I am not going to try out the 100+ templates that they offer and neither would I compromise on the rest of the layout just because some theme shows the pre tags cleanly.

Wednesday, November 17, 2010

Moved to wordpress

Wanted to play with wordpress for quite some time and also wanted to see how difficult/easy it is to migrate posts from blogspot to wordpress.

Verdict: It don't get any simpler than this. Wordpress has an inbuilt import feature. All you need to do is to point it to the blogspot blog url and it asks whether you are allowing this or not. If you give it a nod, it does what's needed. Clean.

Point and click - Here

Thursday, October 21, 2010

Saturday, October 16, 2010

Cygwin + Poderosa = bye bye cmd.exe

Does your job require you to boot up your box in Windows instead of Ubuntu? Do you miss the vi-mode on the command line? Do you become nostalgic at the mention of multiple shell tab sessions? Do you feel like bashing your boss's head if he says he won't shell out any extra money to buy you a shiny new Windows power shell (I think it should be worth a try)

Fear not, for the open source world has made efforts to bring you closer to your $HOME.
 
Cygwin - a Linux like environment for Windows gives you all the tools that you missed on cmd.exe - find, grep, ls and many more. You can download Cygwin from Cygwin home

Just remember one thing - when you install it for the first time - do a basic installation - i.e. do not select too many packages. Let the default installation do its job. You can always add new packages by running the setup again (no - it won't erase the previous setup - that's just cygwin's way of adding new packages).

Speeding up ppm module installation

Now that I have to (not that I want to) work on Windows, I am doing what I can to make my experience on it as comfortable as it was on Ubuntu - at least for the command line part. So a few initial posts will contain links, tips and all that I did to break the barriers of cmd.exe. Irony - I am blogging from my roommate's stylish Dell Studio XPS which is running Ubuntu :)

I recently installed Active state Perl and while playing along I tried to do a few module installations. Invoking ppm-shell gives you a cpan shell like prompt where in you can install all the modules that Active state has to offer. A serious pain in this process is that after downloading and installing each module, it generates HTML as a part of its document repository and this slows down the installation to a great extent.

And what does that deny you? No more under 30 second installs of File::ReadBackwards and trying to see if the apache access log when read backwards spouts out satanic verses. :)

So I did what I do when I am caught in a fix like this - Seek the wisdom of the monks. And as always, I leave the monastery with a happy grin. Following is the link to my post and the replies:

Perl monks query

I have always got a reply to my queries on perlmonks.org within 2-3 hours of my posting a question. I guess that's their unwritten SLA :)