in reply to Modifying order of a hash

Hash of Hashes or Array of Hashes? Or Hash of Arrays which is what your syntax suggests?

Lets say it is an array of hashes:

my @new; foreach $fn (@old) { $new[$fn->{FirstNum}]= $fn; delete $fn->{FirstNum}; }

If it is a hash of hashes:

my %new; foreach $fn (keys %old) { $new{$fn->{FirstNum}}= $fn; delete $fn->{FirstNum}; }

UPDATE: As an afterthought: Use Data::Dumper. It will show you the right syntax you should have used in your question. And also it will tell you which of the wildly different answers posted in this thread does what you need, if you use it to print the data structure before and after.

Replies are listed 'Best First'.
Re^2: Modifying order of a hash
by monaghan (Novice) on Sep 25, 2008 at 21:01 UTC
    Here is what code I have written with a small change to the input hash:
    my %file_attachments = ( '103496-1' => { 'CLVD' => '5678', 'COMP' => '1234', 'FD' + => '0010', 'File' => 'text.txt'}, ); print Dumper(%file_attachments); print "\n"; my %newfile_attachments; my $fn; my %newfile_attachments; foreach $fn (keys %file_attachments) { $newfile_attachments{$fn->{FirstNum}}= $fn; delete $fn->{FirstNum}; } print Dumper(%newfile_attachments); print "\n";
    and my output looks like:
    > perl hashofhash.pl "my" variable %newfile_attachments masks earlier declaration in same s +cope at hashofhash.pl line 17. $VAR1 = '103496-1'; $VAR2 = { 'FD' => '0010', 'COMP' => '1234', 'CLVD' => '5678', 'File' => 'text.txt' }; Can't use string ("103496-1") as a HASH ref while "strict refs" in use + at hashofhash.pl line 19.

      Ah yes, should have tested my code. The first error is because you defined %newfile_attachments twice. The second error goes away if you use this:

      foreach $fn (values %file_attachments) {

      In your example you don't have a key 'FirstNum' in the hash. Be prepared to see an error message "Use of uninitialized value..." because of this. If you have subhashes with no key 'FirstNum', then you have to test for that and do something sensible with those subhashes

      Btw, the output of Dumper looks much better if you use Dumper(\%x) instead of Dumper(%x)

        That worked. Many thanks. Now I want to expand this a bit more by adding more values inside the hash:
        my %hash = ( '103496-1' => [{ 'CLVD' => '5678', 'COMP' => '1234', 'FD +' => '0010', 'Files' => [{'File' => 'text.txt', 'hash' => 'a538346ad3 +485'},{'File' => 'text2.txt', 'hash' => '237d97892376a'}] }] ); print Dumper(%hash); print "\n"; my %newhash; my $fn; foreach $fn (values %hash) { $newhash{$fn->{COMP}}= $fn; delete $fn->{COMP}; } print Dumper(%newhash); print "\n";
        this gives me the error and wrong output as:
        Pseudo-hashes are deprecated at hashofhash.pl line 18. Use of uninitialized value in hash element at hashofhash.pl line 18. Pseudo-hashes are deprecated at hashofhash.pl line 19. $VAR1 = ''; $VAR2 = [ { 'FD' => '0010', 'COMP' => 1234, 'CLVD' => '5678', 'Files' => [ { 'hash' => 'a538346ad3485', 'File' => 'text.txt' }, { 'hash' => '237d97892376a', 'File' => 'text2.txt' } ] } ];
        Any ideas?