4
$\begingroup$

I wish to apply some replacements in my association. For example, suppose I have

<|a->b,c-><|1->2,b->d|>|>

then, how to replace all instances of a with A, b with B, c with C and d with D? My desired output is

<|A->B,C-><|1->2,B->D|>|>

Note that I do not necessarily wish to capitalise. This is just an example. A /. only replaces the values. I wish to replace all elements (keys and values) to the extent of nests as well.

$\endgroup$
7
  • $\begingroup$ Hi zplus11! Some users noted a similarity between your profile image and a flag. It looks like a coincidence, but would you be willing to choose a different image? $\endgroup$ Commented Feb 12 at 12:29
  • $\begingroup$ Hi there C. E.. I understand. However, I would like to clarify that I mean no resemblance by my profile picture to the flag you reference. My profile picture is the red striped ball in the game of Billiards. With this clarification, may I keep this profile picture? If needed, I will happily describe the profile picture in my user bio, on profile. Please advice. $\endgroup$ Commented Feb 13 at 4:22
  • $\begingroup$ Ah! So that is what it is. I will explain this to the others. I don't oppose that you keep it as long as it doesn't become a regular thing that people react to it, which doesn't seem too likely. $\endgroup$ Commented Feb 13 at 8:20
  • $\begingroup$ After more discussion, it felt as though the fraktur font on the billiard ball, which is not the customary font, was too much. I have now reset the profile picture. You are welcome to pick a new profile picture of your own, but please try to avoid ones that might raise suspicions, even if unfounded. $\endgroup$ Commented Feb 13 at 15:43
  • $\begingroup$ @C.E.—while I am not satisfied with this decision, I understand. I will again emphasis, even though you personally seem to agree, I did not intend to resemble that flag. Anyhow, I will change my profile picture to the black & white version of it; there will be no red. I hope that this will be acceptable. Best regards... Edit Funnily, I was especially happy with my profile picture in this forum of all, since the red matched with the forum's theme colour as well. Alas! $\endgroup$ Commented Feb 13 at 16:40

2 Answers 2

4
$\begingroup$
asc = <|a -> b, c -> <|1 -> 2, b -> d|>|>

asc /. Association -> List /. {a -> A, b -> B, c -> C, d -> D} /. 
 List -> Association

<|A -> B, C -> <|1 -> 2, B -> D|>|>

$\endgroup$
1
  • $\begingroup$ Thankyou for the solution, Syed. This also motivates me to see the FullForm of an association 🧐. I'll use this method, thanks a lot. $\endgroup$ Commented Feb 10 at 15:50
3
$\begingroup$

As associations are difficult to handle directly, it is easier to first transform it into a string, do the necessary manipulations, and transform it back into an association:

t = <|a -> b, c -> <|1 -> 2, b -> d|>|> ;
t // ToString // ToUpperCase // ToExpression

<|A -> B, C -> <|1 -> 2, B -> D|>|>
$\endgroup$
1
  • $\begingroup$ Daniel, thankyou for the solution. I see that you have solved the problem of capitalising as I asked. I will keep this method in mind, and use it wherever I can. For the general replacements, I think the solution by Syed might be better suited. Thanks for your help... $\endgroup$ Commented Feb 10 at 15:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.