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$

2 Answers 2

5
$\begingroup$
DeleteCases[Association@KeyValueMap[#1->DeleteCases[#2,_?(StringMatchQ[ToString@#,Whitespace..]&)]&,acs],_?(#==<||>&)]
$\endgroup$
2
  • $\begingroup$ Hello Alexey, good to see you here! +1. $\endgroup$ Jul 8, 2015 at 19:02
  • $\begingroup$ @LeonidShifrin Thank you! $\endgroup$ Jul 9, 2015 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 and acknowledge you have read our privacy policy.

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