Re: [Golf-ish] Ordered hash keys
- From: "Peter Wyzl" <wyzelli@xxxxxxxxx>
- Date: Sat, 30 Apr 2005 05:54:39 GMT
"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
.
- References:
- [Golf-ish] Ordered hash keys
- From: Damian James
- [Golf-ish] Ordered hash keys
- Prev by Date: Re: Finally a better script!
- Next by Date: Re: Perfecting index.pl some more!
- Previous by thread: Re: [Golf-ish] Ordered hash keys
- Next by thread: FAQ 3.21 How can I compile my Perl program into byte code or C?
- Index(es):