# Insert a string and add 0.5 in the last element in every list after [closed]

How to insert a string after every 3rd list and then add 0.5 on every Z on every list after?

Input: {"X1","Y1","Z0"},{"X2","Y2","Z0"},{"X3","Y3","Z0"}...{"X100","Y100","Z0"}

output: {"X1","Y1","Z0"},{"X2","Y2","Z0"},{"X3","Y3","Z0"},{"Randomstring"},{X4,Y4,Z0.5}

• I strongly encourage you to learn the basics about "list manipulation". It's one of the most important things when working with mathematica. – Wizard Dec 7 '15 at 9:38
• I've made a partition, then joined the string and made If ContainsStringsQ change Z->Z+0.5 for every 3rd part. But my list is pretty long and it crashes. So I was wondering if there was an easier way or something. – EminemIsLife Dec 7 '15 at 9:43
• This is a very ill-posed question. We expect questions to be formulated more precisely than this on this site. Do you really mean {X4, Y4, Z0.5}? Or do you mean {"X4", "Y4", "Z0.5"}? Further, what do you mean by "add 0.5 on every Z after ..."? Do you mean {"X4", "Y4", "Z0.5"}, .., {"X100", "Y100", "Z0.5"}? – m_goldberg Dec 7 '15 at 11:32
• I mean "X4" as in a string. Every 3rd element (the last element in the 3rd) in the list should be added by 0.5 Example: {"X4", "Y4", "Z0.5"} .. {"X6", "Y6", "Z1"} – EminemIsLife Dec 7 '15 at 11:37
• Does another random string get inserted after {"X6", "Y6", "Z1"}? Or same random string as after {"X3","Y3","Z0"}? Please edit your question to clarify your intent. People will often miss clarifications made in a comment. – m_goldberg Dec 7 '15 at 12:53

list =
{{"X1", "Y1", "Z0"}, {"X2", "Y2", "Z0"}, {"X3", "Y3", "Z0"},
{"X1", "Y1", "Z0"}, {"X2", "Y2", "Z0"}, {"X3", "Y3", "Z1"},
{"X1", "Y1", "Z0"}, {"X2", "Y2", "Z0"}, {"X3", "Y3", "Z2"}};

step = 3;


Add 0.5 to last element at every step positions

rep =
StringTake[#, 1] <>
ToString[ToExpression@FromCharacterCode@Rest@ToCharacterCode[#] + 0.5] & /@
list[[All, -1]][[step ;; ;; step]]


{"Z0.5", "Z1.5", "Z2.5"}

Replacement positions

pos = Transpose[{#, Array[-1 &, Length@#]}] &[Range[step, Length@list, step]]

{{3, -1}, {6, -1}, {9, -1}}


Replacement

sol = ReplacePart[list, Dispatch@Thread[pos -> rep]];


Insertion

Insert[sol, "RandomString", List /@ Range[Length@pos + 1, Length@list, step]] // MatrixForm