This works in that the final "foreach" returns the fields in the order of the "Position" value but I don't understand why (I only got it to work by giving it a go!) or if it's safe to rely on. How does the hash of hashes find the "Position" value when the "fieldname" level has been skipped? That is:use strict; my %tables = ( 'Table_1' => {'Schema' => 'dbo'}, 'Table_2' => {'Schema' => 'dbo'} ); my %fields = ( 'Table_1' => { 'Field_1' => {'Datatype' => 'int', 'Position' => 1}, 'Field_2' => {'Datatype' => 'char', 'Position' => 2}, 'Field_3' => {'Datatype' => 'int', 'Position' => 3} }, 'Table_2' => { 'Field_1' => {'Datatype' => 'float', 'Position' => 1} } ); foreach my $table (sort keys %tables) { foreach my $field (sort {$fields{$table}{'Position'}{$a} <=> $fields +{$table}{'Position'}{$b}} keys %{$fields{$table}}) { print "$table -> $field\n"; } }
and$fields{$tablename}{$fieldname}{'Position'}
Can someone advise on why this works and whether it's safe to rely on? Thanks.$fields{$tablename}{'Position'}
In reply to Sorting a hash by a well buried key by lb483
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |