s2vs(3f) - [M_strings:TYPE] given a string representing numbers
return a numeric array
(LICENSE:PD)
function s2vs(line[,delim])
character(len=*) :: line
doubleprecision,allocatable :: s2vs(:)
The function S2VS(3f) takes a string representing a series of numbers
and converts it to a numeric doubleprecision array. The string values
may be delimited by spaces, semi-colons, and commas by default.
LINE Input string containing numbers
DELIM optional list of delimiter characters. If a space is
included, it should appear as the left-most character
in the list. The default is " ;," (spaces, semi-colons,
and commas).
S2VS doubleprecision array
Sample Program:
program demo_s2vs
use M_strings, only : s2vs
implicit none
character(len=80) :: s=' 10 20e3;3.45 -400.3e-2;1234; 5678 '
real,allocatable :: values(:)
integer,allocatable :: ivalues(:)
integer :: ii
values=s2vs(s)
ivalues=int(s2vs(s))
call reportit()
contains
subroutine reportit()
write(*,*)'S2VS:'
write(*,*)'input string.............',&
& trim(s)
write(*,*)'number of values found...',&
& size(values)
write(*,*)'values...................',&
& (values(ii),ii=1,size(values))
write(*,'(*(g0,1x))')'ivalues..................',&
& (ivalues(ii),ii=1,size(values))
end subroutine reportit
end program demo_s2vs
Expected output
S2VS:
input string............. 10 20e3;3.45 -400.3e-2;1234; 5678
number of values found... 6
values................... 10.0000000 20000.0000 3.45000005
-4.00299978 1234.00000 5678.00000
ivalues.................. 10 20000 3 -4 1234 5678
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | string | |||
character(len=*), | optional | :: | delim |
function s2vs(string,delim) result(darray)
! ident_64="@(#) M_strings s2vs(3f) function returns array of values from a string"
character(len=*),intent(in) :: string ! keyword to retrieve value for from dictionary
character(len=*),optional :: delim ! delimiter characters
character(len=:),allocatable :: delim_local
doubleprecision,allocatable :: darray(:) ! function type
character(len=:),allocatable :: carray(:) ! convert value to an array using split(3f)
integer :: i
integer :: ier
!-----------------------------------------------------------------------------------------------------------------------------------
if(present(delim))then
delim_local=delim
else
delim_local=' ;,'
endif
!-----------------------------------------------------------------------------------------------------------------------------------
call split(string,carray,delimiters=delim_local) ! split string into an array
allocate(darray(size(carray))) ! create the output array
do i=1,size(carray)
call string_to_value(carray(i), darray(i), ier) ! convert the string to a numeric value
enddo
!-----------------------------------------------------------------------------------------------------------------------------------
end function s2vs