# Replacing every nth comma in string output with “\n” [closed]

Feels like a strange question to ask, but I basically have a string with sets within sets, and it seems like the simplest formatting solution to seeing the data without needing to import everything into table or column form would be to replace every 7th comma in the string with \n to indent a new line. Mind you, this is the 7th itself starting from a count of 1 - not the 8th following 7. So starting from a count of 1, incrementing by 1 per count of a comma, all commas corresponding to a value where 'order' mod 7 = 0 would be replaced in the string by "\n". Is this a weird or nonsensical way of doing it? If I wanted to anyway, how would I implement what command generally?

Input

ps = Prime@Range[5, 16];
divs = x /. Solve[4 + x <= # && 1 + # <= 2 x, Primes] & /@ ps;
sets = MapThread[Thread[{#1, #2}] &, {ps, divs}];
results =
Map[With[{p = #[[1]],
x = #[[2]]}, (((Binomial[Reverse[Range[((p + 1)/2), (p - 3)]],
Range[(p - 3) - ((p + 1)/2) + 1]])))/x] &, sets, {2}];

matches =
ReplaceList[
Flatten[MapThread[Transpose[{##}] &, {sets, results}],
1], {___, {{p1_, d1_}, {pre1___, val1_, ___}}, ___, {{p2_,
d2_}, {pre2___, val2_, ___}}, ___} /;
d1 == d2 && val1 == val2 && Length@{pre1} < Length@{pre2} :>
With[{r1 = Length@{pre1} + 2,
r2 = Length@{pre2} + 2}, {d1, {p1, r1}, {p2, r2},
val1, (r2 - (r1 + ((r2 - r1)/2)))}]]


Output

{{13, {17, 4}, {23, 10}, 220/13, 3},
{17, {23, 6}, {29, 12}, 4368/17, 3},
{19, {23, 6}, {29, 12}, 4368/19, 3},
{23, {29, 6}, {41, 18}, 26334/23, 6},
{29, {41, 12}, {47, 18}, 21474180/29, 3},
{29, {41, 10}, {53, 22}, 493350, 6},
{31, {41, 12}, {47, 18}, 21474180/31, 3},
{31, {41, 10}, {53, 22}, 14307150/31, 6},
{37, {41, 12}, {47, 18}, 21474180/37, 3},
{37, {41, 10}, {53, 22}, 14307150/37, 6},
{29, {47, 14}, {53, 20}, 11978400, 3},
{31, {47, 14}, {53, 20}, 11205600, 3},
{37, {47, 14}, {53, 20}, 347373600/37, 3},
{41, {47, 14}, {53, 20}, 347373600/41, 3},
{43, {47, 14}, {53, 20}, 347373600/43, 3}}

• @TravisArlenMcCracken This sort of information should be edited into the main question instead of comments. Use the edit link below the question and make sure to put any code in code blocks. Formatting help is available by clicking the (?) button during editing. – Szabolcs Jun 15 '15 at 8:21
• @TravisArlenMcCracken: "Having trouble implementing it"? What does that mean? Simply enter Column[matches] and evaluate it - it gives each set on its own line - is that not what you're after? – ciao Jun 15 '15 at 8:39
• @ciao Holy #@!& you're right, I just had to type it in! Here I was all going to the online guide for it and complicating things like an idiot hahah. Went to ( reference.wolfram.com/language/DatabaseLink/tutorial/… ). Thanks so much, this is all I needed. Ugh sorry to waste your time, I've all I need now! – Travis Arlen McCracken Jun 15 '15 at 8:41
• @TravisArlenMcCracken Have you looked at the formatting help that I suggested? You need to put it in a code block to keep code intact (and prevent all that bold and italic, etc.) It's easier if you look at the help page than if we try to squeeze all that formatting info in a comment ... – Szabolcs Jun 15 '15 at 9:03
• And where are those strings you are talking about in the title? – Kuba Jun 15 '15 at 9:06

## 1 Answer

I am somewhat confused about the aim. Here is an interpretation. Starting with string:

string = "{{13,{17,4},{23,10},220/13,3},{17,{23,6},{29,12},4368/17,3},\
{19,{23,6},{29,12},4368/19,3},{23,{29,6},{41,18},26334/23,6},{29,{41,\
12},{47,18},21474180/29,3},{29,{41,10},{53,22},493350,6},{31,{41,12},{\
47,18},21474180/31,3},{31,{41,10},{53,22},14307150/31,6},{37,{41,12},{\
47,18},21474180/37,3},{37,{41,10},{53,22},14307150/37,6},{29,{47,14},{\
53,20},11978400,3},{31,{47,14},{53,20},11205600,3},{37,{47,14},{53,20}\
,347373600/37,3},{41,{47,14},{53,20},347373600/41,3},{43,{47,14},{53,\
20},347373600/43,3}}"


My interpretation of aim:

ch = Characters[string];
pos = Position[ch, ","]
res = StringReplace[
StringJoin @@
ReplacePart[ch, Thread[Flatten[Last /@ Partition[pos, 7]] -> "J"]],
"J" -> "\n"]


yielding string formatted as:

{{13,{17,4},{23,10},220/13,3}
{17,{23,6},{29,12},4368/17,3}
{19,{23,6},{29,12},4368/19,3}
{23,{29,6},{41,18},26334/23,6}
{29,{41,12},{47,18},21474180/29,3}
{29,{41,10},{53,22},493350,6}
{31,{41,12},{47,18},21474180/31,3}
{31,{41,10},{53,22},14307150/31,6}
{37,{41,12},{47,18},21474180/37,3}
{37,{41,10},{53,22},14307150/37,6}
{29,{47,14},{53,20},11978400,3}
{31,{47,14},{53,20},11205600,3}
{37,{47,14},{53,20},347373600/37,3}
{41,{47,14},{53,20},347373600/41,3}
{43,{47,14},{53,20},347373600/43,3}}