[Golf-ish] Ordered hash keys
- From: Damian James <djames@xxxxxxxxxxxxx>
- Date: Fri, 29 Apr 2005 22:18:52 GMT
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.
So how might I avoid defining @mappings? Is there a simpler, shorter
or more elegant 'hashificator' that might be suitable in place of the
arithmetic one I've shown? People maintaining this later may well not
be able to find their own um, keyboard, with detailed directions and
a map, but just might be able to change a lookup table if it's clear
enough and the instructions are sufficiently detailed.
I know I can:
my %lookup;
@lookup{ my @fields = qw/field1 field2 field3/ } =
qw/other kitch user/;
But that sacrifices the vertical orientation, and that just won't fit
neatly with the rather long directory attribute names.
This is obviously a style question more than anything, so the more
playful the suggestions the better I guess ;).
--damian
--
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
$:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/} __END__
Just another Perl Hacker,
.
- Follow-Ups:
- Re: [Golf-ish] Ordered hash keys
- From: Peter Wyzl
- Re: [Golf-ish] Ordered hash keys
- From: Tad McClellan
- Re: [Golf-ish] Ordered hash keys
- From: Fabian Pilkowski
- Re: [Golf-ish] Ordered hash keys
- Prev by Date: FAQ 3.14 How can I use X or Tk with Perl?
- Next by Date: Re: Finally a better script!
- Previous by thread: FAQ 3.14 How can I use X or Tk with Perl?
- Next by thread: Re: [Golf-ish] Ordered hash keys
- Index(es):
Relevant Pages
|
|