1
$\begingroup$

About

Watch[...] is a helper function for watching a list of variables or other functions inside a Module[...] during programming or troubleshooting.

Goal Style Row[...] separator so that printed values print apart with distinctively styled separator between items.

Two Coding Attempts

For first attempt (watch1[...]) separator works but does not style separator. For second attempt (watch2[...]) separator is itself styled but kernel complains by reacting with error codes and nothing else works.

list = {a, b, c};

Separator = ToString[Style[" | ", Darker[Green], Bold, 12]];
watch1[list_] := 
  Print[Row[
    Table[ToString[list[[i]]] <> Separator, {i, Length[list]}]]];
watch1[list]

watch2[list_] := 
  Print[Row[
    Table[ToString[list[[i]]] <> 
      Style[" | ", Darker[Green], Bold, 12], {i, Length[list]}]]];
watch2[list]

Specific Request

Please help by correcting code so that it works as cited in Goal.

$\endgroup$
3
$\begingroup$

Try

list = {a, b, c};
Separator = Style[" | ", Darker[Green], Bold, 12];
Row[Flatten@ Table[  {ToString[list[[i]]], Separator}, {i, Length[list]}]]

Mathematica graphics

Or

 Row[Flatten[{ToString[#], Separator} & /@ list]]

Mathematica graphics

Thanks to Bob Hanlon suggestion, Riffle might be better way to go about this so no extra separator at end. The list command above adds | at the end, which OP does not want.

  result = Row[Riffle[list, Separator]]

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ thank you for helping me again and again :) $\endgroup$ May 18 '20 at 0:51
  • 2
    $\begingroup$ watch[list_] := Row@Riffle[list, Separator]; watch[list] $\endgroup$
    – Bob Hanlon
    May 18 '20 at 0:53
  • $\begingroup$ @Nasser i have one more question: why does it print an extra separator (after last item in list)? how could i stop that behavior? $\endgroup$ May 18 '20 at 1:26
  • $\begingroup$ @BobHanlon thank you. i shall use your solution as it does not print an extra separator like Nasser's very generous but slightly buggy solution. $\endgroup$ May 18 '20 at 1:29
  • 1
    $\begingroup$ @JulesManson it might be better in this case to use Riffle like Bob suggested if you do not want seperator at end. $\endgroup$
    – Nasser
    May 18 '20 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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