Sannyasin,
It works with any text. Outside of listboxes I let users write formulas for
things like pricing and commissions. It's nice to let them do this in terms
of things they understand so a formula can look like:

P= (Cost * x) + Z
P= (MSRP * (1 - x)) + Z


There are two ways you can evaluate them.
Native 4D code:
You need to have a a process var defined for the result, I use:
'pfcr_price'. Here's a simplified version of a method used to calculate
selling prices. $1 would be a formula like the ones above. x and Z are most
likely static and the cost and msrp vary depending on the item.

$formula:=$1

$x:=$2
$z:=$3
$cost:=$4
$MSRP:=$5

$exe_string:=Price_formula_insert_values ($formula;$x;$z;$MSRP;$cost)
$exe_string:=Replace string($exe_string;",";"")
$exe_string:=Replace string($exe_string;"P=";"pfcr_price:=")

EXECUTE FORMULA($exe_string)

$0:= pfcr_price


Price_formula_insert_values handles inserting the actual values for the 4
variables, x, z, cost and msrp. Then I remove any commas (in v15 they
started choking execution) and swap "P=" for the name of my variable. Now
just execute the line and return the result.

NTK from Rob Laveaux
Since acquiring NTK I've started using Rob's execute function. Preparation
is the same but it works in a single call. Plus he let's you tokenize the
formula which is good if you will be applying it to large data sets.
Another cool tidbit in NTK.

You have to be careful here about how sophisticated you want to get. You
could let users reference any 4D math functions, for example. But you will
need to do the error checking for things like unclosed parens and such. I
keep things pretty simple for the most part. And like I say to provide
on-the-fly ability to do something like add a string of numbers or enter an
amount with a 3.25% discount in a single line is pretty cool.

If you have really sophisticated users you can provide an formula window on
a form, or a listbox line, and give them access to any variables you have
defined.

Speed becomes an issue if you start looking at thousands of operations but
for the flexibility the trade off can be worth it. Or you can look into NTK
as I mentioned.

On Sun, Sep 24, 2017 at 12:04 PM, Sannyasin Siddhanathaswami via 4D_Tech <
4d_tech@lists.4d.com> wrote:

> Very cool idea! Does this formula evaluation only work with arrays? Or
> text fields too?
>

-- 
Kirk Brooks
San Francisco, CA
=======================

*The only thing necessary for the triumph of evil is for good men to do
nothing.*

*- Edmund Burke*
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**********************************************************************

Reply via email to