Slicing hashes

 | Perl | 1 Comment | 0 TrackBacks

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?

No TrackBacks

TrackBack URL: http://www.kiffingish.com/cgi-bin/mt/mt-tb.cgi/50

1 Comment

Now, Lets see some AIML.

Leave a comment

Recent Entries

Stupid light-pole
Damn. I bashed in the left side of my car against that stupid light-pole while backing out of the ... »
Enjoy life
The man standing next to me was wearing a fancy suit and carrying a leather briefcase in his right ... »
Gale-force winds
"The true (golf) pilgrim wanders off to the outlying, the more humble and obscure courses. For it is on ... »
Secret powers
The front nine was quite a display of supernaturally and (almost) perfectly consistent golfing gusto: Par, par, par, par, ... »
No more winter greens
The cool, windy and grayish afternoon calls me onward. It's drizzling on my windshield but I do not care. ... »