The set up:
First a helper function:
Clear[values]
values[m_, l_, v_] := <|"Math" -> m, "Log" -> l, "Value" -> v|>
I have a nested Association of the form:
RV = <|
"Top01" -> <|
"Name" -> "Top01Name",
"par01" -> values[a1,
\!\(\*SubscriptBox[\(a\), \("\<1\>"\)]\), 1],
"par02" -> values[b1,
\!\(\*SubscriptBox[\(b\), \("\<1\>"\)]\), 2],
"par03" -> values[c1,
\!\(\*SubscriptBox[\(c\), \("\<1\>"\)]\), 3]
|>,
"Top02" -> <|
"Name" -> "Top02Name",
"par01" -> values[aa1,
\!\(\*SubscriptBox[\(a\), \("\<1\>"\)]\), 1],
"par02" -> values[bb1,
\!\(\*SubscriptBox[\(b\), \("\<1\>"\)]\), 2],
"par03" -> values[cc1,
\!\(\*SubscriptBox[\(c\), \("\<1\>"\)]\), 3]
|>,
"Top3" -> <|
"SubTop01" -> <|
"Name" -> "SubTop01Name",
"par01" -> values[as1,
\!\(\*SubscriptBox[\(a\), \("\<1\>"\)]\), 1],
"par02" -> values[bs1,
\!\(\*SubscriptBox[\(b\), \("\<1\>"\)]\), 2],
"par03" -> values[cs1,
\!\(\*SubscriptBox[\(c\), \("\<1\>"\)]\), 3]
|>,
"SubTop02" -> <|
"Name" -> "SubTop02Name",
"par01" -> values[as2,
\!\(\*SubscriptBox[\(a\), \("\<1\>"\)]\), 1],
"par02" -> values[bs2,
\!\(\*SubscriptBox[\(b\), \("\<1\>"\)]\), 2],
"par03" -> values[cs2,
\!\(\*SubscriptBox[\(c\), \("\<1\>"\)]\), 3]
|>
|>
|>;
Because of the constraints on my workflow I'm guaranteed to have a unique single expression for all of the <|"Math"->#,...|>
instances. (Where # is not repeated for all instances of the key "Math" in the full Association.)
One of the things that I have done successfully is make a new Association with all the "Math" symbols as the keys for the "Log" and "Value" expressions.
For example:
LogView = Cases[RV,
KeyValuePattern[{"Math" -> a_, "Log" -> b_}] :>
Rule[a, b], \[Infinity]]
which gives:
(* {
a1 -> \!\(\*SubscriptBox[\(a\), \("1"\)]\),
b1 -> \!\(\*SubscriptBox[\(b\), \("1"\)]\),
c1 -> \!\(\*SubscriptBox[\(c\), \("1"\)]\),
aa1 -> \!\(\*SubscriptBox[\(a\), \("1"\)]\),
}*)
and :
NumberView = Cases[RV,
KeyValuePattern[{"Math" -> a_, "Value" -> b_}] :>
Rule[a, b], \[Infinity]]
which gives:
(*{a1 -> 1, b1 -> 2, c1 -> 3, aa1 -> 1, bb1 -> 2, cc1 -> 3, as1 -> 1,
bs1 -> 2, cs1 -> 3, as2 -> 1, bs2 -> 2, cs2 -> 3}*)
However the last thing I need to get is a List of the Keys to a "Math" symbol and the value of the "Name" key. I have tried:
Cases[RV, KeyValuePattern[a_ -> KeyValuePattern[
{"Name" -> b_, c_ -> KeyValuePattern["Math" -> d_]}]] :>
{d, a, b, c}, {0, \[Infinity]}]
which gives:
(*
{
{as1, "SubTop01", "SubTop01Name", "par01"},
{a1, "Top01", "Top01Name", "par01"}
}
*)
I only need the "par??" key and the parent key to the "par??" key. This key will sometimes be "Top??" or "SubTop??". Along with the value for the "Name" key which will be at the same level as the "par??" key.
The question: Where are all of the other matches?
The Backstory: I'm doing simple math with lots of parameters for many cases which requires careful bookkeeping. I also need to keep a $\LaTeX$ style log file and export my data structure in to a spreadsheet such that it can be turned into an HDF5 file. (Mathematica's Export to HDF5 is not complete so we have made a work around.)
I can now do things like this:
Get an equation for the log file
eq = a1^2 + bs2 - (aa1 + Sqrt[cc1]);
TeXForm[eq /. LogView]
Output:
(*
a_1^2-a_1+b_1-\sqrt{c_1}
*)
I can also do an evaluation:
eq /. NumberView
Output:
(*
2 - Sqrt[3]
*)