Not sure if you've ever needed to rename a hash key before, but I have. I also noticed that it doesn't look like any of the languages I work with have a native method/function to rename hash keys. So, I've taken the liberty to write them. These are just for fun, and are released as public domain code snippets, feel free to use them in whatever you want. There are versions for Perl 6, Ruby, Perl 5 and Python. Enjoy!


Perl 6

Of course, first off, we have Perl 6, where I have implemented a 'rename' method directly in the Hash class, so all hashes will be able to use it.

Implementation

 ## augment class Hash {                ## Not supported by Rakudo yet.
 class Hash is also {                   ## Old syntax still used by Rakudo.
   method rename ($old,$new) { 
     self{$new} = self.delete($old);
   }
 }

Usage

my %example;
%example<hi> = 'hello world';
%example.perl.say;
%example.rename('hi','howdy');
%example.perl.say;

Ruby

Next up, I ported my Perl 6 implementation to Ruby, another language which allows you to modify core classes. As you will be able to tell, it was nearly an exact port, with only a few syntax differences to take into account.

Implementation

class Hash
  def rename(old,new)
    self[new] = self.delete(old)
  end
end

Usage

example = {}
example['hi'] = 'hello world'
p example
example.rename('hi','howdy')
p example

Perl 5

Most of my day to day programming is in Perl 5, so a version of this for it would be useful. Since hashes aren't really objects in the usual sense, this version is implemented as a subroutine that can be called on a hash reference. Not quite as pretty as the others, but it works.

Implementation

sub rename_key {
  my $hashref = shift;
  my $oldkey  = shift;
  my $newkey  = shift;
  $hashref->{$newkey} = delete $hashref->{$oldkey};
}

Usage

use Perl6::Perl 'p';
my %example;
$example{'hi'} = 'hello world';
p \%example;
rename_key( \%example, 'hi', 'howdy' );
p \%example;

Python

At work we use a product that embeds an ancient version of Jython as the scripting language. The version is based on Python 2.1, so nice methods such as .pop and the ability to inherit from 'dict' is not available. It is also not possible to monkey-patch native classes, so we're implementing this as a function that takes a dictionary object, instead of a method.

Implementation

def rename_key (hash,old,new):
  hash[new] = hash[old]
  del hash[old]

Usage

example = {}
example['hi'] = 'hello world'
print example
rename_key( example, 'hi', 'howdy' )
print example

That's all folks

Hope you've enjoyed the snippets of code. May not be very useful, but the above does show the similarities and differences between four programming languages. All of the above code has been tested and works just fine, but could definitely be improved with better type checking and/or type enforcing.

The usage example for Perl 5 requires the "Perl6::Perl" library which is available on CPAN.

Also, the Perl 6 version currently has an interesting side effect on the current 'master' branch of Rakudo. When rename is called, it wraps the value in an Array. So instead of { 'hi' => 'hello world' } you end up with { 'howdy' => ['hello world'] }. If anyone can tell me why it's doing this, please let me know, it's a rather strange behavior.

For a bonus snippet, if you like the p keyword from Ruby and Perl6::Perl, and wanted something similar for Perl 6, here's a quick sub you can put in place that allows you to replace %example.perl.say with p %example, which looks a bit nicer.

sub p ($object) {
  $object.perl.say;
}

It doesn't get much simpler (or lazy) than that! See you next time.

Changelog

Jan 13, 2010
Last update prior to importation to GreyNoise.
Jan 13, 2010
Initial version.