1
$\begingroup$

Although I searched a bit on google but did not find a quick and easy answer.

So suppose we have an array with, say 3 elements

X[1]={a,b};X[2]={w,f,s};X[3]={t,y,u};

Now I want to print them in the same line using Do loop. I use the code

Do[Print[X[i]],{i,3}]

But it will print in different lines. I also want to add text in between two elements. I tried to wrap everything using Row[] but it didn't work.

$\endgroup$
2
  • $\begingroup$ Don’t use Do, don’t use Print either. Row or Grid would have been my first choices. Can you show us how exactly you used Row, what output you obtained, and what you wanted to obtain instead? You May also be interested in Riffle to interleave elements into an array. $\endgroup$
    – MarcoB
    Commented Jun 17, 2020 at 2:47
  • $\begingroup$ I tried these two code Row[Do[Print[X[i]], {i, 3}]] and Do[Row[Print[X[i]]], {i, 3}]. The output I want is: "The set is : {a,b} && {w,fs} && {t,yu}". All in a single line. The used do loop as the number of elements (which is 3 here) is not fixed. $\endgroup$
    – Epsilon
    Commented Jun 17, 2020 at 3:24

2 Answers 2

1
$\begingroup$

The output I want is: "The set is : {a,b} && {w,fs} && {t,yu}". All in a single line

 X[1]={a,b};X[2]={w,f,s};X[3]={t,y,u};

 And @@ (X[#] & /@ Range[3])

Mathematica graphics

 Print["the set is : ", And @@ (X[#] & /@ Range[3])]

enter image description here

if someone wants to use a different in between symbol, like "--" instead of "&&" what changes will be needed?

If "--" is meant to be just normal text of 2 dashes and not some Mathematical operator, then only way I could think of is to make the whole thing string. Something like

str = StringJoin@Riffle[(ToString[X[#]] & /@ Range[3]), "--"];
Print["the set is : ", str]

enter image description here

$\endgroup$
1
  • $\begingroup$ thanks a lot. One small question if someone wants to use a different in between symbol, like "--" instead of "&&" what changes will be needed? $\endgroup$
    – Epsilon
    Commented Jun 17, 2020 at 4:12
2
$\begingroup$

I would use:

X[1] = {a, b}; X[2] = {w, f, s}; X[3] = {t, y, u};

Row[Array[X, 3], "&&"]
Row[Array[X, 3], "--"]
{a, b} && {w, f, s} && {t, y, u}
{a, b} -- {w, f, s} -- {t, y, u}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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