RosettaCodeData/Task/Middle-three-digits/NetRexx/middle-three-digits.netrexx

47 lines
1.6 KiB
Plaintext

/* NetRexx */
options replace format comments java crossref symbols nobinary
sl = '123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345' -
'1 2 -1 -10 2002 -2002 0' -
'abc 1e3 -17e-3 4004.5 12345678 9876543210' -- extras
parse arg digsL digsR .
if \digsL.datatype('w') then digsL = 3
if \digsR.datatype('w') then digsR = digsL
if digsL > digsR then digsR = digsL
loop dc = digsL to digsR
say 'Middle' dc 'characters'
loop nn = 1 to sl.words()
val = sl.word(nn)
say middleDigits(dc, val)
end nn
say
end dc
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method middle3Digits(val) constant
return middleDigits(3, val)
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method middleDigits(digitCt, val) constant
text = val.right(15)':'
even = digitCt // 2 == 0 -- odd or even?
select
when digitCt <= 0 then text = 'digit selection size must be >= 1'
when \val.datatype('w') then text = text 'is not a whole number'
when val.abs().length < digitCt then text = text 'has less than' digitCt 'digits'
when \even & val.abs().length // 2 == 0 then text = text 'does not have an odd number of digits'
when even & val.abs().length // 2 \= 0 then text = text 'does not have an even number of digits'
otherwise do
val = val.abs()
valL = val.length
cutP = (valL - digitCt) % 2
text = text val.substr(cutP + 1, digitCt)
end
catch NumberFormatException
text = val 'is not numeric'
end
return text