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
    $\begingroup$ Try DynamicModule[{x = "", xc = {}}, Column[{InputField[Dynamic[x], String, ContinuousAction -> True], Dynamic[xc = ImportString[x, "List", "LineSeparators" -> ","]], Dynamic[Total[xc]]}]]. $\endgroup$ – J. M.'s technical difficulties Mar 19 '18 at 19:30
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

|improve this answer|||||
$\endgroup$

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.