# Appending to each string in a list with mapping

I have a list of 100 things:

endings = ".w_" <> ToString[#] <> "_cold.r_0" <> ".xvg" & /@ Table[i, {i, 0, 99, 1}]


And I'm trying to add 20 different prefixes to the 100 elements, so that I have a list of lists, so I have 20 lists of 100 elements each.

When I try and map onto endings:

all = {"t_" <> ToString[#] <> endings} & /@ Table[i, {i, 0, 95, 5}]


I get a list of 20 lists, each of length 1. The thing I'm appending in all "t_" <> ToString[#] only gets appended to the beginning of the long string for each list. I need to add it to each element in endings.

I tried playing with where I put the {} when I did the mapping in all to no avail.

Is there a nifty way to do this in Mathematica?

To append "t_i" to the elements of endings (i being one of 20 labels), you can simply do:

endings = ".w_" <> ToString[#] <> "_cold.r_0" <> ".xvg" & /@ Table[i, {i, 0, 99, 1}]

prefixes = Table[StringJoin["t_", IntegerString[b]], {b, 1, 20}];

myNewList=Table[StringJoin[prefixes[[a]], #] & /@ endings, {a, 1, Length[prefixes]}]


myNewList contains a list of lists with the prefixes at the beggining of every element (t_i changes from t_1 to t_20 in each of the lists).

• double mapping in mathematica?! whoa -- thanks for the trick! – wiscoYogi Oct 6 '19 at 0:02

You can do this with a single Table command:

Table[
StringJoin["t_", pre, ".w_", end, "_cold.r_0", ".xvg"],
{pre, ToString/@Range[0, 95, 5]},
{end, ToString/@Range[0,99]}
]


This is a good use case for Outer:

f = StringJoin["t_", #, ".w_", #2, "_cold.r_0", ".xvg"] &;

all1 = Outer[f, ToString /@ Range[0, 95, 5], ToString /@ Range[0, 99]]

Dimensions @ all1


{20, 100}

Short @ all1


{{t_0.w_0_cold.r_0.xvg, t_0.w_1_cold.r_0.xvg, <<96>>, t_0.w_98_cold.r_0.xvg, t_0.w_99_cold.r_0.xvg}, <<18>>, {<<1>>}}

Alternatively,

endings = ".w_" <> ToString[#] <> "_cold.r_0" <> ".xvg" & /@ Range[0, 99];
beginnings = "t_" <> ToString[#] & /@ Range[0, 95, 5];

all2 = Outer[StringJoin, beginnings, endings]

all2 == all1


True

endings = ".w_" <> ToString[#] <> "_cold.r_0" <> ".xvg" & /@ Table[i, {i, 0, 99, 1}];

newlist = (pre = "t_" <> ToString[#]; pre <> # & /@ endings) & /@ Table[i, {i, 0, 95, 5}];

Dimensions[newlist]
(* {20, 100} *)
$$$$
`