0
$\begingroup$

How do you input a list of numbers via InputField and immediately generate their sum (or whatever other operation involving a list of numbers) underneath.

I've tried with ImportString:

DynamicModule[{x}, Column[{InputField[Dynamic[x], String, ContinuousAction -> True], Dynamic[Activate@Plus[Delete[#, 0] &@ImportString[x, "List"]]]}]]

Also with ToExpression:

Column[{InputField[Dynamic[x], String, ContinuousAction -> True], Dynamic[xNum = ImportString[x, "List"]], Dynamic[Activate@Plus@Delete[xNum, 0]]}]

Neither one works...

$\endgroup$
1
  • 1
    $\begingroup$ Try DynamicModule[{x = "", xc = {}}, Column[{InputField[Dynamic[x], String, ContinuousAction -> True], Dynamic[xc = ImportString[x, "List", "LineSeparators" -> ","]], Dynamic[Total[xc]]}]]. $\endgroup$ Mar 19, 2018 at 19:30

1 Answer 1

2
$\begingroup$

You could use ToExpression after adding "{" and "}" (I also removed trailing commas):

DynamicModule[{x=""},
    Column[{
        InputField[Dynamic[x], String, ContinuousAction->True],
        Dynamic[Total @ ToExpression["{"<>StringTrim[x,","]<>"}"]]
    }]
]

Here is a short animation:

enter image description here

$\endgroup$

Your Answer

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

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