string_to_values(3f) - [M_strings:TYPE] read a string representing numbers into a numeric array (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
Author
License
subroutine string_to_values(line,iread,values,inums,delims,ierr)
character(len=*) :: line integer :: iread real :: values(*) integer :: inums character(len=*) :: delims integer :: ierr
This routine can take a string representing a series of numbers and convert it to a numeric array and return how many numbers were found.
LINE Input string containing numbers IREAD maximum number of values to try to read from input string
VALUES real array to be filled with numbers INUMS number of values successfully read (before error occurs if one does) DELIMS delimiter character(s), usually a space. must not be a null string. If more than one character, a space must not be the last character or it will be ignored. IERR error flag (0=no error, else column number string starts at that error occurred on).
Sample Program:
program demo_string_to_values use M_strings, only : string_to_values implicit none character(len=80) :: s= 10 20e3;3.45 -400.3e-2;1234; 5678 integer,parameter :: isz=10 real :: array(isz) integer :: inums, ierr, iiExpected outputcall string_to_values(s,10,array,inums, ;,ierr) call reportit()
call string_to_values(10;2.3;3.1416,isz,array,inums, ;,ierr) call reportit()
contains subroutine reportit() write(*,*)string_to_values: write(*,*)input string.............,trim(s) write(*,*)number of values found...,inums write(*,*)values...................,(array(ii),ii=1,inums) end subroutine reportit end program demo_string_to_values
string_to_values: 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 string_to_values: input string............. 10 20e3;3.45 -400.3e-2;1234; 5678 number of values found... 3 values................... 10.0000000 2.29999995 3.14159989
John S. Urban
Public Domain
Nemo Release 3.1 | string_to_values (3m_strings) | January 10, 2025 |