Git Product home page Git Product logo

Comments (11)

zerothi avatar zerothi commented on May 26, 2024 1

@14NGiestas I am quite busy this week, if you can provide a complete test example I can more easily test it :)

from fdict.

zerothi avatar zerothi commented on May 26, 2024 1

Thanks! Your zip file says it is incomplete?

Archive:  ~/fdict-test.zip
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.

from fdict.

zerothi avatar zerothi commented on May 26, 2024 1

Ok, the problem is quite trivial. When you are using .first. you are not retrieving the value of the first element in the dictionary. You are basically creating a new pointer to the dictionary starting from the first key. Then you can loop on this using:

type(Dict) :: main_dict
type(Dict) :: loop_dict

loop_dict = .first. main_dict
do while ( .not. .empty. loop_dict ) 
   < do something with this key-val segment in the dictionary > 
   loop_dict = .next. loop_dict
end do

So your code should look like this:

module my_module
  use dictionary
  type MyType
    type(Dict) :: main_dict
  end type MyType
contains
  function MyType_(array) result(self)
    implicit none
    type(MyType) :: self
    type(Dict) :: sub_dict
    character(*) :: array(:)
    integer :: i
    real(8) :: v
    do i = 1 , size(array)
      sub_dict = ('val1' .kv. 100.d0) // ('val2' .kv. 'A string')
      self%main_dict = self%main_dict // (array(i) .kvp. sub_dict)
      call nullify(sub_dict)
    end do
  end function MyType_
end module my_module

program main
  use my_module
  type(MyType) :: t
  type(Dict) :: sub_dict, loop_dict
  real(8) :: val
  character(20) :: buffer
  t = MyType_(['hello', 'world', 'then!'])
  loop_dict = .first. (t%main_dict)
  call associate(sub_dict, loop_dict)
  call assign(buffer, sub_dict, 'val2')
  call assign(val, sub_dict, 'val1')
  print*, "Should print 100.0d0:", val
  print*, "Should print 'A string':", buffer ! Should print "A string" ?
end program main

from fdict.

zerothi avatar zerothi commented on May 26, 2024

I am not really sure what you are trying to do.
Do you populate the main dict in multiple subroutines?
If yes, then the above code should concatenate the main dict

main_dict = main_dict // ('sub-dict1' .kvp. sub_dict)

In case you are dealing with allocatable arrays or static arrays in subroutines and using kvp, then it won't work. I am not at my computer so I can't create a small example, but if you clarify a bit more I'll be able to help you.

from fdict.

14NGiestas avatar 14NGiestas commented on May 26, 2024

Sorry I'll try to be more clear,
I have a derived type which holds the main dict then I try to populate using a array of strings as key in that manner, in fact this works however the sub-dict do not keep the default values I provided.

subroutine (...)
do i=(...)
new_dict = (
    array(i) .kvp. (
        ('value1' .kv. 0.d0) //
        ('value2' .kv. 'string')
    )
)
self % main_dict = self % main_dict // (new_dict)
end do
(...)
end subroutine

I also tried to do the following:

new_dict = ('value1' .kv. 0.d0)//('value2' .kv. 'string')
self % main_dict = self % main_dict // (array(i) .kvp. new_dict)

In main program I try to retrieve the defaults:

type(dict) :: d
type(myderived) :: my
real :: r
character(255) :: buffer
! here I can retrieve all keys (populated by array(i)) 
! but can't retrieve the values of the sub dict
d = .first. my % main_dict
call assign(r, d, 'value1')  ! non sense
call assign(buffer, d, 'value2')  ! blank
print*, .key. d ! ok returns the right key name

from fdict.

14NGiestas avatar 14NGiestas commented on May 26, 2024

Here it is a complete example...
fdict-test.zip

from fdict.

14NGiestas avatar 14NGiestas commented on May 26, 2024

Oops... I edited the previous comment adding the correct file

from fdict.

zerothi avatar zerothi commented on May 26, 2024

hmm, same error appears. Could you try and create a tar?

from fdict.

14NGiestas avatar 14NGiestas commented on May 26, 2024

Using a online tool this time O_O
EDIT: Fixed the example...
fdict-test-3.zip

from fdict.

zerothi avatar zerothi commented on May 26, 2024

worked! ;) I'll have a look!

from fdict.

14NGiestas avatar 14NGiestas commented on May 26, 2024

Thank you, Zerothi, it works! 😃

from fdict.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.