lb483 has asked for the wisdom of the Perl Monks concerning the following question:
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'}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting a hash by a well buried key
by haukex (Archbishop) on Sep 25, 2019 at 22:26 UTC | |
by lb483 (Initiate) on Sep 26, 2019 at 16:57 UTC | |
|
Re: Sorting a hash by a well buried key
by swl (Prior) on Sep 25, 2019 at 21:46 UTC |