M_CLI2: m_cli2::replace Interface Reference

M_CLI2 M_CLI2 module (Fortran)

Private Member Functions | List of all members
m_cli2::replace Interface Reference

## Private Member Functions
subroutine | replace_c (list, value, place)
subroutine | replace_i (list, value, place)
subroutine | replace_l (list, value, place)

## Member Function/Subroutine Documentation

## ◆ replace_c()

subroutine m_cli2::replace::replace_c | ( | character(len=:), dimension(:), allocatable | list,
|  | character(len=*), intent(in) | value,
|  | integer, intent(in) | place
| ) |  |
| private

## NAME

replace(3f) - [M_CLI2] replace entry in a string array at specified position (LICENSE:PD)

## SYNOPSIS

subroutine replace(list,value,place)

character(len=*)|doubleprecision|real|integer,intent(in) :: value character(len=:)|doubleprecision|real|integer,intent(in) :: list(:) integer, intent(out) :: PLACE

## DESCRIPTION

    replace a value in an allocatable array at the specified index. Unless the
    array needs the string length to increase this is merely an assign of a value
    to an array element.

    The array may be of type CHARACTER, DOUBLEPRECISION, REAL, or INTEGER>
    It is assumed to be sorted in descending order without duplicate values.

    The value and list must be of the same type.

## OPTIONS

    VALUE         the value to place in the array
    LIST          is the array.
    PLACE         is the subscript that the entry should be placed at

## EXAMPLES

Replace key-value pairs in a dictionary

    program demo_replace
    use M_CLI2, only  : insert, locate, replace
    ! Find if a key is in a list and insert it
    ! into the key list and value list if it is not present
    ! or replace the associated value if the key existed
    implicit none
    character(len=20)            :: key
    character(len=100)           :: val
    character(len=:),allocatable :: keywords(:)
    character(len=:),allocatable :: values(:)
    integer                      :: i
    integer                      :: place
    call update('b','value of b')
    call update('a','value of a')
    call update('c','value of c')
    call update('c','value of c again')
    call update('d','value of d')
    call update('a','value of a again')
    ! show array
    write(*,'(*(a,"==>",a,/))')(trim(keywords(i)),trim(values(i)),i=1,size(keywords))

    call locate_key('a',place)
    if(place.gt.0)then
       write(*,*)'The value of "a" is',trim(values(place))
    else
       write(*,*)'"a" not found'
    endif

    contains
    subroutine update(key,val)
    character(len=*),intent(in)  :: key
    character(len=*),intent(in)  :: val
    integer                      :: place

    ! find where string is or should be
    call locate_key(key,place)
    ! if string was not found insert it
    if(place.lt.1)then
       call insert(keywords,key,abs(place))
       call insert(values,val,abs(place))
    else ! replace
       call replace(values,val,place)
    endif

    end subroutine update

end program demo_replace

Expected output

d==>value of d c==>value of c again b==>value of b a==>value of a again

## AUTHOR

1989,2017 John S. Urban

## LICENSE

Public Domain

## ◆ replace_i()

subroutine m_cli2::replace::replace_i | ( | integer, dimension(:), allocatable | list,
|  | integer, intent(in) | value,
|  | integer, intent(in) | place
| ) |  |
| private

## ◆ replace_l()

subroutine m_cli2::replace::replace_l | ( | logical, dimension(:), allocatable | list,
|  | logical, intent(in) | value,
|  | integer, intent(in) | place
| ) |  |
| private

The documentation for this interface was generated from the following file:

- /home/urbanjs/venus/V600/github/ARGS/M_CLI2/src/M_CLI2.f90