replace Interface

public interface replace

Contents


Module Procedures

private subroutine replace_c(list, value, place)

NAME

replace(3f) - [M_list] 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_list, 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(keywords,'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(keywords,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

Results

> 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

Arguments

Type IntentOptional Attributes Name
character(len=:), allocatable :: list(:)
character(len=*), intent(in) :: value
integer, intent(in) :: place

private subroutine replace_d(list, value, place)

Arguments

Type IntentOptional Attributes Name
doubleprecision, allocatable :: list(:)
doubleprecision, intent(in) :: value
integer, intent(in) :: place

private subroutine replace_r(list, value, place)

Arguments

Type IntentOptional Attributes Name
real, allocatable :: list(:)
real, intent(in) :: value
integer, intent(in) :: place

private subroutine replace_i(list, value, place)

Arguments

Type IntentOptional Attributes Name
integer, allocatable :: list(:)
integer, intent(in) :: value
integer, intent(in) :: place

private subroutine replace_l(list, value, place)

Arguments

Type IntentOptional Attributes Name
logical, allocatable :: list(:)
logical, intent(in) :: value
integer, intent(in) :: place