Re: [Golf-ish] Ordered hash keys



"Damian James" <djames@xxxxxxxxxxxxx> wrote in message
news:slrnd75cij.mk.djames@xxxxxxxxxxxxx
: Hi folks,
:
: So I wanted to define a lookup table that I would later need to
: iterate over in a specific order. That's easy enough, there are
: many ways to do it; but I specifically wanted to keep the neat,
: visually oriented table in my code:
:
: my @mappings = (
: field1 => 'otherLongwindedDirectoryAttribute',
: field2 => 'kitchenSink',
: field3 => 'userDistanceBetweenEyes'
: );
: my %lookup = @mappings;
: my @fields = @mappings[ map $_*2, 0..($#mappings/2) ];
:
: ...where obviously I am only interested in %lookup and @fields, but
: want to keep the table in the interests of easy alteration.


I have previously done somthing like:

my %lookup = (
field1 => 'otherLongwindedDirectoryAttribute',
field2 => 'kitchenSink',
field3 => 'userDistanceBetweenEyes'
);

my @fields = sort(keys(%lookup));

But this relies on sort giving you the order you want rather than a specific
order which may not necessarily match sort's output. Unless you can define
some sort routine that matches your preferred order. In the example given
it would match but that is probably just a happy coincidence of your example
data. How critical is the 'preferred order' of the keys compared to a
'sorted order' of the keys for later processing?

P


.