7
$\begingroup$

Say I have an Association of the form:

assoc = <|first->"abc", second[param]->"xyz"|>

Now to access first, all I have to do is call assoc[first]. Now the problem is that I need an easy way to access second, but without knowing the parameter (which is here param).

Is it possible to use something in the form assoc[second] with some minor modification? Or do I have to write my own function that manipulates Keys[assoc] to find second?

In short, the question is: What's the easiest way to access second and get the information in param?

$\endgroup$

2 Answers 2

4
$\begingroup$
FilterRules[assoc, second[_]][[1, 1, 1]]
(*param*)
$\endgroup$
4
$\begingroup$

You can Select from the Keys of the Association the elements with Head equal to second and then pick out the parameter from second.

Select[Head@# === second &]@Keys[assoc] /. second[p_] -> p
(* {param} *)

Hope this helps.


Update with version that uses only key functions.

KeySelect[Head@# === second &] /* KeyMap[Replace[_[p_] -> p]] /* Keys@assoc
(* {param} *)
$\endgroup$
0

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.