How can I, Iterate over Hash table (fhash_tbl_t)

I have been using the @lkedward fhash module in my recent project. However, I’m unsure how to iterate over my hash table using this module. Could anyone advise me on how to do this? Here’s the code I’m working with.

use fhash, only: fhash_tbl_t, key => fhash_key
implicit none

type(fhash_tbl_t) :: tbl
integer :: i, val

! Insert some values into the hash table
call tbl%set(key('name 1'), value='value 1')
call tbl%set(key('name 2'), value='value 2')
call tbl%set(key('name 3'), value='value 3')
call tbl%set(key('name 4'), value='value 4')

I would like to achieve something similar to this.

! Iterate over the hash table
do i = 1, tbl%size()
    call tbl%fhash_iterate(i, key=key, value=val)
    ! Do something with key and value
end do

What I want to do is to use this hash table to store HTTP request headers as key-value pairs in the form of strings.

Hi Rajkumar @rajkumardongre ,

Unfortunately, I didn’t get around to implementing an item iterator yet, see Implement item iterator · Issue #2 · LKedward/fhash (

@lewisfish has made a rough implementation here, however I haven’t had a chance to cleanup and merge his implementation.

Please feel free to pick up this issue and make a contribution to fhash if you have the time - I’d be happy to provide support on it.


Thank you, @lkedward. I will definitely try to find time to look into it.

For other users of fhash and future readers, I’ve now implemented and merged a basic item iterator for the hash table type.


  type(fhash_tbl_t) :: tbl
  type(fhash_iter_t) :: iter
  class(fhash_key_t), allocatable :: ikey
  class(*), allocatable :: idata

  call tbl%set(key('my_key_1'), value=10)
  !  ... set key-value data ...

  !> Iterate over all items in table
  iter = fhash_iter_t(tbl)
  do while(iter%next(ikey,idata))

    write(*,*) 'Key = "'//ikey%to_string()//'"'

    call print_polymorphic(idata)

  end do

! call iter%reset()  ! (Reset if iterator needed again)

See the full example here: Example: Iterating over hash table items – fhash API Reference (

Note: Data values are currently returned as an unlimited polymorphic entity - I may in the future write some generic interfaces for the intrinsics types in the same way that the main hash table type already has for its get method.