Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been struggeling with the command riffle.

input: list = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}, {13, 14, 15}}

How do I use riffle like this, but add 1 to "X", every time it gets inserted? So when it has inserted "X" 5 times, it would be X5, instead of just "X".

"X" is a string.

Riffle[list, "X", 3]

 {{1, 2, 3}, {4, 5, 6}, X, {7, 8, 9}, {10, 11, 12}, X, {13, 14, 15}}

The output I am looking for:

Output :{{1, 2, 3}, {4, 5, 6}, X1, {7, 8, 9}, {10, 11, 12}, X2, {13, 14, 15}}`

share|improve this question
3  
Why not spend some quality time with your previous riffle-related questions and learn from the comments/answers there? Once you get the hang of the basics, iterations on the insert-at-x-with-y become trivial. – Yves Klett Dec 7 '15 at 14:21
    
@EminemIsLife, do you think you will ever start to accept the answers according to the site charter? – garej Dec 8 '15 at 21:52
1  
How do I accept the answers? I'm very thankful for all the answers. – EminemIsLife Dec 8 '15 at 22:29
    
@EminemIsLife, I see I've managed it =)) You may just vote for all responses and add comments for those who might also deserved 'accept' check. – garej Dec 9 '15 at 16:38
    
@EminemIsLife, by the way, if you want your comment got seeing by addressee, type @<name>. I repeat those thing because see that you like to quickly ask a lot of questions =)) – garej Dec 9 '15 at 16:42
up vote 8 down vote accepted

Create a list to riffle with,

Riffle[list, Table[StringJoin["X", ToString[i]], {i, 1, Length[list]}], 3]

{{1, 2, 3}, {4, 5, 6}, "X1", {7, 8, 9}, {10, 11, 12}, "X2", {13, 14, 
  15}}
share|improve this answer
Module[{i = 1}, Riffle[list, "X", 3] /. "X" :> "X" <> ToString[i++]]

{{1, 2, 3}, {4, 5, 6}, "X1", {7, 8, 9}, {10, 11, 12}, "X2", {13, 14, 15}}

share|improve this answer
Riffle[list, Table["X" <> ToString[i], {i, Ceiling[Length[list]/3]}],
  3]

{{1, 2, 3}, {4, 5, 6}, "X1", {7, 8, 9}, {10, 11, 12}, "X2", {13, 14, 15}}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.