|
M_CLI2
M_CLI2 module (Fortran)
|
Private Member Functions | |
| subroutine | replace_c (list, value, place) |
| subroutine | replace_i (list, value, place) |
| subroutine | replace_l (list, value, place) |
|
private |
replace(3f) - [M_CLI2] replace entry in a string array at specified position (LICENSE:PD)
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
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.
VALUE the value to place in the array LIST is the array. PLACE is the subscript that the entry should be placed at
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
1989,2017 John S. Urban
Public Domain
|
private |
|
private |
1.8.17