in reply to Re^2: Retaining hash order with Config::General?
in thread Retaining hash order with Config::General?
Based on the code I've seen, you are best off just resorting the hash keys to the order you want just prior to display. Alphabetical is easiest, but you can resort the hash order with a custom test like this:
#!/usr/bin/perl use strict; use warnings; use Config::General qw{ParseConfig}; use Tie::IxHash; my $file = 'test.cfg'; tie my %CFG, "Tie::IxHash"; %CFG = ParseConfig( -ConfigFile => $file, -Tie => "Tie::IxHash", ); print "When freshly loaded:\n"; print join "\n", keys %CFG; print "\n\n"; (new Config::General( -ConfigHash => \%CFG, -Tie => "Tie::IxHash", ))->save_file($file); # SaveConfig appears "equally bad" at this.. %CFG = ParseConfig( -ConfigFile => $file, -Tie => "Tie::IxHash", ); print "When saved & reloaded:\n"; print join "\n", sort {myfunction($a, $b)} keys %CFG; sub myfunction { my($key1, $key2) = @_; if (substr($key1,length($key1)-1,1) == substr($key2,length($key2)- +1,1)) { return ($key1 cmp $key2); } else { return (substr($key1,length($key1)-1,1) cmp substr($key2,lengt +h($key2)-1,1)); } }
|
|---|