Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with AssociativeArray()'s within a struct

    Hi everybody,

    I'm writing a package and during the development I needed to create a structure (say S) containing some associative arrays (say, A1 and A2). Then, I wanted to nest different instances of this structure into another associative array (say B) to keep track of the results after each iteration. However, at then end of the calculation I wasn't able to access the structures within B anymore. Let me post below a simplified version of this issue:

    Code:
    mata:
      struct try_struct
      {
        class AssociativeArray scalar a, b
      }
    end
    mata:
      aa = AssociativeArray()
      bb = AssociativeArray()
      aa.put("a", 4)
      bb.put("b", -4)
    
      S1 = try_struct()
      (S1.a).put("a", aa.get("a"))
      (S1.b).put("b", bb.get("b"))
      
      S2 = try_struct()
      (S2.a).put("a", aa.get("a"))
      (S2.b).put("b", bb.get("b"))
      
      A = AssociativeArray()
      A.reinit("real", 1)
      A.put(1, S1)
      A.put(2, S2)
      
      liststruct(A.get(1))     // this produces the expected result
      structname(A.get(2))     // this doesn't return anything (it should return 'try_struct')
      ((A.get(1)).a).get("a")  // this raises an error!
    end
    Maybe this is a silly question, but I'm not able to figure out why it doesn't work.

    Please, let me know if you have any idea of how to solve this issue. I'm using Stata 17 updated with the latest available patch.

    Thanks!
    Last edited by SergioV; 09 Aug 2023, 04:47.
    Sergio Venturini
Working...
X