8
$\begingroup$

Considering we have an Association:

<|
  a -> tr, 
  b -> <|
     c -> <|
       d -> 12, 
       e -> del
       |>
     |>,
  f -> g|>

My issue is that I want to make a table based on the values of the nested association. For example, the output must like:

tr, 12, del, g

Who has a suggestion?

$\endgroup$
  • $\begingroup$ The question is unclear, and your examples of data don't improve that. Make a small self-contained example, with sample (small!) input and desired output, if you want a better chance to get good answers. $\endgroup$ – Leonid Shifrin Jun 17 '15 at 15:43
  • $\begingroup$ Hi Leonid, I edited my question $\endgroup$ – Michiel van Mens Jun 18 '15 at 6:49
  • $\begingroup$ Kuba, you are right, I changed it $\endgroup$ – Michiel van Mens Jun 18 '15 at 6:53
5
$\begingroup$
ass = <|a -> tr, b -> <|c -> <|d -> 12, e -> del|>|>, f -> g|>

Those are mirrored answers from How to switch from List of rules to Association?

ass //. a_Association :> Values[a] // Flatten

or

Flatten @ Replace[ass, a_Association :> Values[a], {0, Infinity}]

{tr, 12, del, g}

$\endgroup$
  • 5
    $\begingroup$ Resulting in a flat ass? +1 $\endgroup$ – ciao Jun 18 '15 at 6:54
  • 1
    $\begingroup$ @ciao good point :P $\endgroup$ – Kuba Jun 18 '15 at 6:59
14
$\begingroup$

Given

asso = <|a -> tr, b -> <|c -> <|d -> 12, e -> del|>|>, f -> g|>

then directly

Level[asso, {-1}]

{tr, 12, del, g}

See this post where you can find clear illustrations of "how work levels".

$\endgroup$
4
$\begingroup$

I'm just adding this answer because of your other question which I think is strongly related to this one and only when seeing both questions together it becomes clear you are not using the JSON data of the other question correctly. It probably is worth noting that Associations do AFAIK respect the order of fields, so answering this question on its own seems reasonable, especially when making the assumption that you control how the data is generated. I still wouldn't suggest to ignore the actual keys even in that case because keys usually are there for a reason and using them creates code which is much cleaner and more robust, but that is a different subject.

But as the original data comes from an imported JSON string which is generated from code not under your control I think it is plain wrong (or at least asking for trouble) to extract the data without actually looking at the keys. Here is what I would do for your example:

data = <|a -> tr1, b -> <|c -> <|d -> 12, e -> del|>|>, f -> g|>
data @@@ {{a}, {b, c, d}, {b, c, e}, {f}}

or, if generalized to a list of such associations (which I think is what you'll ultimately have to do for the data in your other question):

data = {
 <|a -> tr1, b -> <|c -> <|d -> 12, e -> del|>|>, f -> g|>,
 <|a -> tr2, b -> <|c -> <|e -> del|>|>, f -> h|>,
 <|a -> tr3, b -> <|c -> <|e -> del, ee-> 5, d -> 14|>|>, f -> i|>
};
(# @@@ {{a}, {b, c, d}, {b, c, e}, {f}}) & /@ data

this is of course somewhat more work than the other solutions, but at least will give a well defined answer. As this second example shows that approach will not fail when there are more keys in the data or these come in another order, and even returns something useful when a key is missing. All that can (and will) happen when you import data in JSON format from a foreign source, as you have learned. Extracting something by position just because in the example you have currently available that part of the data was at that position is highly dangerous and will most probably break in short time...

$\endgroup$
1
$\begingroup$

You could also traverse the nested association recursivly until the nested structure ends

getValues[assoc_Association] := Flatten[Map[getValues]@Values[assoc]]
getValues[value_] := value 

check that it works:

<|a -> tr, b -> <|c -> <|d -> 12, e -> del|>|>, f -> g|> //getValues

{tr, 12, del, g}

$\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.