Ask

Ask["key"]

is a construct for use inside AskFunction that gives the value associated with key, or interactively asks for it if it is not already known.

Ask["key"formspec]

uses formspec to define how input should be requested and interpreted.

Ask[{"key1","key2",}]

gives the values associated with all the keyi, interactively asking for any that are needed.

Ask[{"key1"->formspec1,"key2"->formspec2,}]

uses the formspeci to define how input for the keyi should be requested and interpreted.

Details

  • The first time Ask["key"] occurs in the evaluation of AskFunction[{,"key"->spec,},body], a value associated with "key" is interactively requested using the form and interpretation specified by spec. Subsequent evaluations of Ask["key"] return the stored value.
  • Ask["key"] is taken to be equivalent to Ask["key"->"String"] if it is evaluated inside an AskFunction that does not give a specification for how to ask about "key".
  • Values found for "key" using Ask["key"] can be changed through evaluation of subsequent AskAppend[] constructs and can be deleted through AskConfirm[].
  • If a specification for how to ask about a key is given in an AskFunction, it overrides any specifications given in Ask constructs it contains. If no specification is given in the AskFunction, then the specification in the first Ask to be evaluated is used.
  • Ask["key"]=value or Ask["key"]:=value can be used to set the value associated with a key inside an AskFunction, if it was not previously set.
  • Ask["key"]=. unsets the value associated with a key inside an AskFunction and effectively rewinds evaluation inside an AskFunction back to the previous point a value for "key" was requested.

Examples

open allclose all

Basic Examples  (3)

Ask for a number:

Collect a user's name and email:

Already known answers are not asked for again:

Generalizations & Extensions  (6)

Ask multiple questions at the same time:

Ask omits already known questions also when given multiple keys:

Retrieve a known value:

Retrieve multiple known values:

Values can be set programmatically:

If the answer to the first question is "male", the value associated with "pregnant" is programmatically set to False:

Values can also be unset programmatically:

Since the value is not positive, the user is taken back to the original question:

Properties & Relations  (4)

Using Ask before AskAppend will result in the first answer being wrapped in a list before appending the result:

Using Ask after AskAppend will result in the currently known list of values being returned without a form being presented:

AskConfirm can be used to rewind the evaluation to the point before a value was known. Ask values that were deleted by a negative outcome to AskAppend are asked again:

AskConfirm can be simulated by unsetting the value associated with a key:

Possible Issues  (1)

If Ask is provided with a nonexistent interpreter type, it will default to "String":

Introduced in 2016
 (10.4)
 |
Updated in 2017
 (11.1)