Simple maths problem

Hi,

I want to enter values into fields A and B and have field C show A/B

<$vars a={{{ [get[A]] }}}>
<$vars b={{{ [get[B]] }}}>
{{{ [<a>divide<b>fixed[1]] }}}

This does the calculation (although I don’t know if this is over the top and there’s a simpler solution), but I want the calculation to appear in field C.

Regards
Jon

Be aware that [get[A]] will get the value of the field A for EVERY tiddlers of the wiki, here you will get only a single value because the a variable contains the first result of a filtered transclusion. To get the value of the current tiddler, you can either do a={{!!a}}, a={{{ [<currentTiddler>get[a]] }}}, a={{{ [{!!a}] }}}

Also note that you can set several variables in a single vars widget. The let widget does the same but with the added bonus to be able to use variables declared inside the same let widget, so you could do this:

<$let a={{!!a}} b={{!!b}} c={{{ [<a>divide<b>fixed[1]] }}}>
<<c>>
</$let>

In some case it’s not necessary to use variables at all:

{{{ [{!!a}divide{!!b}fixed[1]] }}}

To set the value of a field, you can use the action-setfield widget. It will be triggered within a triggering widget (for example a button widget), and apply the value to the designated field :

<$edit-text field="a"/>/<$edit-text field="b"/><$button actions="""
<$action-setfield c={{{ [{!!a}divide{!!b}fixed[1]] }}}/>
""">={{!!c}}</$button>

Learn more about filters here: Filters

EDIT: if you want to get a bit fancy:

\define calc() <$action-setfield c={{{ [{!!a}divide{!!b}fixed[1]] }}}/>
\define input(field)<$edit-text inputActions=<<calc>> type="number" field=<<__field__>> />

<<input a>>/<<input b>>=<$button actions=<<result>> >{{!!c}}</$button>
3 Likes

Many thanks, and for the detailed explanation.

Regards
Jon

1 Like

Instead of setting the value of the field, I notice that if I type

{{{ [{!!a}divide{!!b}fixed[1]] }}}

as the field value of field c, the calculation also works.

Is this ok or bad practice?

Regards
Jon

Actually, that would be no good as I want the value to appear in a table so setting the value would be the right way to go.