On Sunday, December 23, 2018 at 6:45:39 PM UTC-6, Arthur T. wrote:
In
Gerard_Schildberger wrote:
Here is a short REXX program that finds the 1st number (decimal digits only) >in a character string, the search stops before the last period (if any) in a >filename.
Thank you. It's more general than I need because I'm looking for
a number that starts immediately after the period (when reversed).
But it's good to have working code to cannibalize so I can avoid some
of the stupid mistakes.
The REXX code could be condensed, but I left the code to be be broken down >so as to be easier to read and/or modify.
I tend not to condense my REXX code. Even when writing for
myself, I follow Martin Golding's advice: "Always code as if the
person who ends up maintaining your code will be a violent psychopath
who knows where you live."
--
Arthur T. - ar23hur "at" pobox "dot" com
Ah. My REXX program (shown previously) finds the first number in the
filename (before the first period), while your code finds the last number.
The modified REXX program to match what your algorithm does is (which has
to contend with an "extra" error condition):
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ /*REXX program finds decimal numerals in a filename before last period*/ /*------------------------ Only the last numerals (number) is found. */
parse arg fn /*fn could have embedded blanks.*/
fn= strip(fn) /*strip leading/trailing blanks.*/
if length(fn)==0 then do; say 'filename is empty.'; exit; end
rev= reverse(fn) /*reverse the filename string. */
dot= pos(., rev) /*find location of the period. */
if dot==0 then say 'no period in the filename:' fn
if dot==0 then dot= 1 /*test for special case: no dot*/
b = verify(rev, 0123456789, 'M', dot) /*find beginning of a number. */
if b==0 then do; say 'no number found.'; exit; end
e = verify(rev, 0123456789, , b) - 1 /*find the "end" of the number. */
if e<1 then e= length(rev) /*handle case of just a number. */
num= reverse( substr(rev, b, e-b+1) ) /*extract the number, reverse it*/
say 'number is: ' num @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
I normally use more whitespace, but this newsgroup reader condenses (er, butchers) whitespace. Comments are normally a bit further to the right. _____________________________________________ Gerard Schildberger
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)