storing array of dictionaries within t_dictionary

Zachary Seldess's icon

Hi all,

Can anyone give me a few tips on saving dictionaries within an array inside a t_dictionary? Below is a code snippet where I embed several dictionaries inside a parent dictionary, naming their keys "0" through "n". What I really want to do is store these in an array within the top level dictionary (m_funcDict), whose name can be whatever.

best,
Zachary

void smap_trajectory_traj_to_dict(t_smap_trajectory *x)
{
    unsigned int i;
    
    dictionary_clear(x->m_funcDict);
    
    for (i = 0; i < x->m_funcSize; i++) {
        t_dictionary *funcDict = dictionary_new();
        char TrajNodeKey[15];
        sprintf(TrajNodeKey, "%d", i);
        
        dictionary_appendfloat(funcDict, gensym("x"), x->m_funcList[i].x);
        dictionary_appendfloat(funcDict, gensym("y"), x->m_funcList[i].y);
        dictionary_appendlong(funcDict, gensym("dur"), x->m_funcList[i].dur);
        dictionary_appenddictionary(x->m_funcDict, gensym(TrajNodeKey), (t_object *)funcDict);
    }
}

Luigi Castelli's icon

Hi there,

if I understand you correctly, this is what you want:

void smap_trajectory_traj_to_dict(t_smap_trajectory *x)
{
    t_atom argv[MAX_FUNC_SIZE];
    long argc = x->m_funcSize;
    long i;

    dictionary_clear(x->m_funcDict);

    for (i = 0; i < argc; i++) {

        t_dictionary *funcDict = dictionary_new();

        dictionary_appendfloat(funcDict, gensym("x"), x->m_funcList[i].x);
        dictionary_appendfloat(funcDict, gensym("y"), x->m_funcList[i].y);
        dictionary_appendlong(funcDict, gensym("dur"), x->m_funcList[i].dur);

        atom_setobj(argv+i, funcDict);
        // do not free funcDict because it is now owned by the atom array
    }
    dictionary_appendatoms(x->m_funcDict, gensym("funclist"), argc, argv);
}

I just coded it off the top of my head so it might need some retouching, but you get the idea.
Let me know if it serves your purpose...

Best

- Luigi

Zachary Seldess's icon

Thanks, Luigi. That was just the example I was looking for.

best,
Zachary