#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ( 'key1' => 'test1', 'key2' => 'test2', 'key3' => 'test2', 'key4' => 'test4', ); print Dumper \%hash; my @oldKeys = sort keys %hash; print Dumper \@oldKeys; # Remove what ever keys you want to keep e.g. key1 key3 splice @oldKeys, 0, 1; splice @oldKeys, 1, 1; print Dumper \@oldKeys; my @newKeys = ('key5', 'key6'); @hash{@newKeys} = delete @hash{@oldKeys}; print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'key2' => 'test2', 'key3' => 'test2', 'key1' => 'test1', 'key4' => 'test4' }; $VAR1 = [ 'key1', 'key2', 'key3', 'key4' ]; $VAR1 = [ 'key2', 'key4' ]; $VAR1 = { 'key6' => 'test4', 'key5' => 'test2', 'key1' => 'test1', 'key3' => 'test2' };