4
$\begingroup$

Considering we have:

acs = <|"A" -> <|"a" -> "    ", "aa" -> "asd"|>, "B" -> <|"bb" -> "  "|>|>

How can we come to have a result as:

res = <|"A" -> <| "aa" -> "asd"|>|>

Thanks!

$\endgroup$
5
$\begingroup$
DeleteCases[Association@KeyValueMap[#1->DeleteCases[#2,_?(StringMatchQ[ToString@#,Whitespace..]&)]&,acs],_?(#==<||>&)]
$\endgroup$
  • $\begingroup$ Hello Alexey, good to see you here! +1. $\endgroup$ – Leonid Shifrin Jul 8 '15 at 19:02
  • $\begingroup$ @LeonidShifrin Thank you! $\endgroup$ – Alexey Golyshev Jul 9 '15 at 3:14
4
$\begingroup$

Here is a solution, based on pattern matching in the list of rules of the association:

acs //. x_Association :> Normal[x]
DeleteCases[ %, (key_->val_String) /; StringMatchQ[val, " "..], Infinity]
DeleteCases[ %, _->{}]
% /. List->Association

(*
{A->{a->    ,aa->asd},B->{bb->  }}
{A->{aa->asd},B->{}}
{A->{aa->asd}}
<|A-><|aa->asd|>|>
*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.