Re: Bidirectional mapping




"slebetman@xxxxxxxxx" <slebetman@xxxxxxxxx> wrote in message
news:1187520156.714910.61820@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Aug 19, 4:49 pm, "Andrew Holme" <and...@xxxxxxxxxx> wrote:
If I setup an array like this:

set day_num(MON) 1
set day_num(TUES) 2
set day_num(WED) 3
set day_num(THURS) 4
set day_num(FRI) 5
set day_num(SAT) 6
sat day_num(SUN) 7

I can map symbolic names to numbers using $day_num($day); but what is the
simplest, most elegant way to map in both directions e.g. convert THURS
to 4
or convert 3 to WED? Do I have to declare a list [MON TUES WED THURS FRI
SAT SUN]? I don't like having to list the symbols twice in my code.


For quick and dirty and highly readable I usually do it like:

foreach {day num} {
MON 1
TUES 2
WED 3
THURS 4
FRI 5
SAT 6
SUN 7
} {
set day_num($day) $num
set day($num) $day
}


I like this.


.



Relevant Pages

  • Re: Bidirectional mapping
    ... SAT SUN]? ... foreach { ... set day_num($day) $num ... Tcl makes metaprogramming so painless that not many ...
    (comp.lang.tcl)
  • Re: Bidirectional mapping
    ... most elegant way to map in both directions e.g. convert THURS to 4 ... SAT SUN]? ... proc day_num { ... foreach {day num} { ...
    (comp.lang.tcl)
  • Re: Bidirectional mapping
    ... SAT SUN]? ... proc day_num { ... foreach {day num} { ... or something alike ...
    (comp.lang.tcl)
  • Re: Bidirectional mapping
    ... foreach {day num} { ... SUN 7 ... proc, so you don't have to do it every time you invoke the proc. ...
    (comp.lang.tcl)
  • Re: re calendar to 12 month calendar
    ... contrl shift enter, ... I am getting #NUM! ... The days are row wise ie Sun to Sat is this my problem ...
    (microsoft.public.excel.worksheet.functions)