I want to transform some nested lists into nested associations except the "rows"->List pattern.
Here is the example
s={"sensor" -> {"opto" -> {"filename" ->
"137fb39d9a39f0e18ec49f4a68d72554_opto.csv",
"data" -> {"time" -> {"unit" -> "s", "rows" -> {1}},
"4" -> {"unit" -> "intensity", "rows" -> {2, 3}},
"1" -> {"unit" -> "intensity", "rows" -> {4, 5}}}},
"qmb1" -> {"filename" ->
"137fb39d9a39f0e18ec49f4a68d72554_qmb1.csv",
"data" -> {"qmb1" -> {"unit" -> "intensity", "rows" -> {2}},
"time" -> {"unit" -> "s", "rows" -> {1}}}},
"saw" -> {"filename" -> "137fb39d9a39f0e18ec49f4a68d72554_saw.csv",
"data" -> {"time" -> {"unit" -> "s", "rows" -> {1}},
"15" -> {"unit" -> "Hz", "rows" -> {5, 6}},
"10" -> {"unit" -> "Hz", "rows" -> {2, 3, 4}},
"9" -> {"unit" -> "Hz", "rows" -> {7, 8, 9}}}},
"qmb2" -> {"filename" ->
"137fb39d9a39f0e18ec49f4a68d72554_qmb2.csv",
"data" -> {"time" -> {"unit" -> "s", "rows" -> {1}},
"qmb2" -> {"unit" -> "intensity", "rows" -> {2}},
"flow" -> {"unit" -> "sscm", "rows" -> {4}},
"temperature" -> {"unit" -> "\[Degree]C", "rows" -> {3}},
"humidity" -> {"unit" -> "%", "rows" -> {5}}}}},
"date" -> {"year" -> 2016, "month" -> 11, "sec" -> 10, "day" -> 17,
"hour" -> 14, "min" -> 18}, "serie" -> 1,
"compound" -> "multi-detection", "inLab" -> 0, "proto" -> 1,
"uuid" -> "137fb39d9a39f0e18ec49f4a68d72554"}
So far I have done:
ReplaceAll[s, List :> Association]
which gives
<|"sensor" -> <|"opto" -> <|"filename" ->
"137fb39d9a39f0e18ec49f4a68d72554_opto.csv",
"data" -> <|"time" -> <|"unit" -> "s",
"rows" -> Association[1]|>,
"4" -> <|"unit" -> "intensity", "rows" -> Association[2, 3]|>,
"1" -> <|"unit" -> "intensity",
"rows" -> Association[4, 5]|>|>|>,
"qmb1" -> <|"filename" ->
"137fb39d9a39f0e18ec49f4a68d72554_qmb1.csv",
"data" -> <|"qmb1" -> <|"unit" -> "intensity",
"rows" -> Association[2]|>,
"time" -> <|"unit" -> "s", "rows" -> Association[1]|>|>|>,
"saw" -> <|"filename" ->
"137fb39d9a39f0e18ec49f4a68d72554_saw.csv",
"data" -> <|"time" -> <|"unit" -> "s",
"rows" -> Association[1]|>,
"15" -> <|"unit" -> "Hz", "rows" -> Association[5, 6]|>,
"10" -> <|"unit" -> "Hz", "rows" -> Association[2, 3, 4]|>,
"9" -> <|"unit" -> "Hz", "rows" -> Association[7, 8, 9]|>|>|>,
"qmb2" -> <|"filename" ->
"137fb39d9a39f0e18ec49f4a68d72554_qmb2.csv",
"data" -> <|"time" -> <|"unit" -> "s",
"rows" -> Association[1]|>,
"qmb2" -> <|"unit" -> "intensity", "rows" -> Association[2]|>,
"flow" -> <|"unit" -> "sscm", "rows" -> Association[4]|>,
"temperature" -> <|"unit" -> "\[Degree]C",
"rows" -> Association[3]|>,
"humidity" -> <|"unit" -> "%",
"rows" -> Association[5]|>|>|>|>,
"date" -> <|"year" -> 2016, "month" -> 11, "sec" -> 10, "day" -> 17,
"hour" -> 14, "min" -> 18|>, "serie" -> 1,
"compound" -> "multi-detection", "inLab" -> 0, "proto" -> 1,
"uuid" -> "137fb39d9a39f0e18ec49f4a68d72554"|>
However I am stuck here, because ReplaceAll does not seem to work for Association as it works for List. By example:
ReplaceAll[Normal@<|"unit" -> "intensity","rows" -> Association[4, 5]|>,
("rows" -> a_Association) :> ("rows" -> Apply[List, a])]
gives (as expected)
{"unit" -> "intensity", "rows" -> {4, 5}}
but
ReplaceAll[<|"unit" -> "intensity","rows" -> Association[4, 5]|>,
("rows" -> a_Association) :> ("rows" -> Apply[List, a])]
has no effect
<|"unit" -> "intensity", "rows" -> Association[4, 5]|>
Questions
- why
ReplaceAlldoes not work forAssociation? - how to turns
"rows"->Association[4,5,...]into"rows"->{4,5,...}in my association ("rows"->...can be at any place)
Note: I am using MMA v10
ToAssociationsfrom the"GeneralUtilities`"package, for which you can find examples in the answers to other questions. Associations were new with 10.0 and replacing within associations was not working from the beginning, so it would be necessary to know which exact version you are using if you are seeking for alternatives toToAssociations... – Albert Retey May 11 '19 at 11:15