5
$\begingroup$

I have association and want to replace integer in specific element in this association. I'm trying to build pattern to be used by ReplaceAll command. When number is explicitly provided in pattern match occurs, but when I put number in a variable and provide number from variable match fails.

In[1]:= MatchQ[<|id -> 6, dur -> 10, skill -> 1, succ -> {7}, 
  dl -> 250|>, <|la___, succ -> {l___, 7, r___}, ra___|>]

Out[1]= True

In[2]:= last = 7
last[[0]]

Out[2]= 7

Out[3]= Integer

In[4]:= MatchQ[<|id -> 6, dur -> 10, skill -> 1, succ -> {7}, 
  dl -> 250|>, <|la___, succ -> {l___, last, r___}, ra___|>]

Out[4]= False

As shown in this example first match occurs, but second fails with number in variable.

$\endgroup$
  • 1
    $\begingroup$ It is probably because, Association as the attribute HoldAllComplete. You work around this with pat = With[{last = last}, <|la___, succ -> {l___, last, r___}, ra___|>]; MatchQ[<|id -> 6, dur -> 10, skill -> 1, succ -> {7}, dl -> 250|>, pat]. $\endgroup$ – Henrik Schumacher Jun 20 at 11:46
  • $\begingroup$ @HenrikSchumacher yeap, it works! I even managed to perform ReplaceAll operation with this pattern $\endgroup$ – Алексей Песчаный Jun 20 at 11:53
  • $\begingroup$ I can accept this as answer if nobody will provide "cleaner" solution $\endgroup$ – Алексей Песчаный Jun 20 at 11:55
8
$\begingroup$

You may use KeyValuePattern.

With

assoc = <|id -> 6, dur -> 10, skill -> 1, succ -> {7}, dl -> 250|>;
last = 7;

then the following return True.

MatchQ[assoc, KeyValuePattern[succ -> {___, last, ___}]]

MatchQ[assoc, KeyValuePattern[(succ -> v_List) /; MemberQ[v, last]]]
True

Hope this helps.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I tried this, but there is problem when I try to perform ReplaceAll operation. I need to make something like this: list /. <|la___, succ -> {l___, last, r___}, ra___|> :> <|la, succ -> {l, r}, ra|>} $\endgroup$ – Алексей Песчаный Jun 21 at 14:17
  • $\begingroup$ Is possible to do replace with KeyValuePattern? $\endgroup$ – Алексей Песчаный Jun 21 at 14:20
  • $\begingroup$ @АлексейПесчаный I would go with Query. Something like Query[{Key[succ] -> DeleteCases[last]}]@assoc. Note that you should ask for what you need help with instead of what you think you need to solve what you need help with; This is an XY Problem question. $\endgroup$ – Edmund Jun 21 at 15:05
7
$\begingroup$

Turning my comment into an answer.

It is probably because Association has the attribute HoldAllComplete: You can see that from the fact that the last in the pattern expression stays unevaluated.

You can work around this with

last= 7;
pat = With[{last = last}, <|la___, succ -> {l___, last, r___}, ra___|>];     
MatchQ[<|id -> 6, dur -> 10, skill -> 1, succ -> {7}, dl -> 250|>, pat]
| improve this answer | |
$\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.