Viewing contents of file '../idllib/iuedac/iuelib/pro/getnum.pro'
;******************************************************************************
;+
;*NAME:
;
; GETNUM
;
;*CLASS:
;
; Input/output
;
;*CATEGORY:
;
;*PURPOSE:
;
; Routine to extract numbers from a string with various delimiters
; between the numbers. The delimiters can be anything and in any
; quantity except plus and minus sign and decimal points (+ - .)'s.
;
;*CALLING SEQUENCE:
;
; PRO GETNUM,INPUT,NUMBERS,CT
;
;*PARAMETERS:
;
; INPUT (REQ) (I) (0 S)
; The string that is to be searched for numbers.
;
; NUMBERS (REQ) (O) (0 F)
; The returned array of numbers that where found in
; the string.
;
; CT (OPT) (O) (0 F)
; The count of numbers found in the string.
;
;*EXAMPLES:
;
; x = ' 12, 34 % 56.9, 23.5'
; GETNUM,x,n,ct
; n will equal [12.0,34.0,56.9,23.5]
; ct will equal 4.0
;
;*SYSTEM VARIABLES USED:
;
;*COMMON BLOCKS:
;
;*INTERACTIVE INPUT:
;
;*SUBROUTINES CALLED:
;
; PARCHECK
;
;*EMBEDDED SUBROUTINES:
;
;*FILES USED:
;
;*SIDE EFFECTS:
;
;*RESTRICTIONS:
;
;*NOTES:
;
; Tested with IDL version 3.5.1 (VMS VAX) 1 JUN 1994
; Tested with IDL version 3.5.1 (Sun) 1 JUN 1994
;
;*PROCEDURE:
;
; Convert the number into a BYTE array. Replace anything that is
; not a number or a plus or minus sign or decimal point with a blank
; space. Convert that back to a string and remove excess blanks.
; Convert this to a floating point array.
;
;*I_HELP nn:
;
;*MODIFICATION HISTORY:
;
; VERSION 1 Walker 16 JULY 1994
;
;-
;******************************************************************************
pro getnum,input,numbers,ct
;
npar = n_params(0)
if (npar eq 0) then begin
print,'GETNUM,INPUT,NUMBERS,CT'
retall
endif ; npar
parcheck,npar,2,'GETNUM'
a = input(0)
b = byte(a)
;
; locate any place in input string where there is not a number or a
; plus, minus or decimal point
;
pos = where((b ne 43) and (b ne 45) and (b ne 46) and $
not ((b gt 47) and (b lt 58)),c)
case 1 of
;
; if the entire string is non-numeric or the string is null, set output
; values to 0 and return
;
(c eq strlen(a)) or (total(b) eq 0) : begin
numbers = [0]
ct = 0
return
end
;
; if the entire string is numeric, set output and return
;
(c eq 0) : begin
numbers = [float(a)]
ct = 1
return
end
;
; else set all characters which are non-numeric or not plus, minus
; or decimal point equal to a blank space (byte = 32)
;
else : b(pos) = 32
endcase ; true
;
; remove all leading and trailing blanks and reduce blanks in the string
; to a single space.
;
b = byte(strtrim(strcompress(string(b)),2))
;
; create an array of positions of '+' and '-' signs and remove any blank
; space after them and replace with zeros.
;
check = where(((b eq 43) or (b eq 45)),c)
if (c ne 0) then begin
for i = 0,c-1 do begin
if ((check(i)+1) lt strlen(string(b))) then begin
if (b(check(i)+1) eq 32) then b(check(i)+1) = 48
endif ; check(i)
endfor ; i
endif ; (c ne 0)
;
; create an array of positions where the string has a blank space.
;
check = where(b eq 32,c)
;
; go through string to convert numbers to floating point
;
if (c eq 0) then begin
numbers = [float(string(b))]
ct = 1
endif else begin
pos = [0,check,strlen(string(b))-1]
ct = c + 1
numbers = fltarr(ct)
for i = 0,c do numbers(i) = float(string(b(pos(i):pos(i+1))))
endelse ; (c eq 0)
end ; getnum