Monday, October 26, 2009

how to remove an element in a Perl array/list

splice does the trick:

@a = qw( A B C D E F G );
splice( @a, 3, 1 );


This will remove 'D' from the @a array. (Remember that indexing starts at 0.

how to reload a file in vim/gvim

It took me a while to discover this but to reload a file in vim/gvim simply do the following:


:e


Done!

Saturday, October 24, 2009

A basic probability question concerning throwing a die


Someone asked this question on my Facebook today:

If a fair sided die is thrown 6x, what is the probability of it landing 3x on the number 6?



My answer:
[BTW, C(n,k) refers to combination. While x^y refers to exponentation.]

Suppose you number the throws 1 to 6:
C(6,3) => which of the 6 throws hits a 6. (There should be only exactly 3. That's why C(6,3).

Then you have to figure out the number of ways the other 3 non-6 throws could come out:
=> 5^3
since they can be anything from 1 to 5.

Multiply those 2 things to get the total number of ways to throw 3 sixes with 6 throws.

But that doesn't answer the problem completely. To get the probability, you need to divide that by the total number of ways to throw a 6 sided die, which is 6^6.

So the complete answer is: (C(6,3)*5^3) / 6^6


So this might be useful for someone preparing for one of the acronym standardized tests like the GRE or the GMAT. :)

Monday, October 19, 2009

Problems with connecting to Xming from a remote system

My setup is this:

Main desktop running Windows XP (it's company provided so I can't just blow the OS away and install Linux). So I installed Xming on it so I can forward X connections from my secondary PC which is running Arch Linux.

I do the proper incantation on the Linux box as:

export DISPLAY=winxpipaddress:0.0

I check that the display is indeed 0.0 on my WinXP box. But I get the following error message:

AUDIT: Tue Oct 20 :31:12 2009: 5636 Xming: client 4 rejected from IP ipaddress_of_archlinuxbox}

After some searching, find the X0.hosts file in the Xming main directory and add the Arch Linux IP. And everything works just fine.

Try Try::Tiny for easier Perl 5 exception handling

Perl 5 exception handling is not an easy thing. For one thing, it makes use of the eval construct. Any Perl code that uses eval for exception handling directly moves a notch closer to a "write-only" mode. Basically, you have to figure out whether the eval means dynamic code compilation or exception handling.

This is the reason that there are is a good number of exception handling libraries on CPAN.

There are a number of worthy contenders for the exception handling crown, but for minimalist reasons, I like Try::Tiny. First of all, it doesn't try to do too much. Secondly, it covers the cases where using eval/if becomes difficult in practice. The use of $@ pretty much dominates the difficulty in using eval/if. In a nutshell, $@ must be localized to prevent clobbering previous values. At the same time, all die statements should have the proper effect.

Try::Tiny is currently at version 0.02. For the few times that I will need to write in Perl, this is definitely a the top of my list for exception handling.

Tuesday, October 13, 2009

vim 101: how to enable line numbers

To enable line numbers, simply enter the following command in command mode:

:set number

To turn off line numbers, do the following:

:set nonumber


Saturday, October 10, 2009

how to enable incremental search in vim

It's not that hard. All you do is:

:set incsearch


The key is to actually know the words to google for. Basically the installation of gvim on OpenSuse 11.1 did not have incremental search enabled. So I searched for smart search and somehow got the right phrase I needed. Once I knew that this feature meant incremental search, it was easy.

So for those who are unclear on what incremental search is according the vim help:


While typing a search command, show where the pattern, as it was typed
so far, matches. The matched string is highlighted. If the pattern
is invalid or not found, nothing is shown. The screen will be updated
often, this is only useful on fast terminals.







EMC rolling out a compute cloud?

The Register recently broke the news that EMC will be rolling out a compute cloud that will be competing with Amazon's EC2 compute cloud service. I suppose this is to be expected after EMC rolled out its own storage cloud service, Atmos.

It should be noted that EMC bought then spun off virtual computing company, VMWare. That turned out to be a great decision on their part. So it's no surprise that they came out with cloud storage and computing solutions. I'm definitely watching this with great interest.

Friday, October 9, 2009

How to find the absolute path of a script in Python

(Or for you perl-heads, how to do FindBin in Python. Thanks to Naveed for giving me the idea for this little tip).

In Python, you'll need os.path and sys
. So it will look like something like this:


import os
import sys

myabspath = os.path.abspath(os.path.dirname(sys.argv[0]))

Wednesday, October 7, 2009

How to serve your hg repository using http

Mercurial (hg, get it? ;) is one of a number of distributed version control systems. So this is an easy way to host an hg repository. So from the command line, cd into the repository you want to host and simply type:


hg serve

The repository will be located at http://localhost:8000/. If you want to access it from another computer you'll have to use the IP address or host address like so:

hg clone http://yourpc.example.net:8000/

Very cool.

Monday, October 5, 2009

Python 101: How to iterate a dictionary/hash



For those who don't know, python dictionary == perl hash.

The most common idiom uses the iteritems function:

mydict = { "a" : "Alpha", "b" : "Beta", "c" : "Charlie" }
for key, value in mydict.iteritems():
print key, value

The iteritems function returns an iterator that gives us key and value.
If for some reason you only wanted the keys, you can use iterkeys:

mydict = { "a" : "Alpha", "b" : "Beta", "c" : "Charlie" }
for key in mydict.iterkeys():
print key, mydict[key]


Or if you wanted the values only, you can use itervalues:

mydict = { "a" : "Alpha", "b" : "Beta", "c" : "Charlie" }
for value in mydict.itervalues():
print value





Like Python? Here are some links to some more articles on Python:
  1. Little Python SQLite tutorial
  2. Little Python optparse module tutorial
  3. Little python dictionary hack

Sunday, October 4, 2009

how to start a simple HTTP server using Python

In python 2.6, just type the following command in the directory where the files to be served are:

python -m SimpleHTTPServer

In Python 3.0, SimpleHTTPServer has been merged into http.server, so change the argument to -m to http.server.SimpleHTTPRequestHandler

how to set system date/time on Linux

To set the system time on Linux, use the date command. For example to set the date/time to October 4, 11:05 pm, do the following:

date 10042305


Note that this retains the current year. To change the year to 2009, use 100423052009