pkg_default(P;K;D)

Returns a value from a package at a given key position, or returns a default value if the value is invalid. (Available as of version 15.03)

Syntax

pkg_default(P;K;D)

Input

Argument Type Description
P any The name of a package variable
K any The name of a key
D any If P does not contain the key K or its value does not have the same type as D, it will return the value in D.

Return Value

Returns a single value from a package at position K. The return type will always match that of the default value (D).

If P is N/A, an error is returned.

If the value at position K is N/A, the default (D) is returned.

If the value at position K is of a different type than D, the default (D) is returned.

If the value at position K is a charvec/cstring while D is a symbol (or vice versa), pkg_default(P;K;D) returns the value at position K, but it will convert the value to match the value in D.

Sample Usage

As an example, the following function call:
value="{pkg_default(@package;'keyN';'default')}"
returns the value located at keyN in list-value @package. If there is no value at keyN or if the type of keyN is different from the type of default, the function returns default.

Example

The following example shows how dejson(string;) converts a "mixed" JSON string into package of name-value pairs that 1010data can use, and then pkg_default(P;K;D) returns a scalar value located at key position K in package P.

<base table="default.test.json"/>
<colord cols="mixed"/>
<willbe name="p" value="dejson(mixed;'symbols')"/>
<willbe name="a" value="pkg_default(p;'a';98.6)"/>
<willbe name="c" value="pkg_default(p;'c';'noun')"/>

The resulting table is as follows:

In column p, dejson(string;) converts the mixed JSON into a package of name-value pairs. Column a contains the value of a, and column c contains the value of c. In the third row of the table, the data type for c does not match the default, so the default value, noun, is used. The last row of the table does not contain an a or c value at all, so the a and c columns contain the default values 98.6 and noun, respectively.