[UP]


Manual Reference Pages  - replace (3)

NAME

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

CONTENTS

Synopsis
Description
Options
Examples
Author
License

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

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 (3) March 11, 2021
Generated by manServer 1.08 from a8ec5e27-fda1-4075-b672-8f18680d1f4a using man macros.