Saving User Input in Variables and Macros

 

When the user inputs information into a menu, you may want to store it in a variable or a macro for use.  For example, the menus below prompt the user for the length and sample spacing of the desired waveform, and store these values in variables and macros.  

 

The following menu stores input in variables wlen and wspc:

 

// mkwave.pan

@dialog

 

Generate Noise

 

Length:  <w=20>~wlen=< >~input(2)

Spacing: <w=20>~wspc=< >~input(1)

<L>

~menuclear;GRANDOM(wlen,wspc)

 

 

 

 

In this example, both display area fields input the w=20 modifier to specify an explicit in field width of 20 characters.

 

If the Length field is selected, DADiSP evaluates the expression INPUT(2) to enter an integer value into the dialog box. This value is substituted in the second set of < > located in the expression area of the Length field.

 

If the Spacing field is selected, DADiSP evaluates the expression INPUT(1) to enter a real value into the dialog box. This value is substituted in the second set of < > located in the expression area of the Spacing field.

 

For example, if you enter 100 for length and 0.01 for the spacing, DADiSP substitutes those values when it processes the dialog box.  

 

After the OK button is pressed to process the dialog box, DADiSP evaluates the expressions:

 

wlen=100

wspc=0.01

menuclear;GRANDOM(wlen,wspc)

 

DADiSP assigns the variables, clears the dialog box, and generates a 100 point random series with sample spacing of 0.01.

 

The same menu could be written using the SETVAR function:

 

// mkwave2.pan

@dialog

 

Generate Noise

 

Length:  <w=20>~setvar("wlen", < >)~input(2)

Spacing: <w=20>~setvar("wspc", < >)~input(1)

<L>

~menuclear;GRANDOM(wlen,wspc)

 

Because SETVAR sets a global variable and global variables can be accessed from any SPL routine, this form is useful if the menu is invoked from a variety of contexts, such as multiple SPL functions.

 

Many built-in legacy menus use DEFMACRO to save input values.

 

// mkwave3.pan

@dialog

 

Generate Noise

 

Length:  <w=20>~defmacro("mlen", < >, 1)~input(2)

Spacing: <w=20>~defmacro("mspc", < >, 1)~input(1)

<L>

~menuclear;GRANDOM(mlen,mspc)

 

However, variables are preferred for new or custom dialog boxes.

 

Variable Name Convention

 

You may notice that many built-in dialog boxes that reside in the .\menus folder use variables that begin with an _ underscore, such as _form_func. Variables referenced in a dialog box are typically global variables. Variables that begin with an _ underscore are not saved with a worksheet, insuring that a newly loaded worksheet does alter previously selected dialog box options. In addition, _ variables do not appear in the standard variable display list and are much less likely to conflict with user defined variable names. Although for clarity the examples here may differ, it is generally recommended to preface variable names in production dialog boxes with the underscore.