# ReplaceRepeated problem

I am driving myself nuts.

I have a list of data consisting of team number, team names and results from a tournamet (the results are called imps, vp, and cum).

{teamNumber1, teamName1, teamNumber2, teamName2, imps, vp, cum}

Here is one example

thu2BracketedResults1 = {{21, "McDougal", 22, "Zwald", -5, 7, 7}, {22,
"Zwald", 23, "Moutz", 3, 12, 12}, {23, "Moutz", 21, "McDougal",
18, 17, 17}, {24, "Winston", 25, "Harrison", 12, 15, 15}, {25,
"Harrison", 26, "Bellan", 19, 17, 17}, {26, "Bellan", 24,
"Winston", -10, 6, 6}, {27, "Dailey", 28, "Bell", -2, 9, 9}, {28,
"Bell", 29, "McReynolds", 8, 14, 14}, {29, "McReynolds", 27,
"Dailey", 3, 12, 12}};


It looks like this in a grid

Grid[thu2BracketedResults1,
Alignment -> Left,
Dividers -> All]


Without explaining why, I need to swap the team numbers around. I applied ReplaceRepeated with this rule

thu2BracketedResultsBS1 = thu2BracketedResults1 //. {
{before___,26,"Bellan",    after__} -> {before,21,"Bellan",    after},
{before___,29,"McReynolds",after__} -> {before,22,"McReynolds",after},
{before___,27,"Dailey",    after__} -> {before,23,"Dailey",    after},
{before___,22,"Zwald",     after__} -> {before,24,"Zwald",     after},
{before___,25,"Harrison",  after__} -> {before,25,"Harrison",  after},
{before___,24,"Winston",   after__} -> {before,26,"Winston",   after},
{before___,21,"McDougal",  after__} -> {before,27,"McDougal",  after},
{before___,23,"Moutz",     after__} -> {before,28,"Moutz",     after},
{before___,28,"Bell",      after__} -> {before,29,"Bell",      after}
};


However the results were incorrect and frankly quite bizarre.

Observe that it works in every instance except for the first occurrence of "Winston" in row 4. Strangely the second occurrence of "Winston" works fine in row 6 and all other team numbers are swapped properly.

Grid[thu2BracketedResultsBS1,
Alignment -> Left,
Dividers -> All]


I have used this type of rule in many cases and it always works. I can't for the life of me figure out what is going on.

When I extract row 4 from the list and study it, everything appears normal except when I apply the complete rule (see below).

thu2BracketedResults1[[4, 2]]
(* "Winston" *)

StringMatchQ[thu2BracketedResults1[[4, 2]], "Winston"]
(* True *)

thu2BracketedResults1[[4]]
(* {24, "Winston", 25, "Harrison", 12, 15, 15} *)

thu2BracketedResults1[[4]] /. {before___, 24, "Winston",
after__} -> {before, 26, "Winston", after}
(* {26, "Winston", 25, "Harrison", 12, 15, 15} *)

thu2BracketedResults1[[4]] //. {
{before___,26,"Bellan",    after__} -> {before,21,"Bellan",    after},
{before___,29,"McReynolds",after__} -> {before,22,"McReynolds",after},
{before___,27,"Dailey",    after__} -> {before,23,"Dailey",    after},
{before___,22,"Zwald",     after__} -> {before,24,"Zwald",     after},
{before___,25,"Harrison",  after__} -> {before,25,"Harrison",  after},
{before___,24,"Winston",   after__} -> {before,26,"Winston",   after},
{before___,21,"McDougal",  after__} -> {before,27,"McDougal",  after},
{before___,23,"Moutz",     after__} -> {before,28,"Moutz",     after},
{before___,28,"Bell",      after__} -> {before,29,"Bell",      after}
}
(* {24, "Winston", 25, "Harrison", 12, 15, 15} *)


Help!!! Can anyone return me to a sane state?

Sign me temporary insane

• It seems to work fine as is, but I would use RuleDelayed instead of Rule here. If before or after had been defined beforehand, then your replacements would not work as expected. Commented Sep 27, 2019 at 17:34

An alternative approach:

ClearAll[renumber]
renumber = # /. Thread[{26, 29, 27, 22, 25, 24, 21, 23, 28} ->  Range[21, 29]] &;


You can use renumber with MapAt to change the values in columns 1 and 3:

thu2BracketedResultsBS1 = MapAt[renumber, thu2BracketedResults1, {All, {1, 3}}];

Grid[thu2BracketedResultsBS1, Alignment -> Left, Dividers -> All]


Alternatively, you can use it to define new values for the first and third columns using Part assignment:

thu2BracketedResultsBS1 = thu2BracketedResults1;
thu2BracketedResultsBS1[[All, {1, 3}]] = renumber@thu2BracketedResultsBS1[[All, {1, 3}]];
Grid[thu2BracketedResultsBS1, Alignment -> Left, Dividers -> All]


same picture

• Although I had run across MapAt in the documentation I don't ever recall using it. Your solution is a wonderful example of its application. Essentially I only need to enter the list of team numbers to be swapped. Thank you! Commented Sep 28, 2019 at 16:07

Notice that once a part of a expression matches a rule in a list of rules, all the rest rules in the list will be omitted. In your example, {24, "Winston", 25, "Harrison", 12, 15, 15} already matches the pattern {before___, 25, "Harrison", after__}, so the rule {before___, 24, "Winston", after__} -> {before, 26, "Winston", after} will be omitted.

• I think you nailed it. Sanity restored :) I need to apply the rules individually rather than in a list. thu2BracketedResults1 //. {before___, 26, "Bellan", after__} :> {before, 21, "Bellan", after} //. {before___, 29, "McReynolds", after__} :> {before, 22, "McReynolds", after} //. {before___, 27, "Dailey", after__} :> {before, 23, "Dailey", after} //. ... {before___, 28, "Bell", after__} :> {before, 29, "Bell", after}; Kindly add this to your answer. Commented Sep 27, 2019 at 17:51