[REBOL] Re: R: Re: [REBOL]
From: joel:neely:fedex at: 31-Jan-2002 19:07
Hi, Alessandro,
I suggest using strings instead of words. Strings always evaluate
to themselves, while some expressions could end up trying to take
the value of MAR or GIU with unexpected results! ;-)
Alessandro Manotti wrote:
> month-list: [gen jan feb feb mar mar apr apr mag may giu jun lug
> jul ago aug set sep ott oct nov nov dic dec]
>
> select month-list 'feb
> select month-list 'giu
>
> Of course, it is good only for ITALIAN->ENGLISH (for reverse, we
> should reverse the order of the items...).
>
You can avoid having two lists by using triples instead of pairs,
as follows:
translate-month: func [month-name [string!]] [
select [
"gen" "jan" "gen" "feb" "feb" "feb" "mar" "mar" "mar"
"apr" "apr" "apr" "mag" "may" "mag" "giu" "jun" "giu"
"lug" "jul" "lug" "ago" "aug" "ago" "set" "sep" "sep"
"ott" "oct" "ott" "nov" "nov" "nov" "dic" "dec" "dic"
] month-name
]
which works as
>> translate-month "jan" == "gen"
>> translate-month "lug" == "jul"
>> translate-month "ott" == "oct"
>> translate-month "oct" == "ott"
>
> date1: [gen feb mar apr mag giu lug ago set ott nov dic]
> date2: [jan feb mar apr may jun jul aug sep oct nov dec]
>
> idx: index? find date1 'gen
> date2/:idx
>
> * Advantage: reverse is ok...
>
> idx: index? find date2 'gen
> date1/:idx
>
A drawback of using ... INDEX? FIND ... is the behavior when an
invalid argument is supplied. Packaged up as a function, with a
refinement to control the direction of translation, we can use the
above idea as:
trans-mon: func [mon-name [string!] /to-it /local date-it date-en] [
date-it: [
"gen" "feb" "mar" "apr" "mag" "giu"
"lug" "ago" "set" "ott" "nov" "dic"
]
date-en: [
"jan" "feb" "mar" "apr" "may" "jun"
"jul" "aug" "sep" "oct" "nov" "dec"
]
either to-it [
pick date-it index? find date-en mon-name
][
pick date-en index? find date-it mon-name
]
]
which behaves this way:
>> trans-mon "gen" == "jan"
>> trans-mon "ott" == "oct"
>> trans-mon/to-it "oct" == "ott"
>> trans-mon/to-it "may" == "mag"
... but ...
>> trans-mon "abc"
** Script Error: index? expected series argument of type: series port
** Where: trans-mon
** Near: pick date-en index? find date-it
So, regardless of the strategy chosen, handling erroneous cases should
be part of the plan, IMHO.
-jn-