Message Boards Message Boards

0
|
7099 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

Get correct value

Posted 9 years ago

Hello,

I am trying to handle Associations.

 c = <|w -> 1, t -> <|r -> 2, m -> 3|>|>

I am using a function in order to print out the values

calculate2[a_Association] := (Print[a];)
calculate2[else_] := else

this commando:

calculate2 //@ c // InputForm;

results in:

   <|r->2,m->3|>
   <|w->1,t->Null|>

I am confused about the 'Null', what I need is

 <|w->1,t-> <|r->2,m->3|>|> 

I think the problem is that the value for t is also an association(?). Is there a workaround? The list c is just a small example, I have to work with larger lists (more than 3 hierarchy-levels). Later on I have to work with the input a (not only Print statements).

Thanks! Gordon

POSTED BY: gordon-fischer

You see the result of Print, which is Null.

In[37]:= Print["Mary had a little lamb."]
During evaluation of In[37]:= Mary had a little lamb.

In[39]:= %37 === Null
Out[39]= True

To see something, one has to give it after printing

In[30]:= Clear[calculate3]
calculate3[a_] := (Print[a]; a) /; AssociationQ[a]
calculate3[a_] := a /; ! AssociationQ[a]

In[33]:= calculate3 //@ c
During evaluation of In[33]:= <|r->2,m->3|>
During evaluation of In[33]:= <|w->1,t-><|r->2,m->3|>|>
Out[33]= <|w -> 1, t -> <|r -> 2, m -> 3|>|>

Now you see that your function calculate is highly questionable. One gets the keys of an association with Keys and the values with Values, usually. Printing as well as InputForm are very bad data handling ideas, because they are wrappers: with other words, you can not continue working on their results.

POSTED BY: Udo Krause
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract