str_to_int_array_with_mapping Function

public pure function str_to_int_array_with_mapping(str, ichars, iints) result(array)

Convert a string to a numeric array by mapping characters to integers (user-specified)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str
character(len=1), intent(in), dimension(:) :: ichars

characters to process

integer, intent(in), dimension(:) :: iints

int values of the chars

Return Value integer, dimension(:), allocatable


Source Code

    pure function str_to_int_array_with_mapping(str, ichars, iints) result(array)
        character(len=*),intent(in) :: str
        character(len=1),dimension(:),intent(in) :: ichars !! characters to process
        integer,dimension(:),intent(in) :: iints !! int values of the chars
        integer,dimension(:),allocatable :: array
        integer :: i
        integer,dimension(1) :: iloc
        allocate(array(len(str)))
        do i = 1, len(str)
            iloc = findloc(ichars,str(i:i))
            if (iloc(1)>0) then
                array(i) = iints(iloc(1))
            else
                error stop 'error: could not map character: '//str(i:i)
            end if
        end do
    end function str_to_int_array_with_mapping