Here is a simplified version of my problem.
I have a nested Association.
worksheet = <|"startDate" ->
DateObject[{2017, 8, 10}, "Day", "Gregorian", -5.`],
"eventList" -> {
<|"eventName" -> "Thursday Stratified 49r Pairs",
"eventTotal" -> 336.`|>,
<|"eventName" -> "Thursday Aftt Side Pairs",
"eventTotal" -> 240.`|>}|>
Below are the Keys
Keys[worksheet]
(* {"startDate", "eventList"} *)
I have a function to fix the names in the eventList
when they are bad. Simplified function is:
fixName[name_] := Module[
{
newName = name
},
newName = StringReplace[newName, "Aftt" -> "Aft"]
]
Below is the input name and the corrected name for the second entry in eventList
worksheet["eventList"][[2]]["eventName"]
(* "Thursday Aftt Side Pairs" *)
fixName[worksheet["eventList"][[2]]["eventName"]]
(* "Thursday Aft Side Pairs" *)
I want to Scan
the names in eventList
and replace the corrected name with the input name.
I want to replace worksheet
with the edited version.
Here is what I tried:
worksheet = Function[wsIn,
Module[
{
len = Length[wsIn["eventList"]],
wsOut = wsIn
},
Scan[
(wsOut["eventList"][[#]]["eventName"] =
fixName[wsOut["eventList"][[#]]["eventName"]]) &,
Range[len]
];
wsOut
]
][worksheet]
However, this produces an error
Association::setps: <|eventName->Thursday Stratified 49r Pairs,
eventTotal->336.|> in the part assignment
is not a symbol.
Need help with this code.
Query["eventList", All, "eventName", fixName]@worksheet
$\endgroup$ – Lukas Lang Aug 7 '19 at 22:04ReplacePart
? You can also usewsOut[["eventList", #, "eventName"]] = ...
but that'll be less efficient $\endgroup$ – b3m2a1 Aug 7 '19 at 22:04worksheet
isn't held on input. $\endgroup$ – b3m2a1 Aug 7 '19 at 22:04fiXName[key_,repl_]:= Replace[#,<|key -> v_, y___|> :><|key -> StringReplace[v, repl],y|> ,All]&; fiXName["eventName", "Aftt" -> "Aft"]@worksheet
? $\endgroup$ – kglr Aug 8 '19 at 1:30