1
$\begingroup$

I need a string like the following:

"2+2
\"Hello
world\"
3+3"

to be evaluate like

2+2
"Hello 
world"
3+3

Note that ToExpression evaluates that string as

2+2;
"Hello 
world";
3+3

that is that it prints only the last line.

$\endgroup$

1 Answer 1

1
$\begingroup$

If I understand your question correctly, the following code

str = "2+2
\"Hello
world\"
3+3";
ToExpression[str, InputForm, Print /@ List[##] &];

does what you want. Alternatively, you can use

ToExpression[str, InputForm, List]

and decide what to do with the list later. The documentation for ToExpression states:

ToExpression[input, form, h]

wraps the head h around the expression produced before evaluating it.

$\endgroup$
1
  • $\begingroup$ Yes, thanks. I came to a similar solution: Print/@(List@@ToExpression[s, Inputform, Hold]). So, in general terms, how would you explain the role of the third argument of ToExpression ? and what is the default function ToExpression calls if the third argument is omitted? $\endgroup$
    – Fortsaint
    Commented Aug 26, 2019 at 22:57

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.