Access Menu

Site Navigation

« A dog named Kyffin | Main | Dutch handicap »

October 31, 2005

Slicing hashes ~ Perl stuff

An elegant way to mix and match hash entries is by using the technique of hash slices. For example, let's just say that you have defined the following hash:

my $config = {
  firstname => 'Joseph',
  lastname => 'Smith',
  user => 'pickle',
  ...
  database => 'fruits',
  password => 'z4KLf3&3z',
  ...
  host => 'zeta.demon.nl',
};

Suppose that you are only interested in the hash-keys related to the database stuff. Then you can use the following fancy one-liner:

my ($dbhost, $dbdatabase, $dbuser, $dbpassword) =
  @$config{ qw( host database user password ) };

As the saying goes, there is always more than one way to do it with Perl. Pretty neat stuff, wouldn't you say?

Posted at 1:12 PM

Trackback Pings

TrackBack URL for this entry:
http://www.kiffinsblog.com/cgi-bin/mt/mt-tb.cgi/155

1 Comment(s)

1 /

Now, Lets see some AIML.

Posted by Yuichan at November 1, 2005 5:49 PM

Post a comment

Thanks for signing in, . Now you can comment. (sign out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Remember me?