0

I have a vector/list t = {4, 5, 6}. I want to output each element (its value) of t in a string of characters. For example, the string "Test: " concatenated with each element of t separated by a comma and space. Hence the output should be: Test: 4, 5, 6.

I tried:

  • "Test: " <> ToString[Do[j, {j, t}]] and "Test:" <> ToString[Do[t[[j]], {j, Length[t]}]], but neither outputs the elements of t, and both add Null after "Test: ".
  • "Test: " <> ToString[Do[Print[j], {j, t}]] but it outputs the elements in different lines before "Test: ", and adds Null after "Test: ".

I know the code should be very easy, but I don't know how nor I found similar questions.

Edit

Using the code in this answer, I achieved what I wanted. The actual code is:

WriteString["stdout", "Test: ", t[[1]]];
Do[WriteString["stdout", ", ", t[[k]]], {k, 2, Length[t] - 1}];
WriteString["stdout", ", ", t[[Length[t]]], ".\n"];

Edit 2

Using a code by someone who deleted his comment, and slightly modifying it, we also achieved what I wanted:

"Test: " <> StringJoin[Table[ToString[t[[j]]] <> ", ", {j, Length[t] - 1}]] <> ToString[t[[Length[t]]]] <> "."
alejnavab
  • 453
  • 2
  • 10

0 Answers0