# List of symbols wherein position encoded by number to string

How to transform list wherein position of the symbol encoded by number at second position

{{a,6},{z,1},{g,2},{y,12}}


to string

zg---a-----y


list = {{a, 6}, {z, 1}, {g, 2}, {y, 12}};

#2 -> ToString[#] & @@@ list // Append[_ -> "-"] // SparseArray // StringJoin


"zg---a-----y"

• SparseArray is very clever. (+1) – Edmund Dec 27 '16 at 15:56

One more alternative, using StringReplacePart[]:

l = {{"a", 6}, {"z", 1}, {"g", 2}, {"y", 12}};
str = StringJoin[ConstantArray["-", Max[l[[All, -1]]]]];
Fold[StringReplacePart[#1, Sequence @@ #2] &, str,
MapAt[ConstantArray[#, 2] &, l, {All, -1}]]


Pre-processing:

str = {{a, 6}, {z, 1}, {g, 2}, {y, 12}};
len = Max@Last@Transpose@str
out = StringRepeat["-", len]


String manipulations:

repl = {ToString /@ #1, Transpose@{#2, #2}} & @@ (Transpose@str)
StringReplacePart[out, #1, #2] & @@ repl


"zg---a-----y"

Modify a string of "-"s with the correct length:

list = {{a,6},{z,1},{g,2},{y,12}};
len = Max[list[[All, 2]]];
chars = ConstantArray["-", len];
Do[chars[[s[[2]]]] = ToString @ s[[1]], {s, list}];
StringJoin[chars]


Like the first answer, this keeps list as a list until the end:

list = {{a, 6}, {z, 1}, {g, 2}, {y, 12}};
Fold[ ReplacePart[ #1, #2[[2]] -> ToString @ #2[[1]] ] &
, Table["-", 12], list] // StringJoin