This code is for awk
, but easily adaptable to C/C++/C#
— in awk
, all indices are "1-based"
instead of "0-based"
- the leading edge "="
of the reference string is simply pre-shifting the positions. remove that "="
for any 0-based languages`
function __(_) { # input - Eng. month names, any casing, min. 3 letters
# output - MM : [01-12], zero-padded
return \
((_=toupper(_)) ~ "^[OND]" ? "" : _<_) \
(index("=ANEBARPRAYUNULUGEPCTOVEC", substr(_ "",_+=_^=_<_,_))/_)
}
The reference string might look odd at first -
the 2nd + 3rd letters of month names constitute a unique set
So OP can input the english name of the months, full or abbreviated, and it'll return a zero-padded 2-digit month number. If you need it to be in integer form, then just scrub out the middle line that performs the padding.
You'll notice only 1 input variable declared and no other temp variables whatsoever - one of awk
's major strengths is its extreme agility when it comes to dynamic typing of variables,
even for truly illogical operations like taking the "0th-power"
of a string
variable like
"Boston" ^ 0
This would seamlessly coerce that variable to a numeric
data type, with a new value of 1
.
This flexibility enables the recycling and re-using of the input temp variable(s) for any other purpose the moment the original input value(s) is/are no longer needed.