containers

ascend, clone, foreach, map, prune, redotify, set, undotify


ascend

Syntax

@parent = ascend(@child)

Description

Returns a reference to the "parent container" of @child (i.e. the variable or element that contains the sub-element referred to by @child). If @child is a top-level variable, void is returned.

Examples

ascend(@x['3']) === @x
ascend(@x.y.z) === @x.y
ascend(@x) === void

clone

Syntax

@target = clone(@source, @target)

Description

Makes a "deep copy" of the container @source to @target, meaning that all elements under the source and any sub-elements that they may have (and so on) will be copied to the target. If there is a variable directly at @source it will also be copied to @target. The returned value is the input @target reference.

Notice that this function does not erase any existing elements under @target. You may want to consider calling prune() on @target first.

Examples

clone(@originalData, @myCarbonCopy)

See Also

copy, prune


<foreach>

Syntax

foreach(@map, >doThis)

Description

Traverses all elements under @map (and any sub-elements that they may have and so on) and calls >doThis once for every encountered element. (An alternative description of foreach() is that it calls >doThis for every found variable that begins with the value of @map # '.') Three arguments will be passed to >doThis:

$0 will be the full reference to the found element (e.g. "::zoo.elephant")
$1 will be the name of the element (e.g. "elephant")
$2 will be the value of the element.

The order with which elements are processed is undefined and depends on the implementation of PikaScript. Any modifications to @map while running foreach() will not be reflected in the calls to >doThis. Notice that you normally would not use foreach() on arrays since it would also include the 'n' element (the element count). Use iterate() or a simple for-loop instead.

Examples

foreach(map(@a, 'Asia', 4157, 'Africa', 1030, 'Americas', 929, 'Europe', 739, 'Oceania', 35), >print($1 # '=' # $2))

See Also

iterate


map

Syntax

@map = map(@map, ['keys', <values>, ...])

Description

Creates a "map" under @map by assigning sub-elements to @map for each key / value pair passed to the function. The returned value is the input @map reference.

Notice that this function does not erase any existing elements under @map so you may call this function repeatedly to incrementally build a map. Use prune on @map to delete it.

A creative use of this function is to create efficient "switch statements" something that is otherwise missing in PikaScript by mapping keys to functions / lambda expressions. You could then execute the switch like this: mySwitch[theKey].

Examples

map(@userInfo['magnus'], 'fullName','Magnus Lidstroem' , 'birthDate','31 march 1972' , 'favoriteColor','#3333FF')
map(@actions, 'hi',>print('Good day sir!') , 'spin',>{ for (i = 0; i < 360; ++i) print('Spun ' # i # ' degrees') } , 'quit',>doQuit = true)
[map(@temp, 'zero',0 , 'one',1 , 'two',2 , 'three',3)]['two'] == 2

See Also

foreach, prune, set


prune

Syntax

+count = prune(@reference)

Description

Deletes the variable referenced to by @reference as well as all its sub-elements. Use prune() to delete an entire "plain old data" container (e.g. an "array" or "map"). Use destruct() instead for deleting an "object" to have its destructor called before it is deleted. prune() returns the number of variables that were deleted.

Examples

prune(@myArray)

See Also

delete, destruct


redotify

Syntax

'zzz...' = redotify('zzz%2e%2e%2e')

Description

Decodes an "undotified" string as returned from undotify(). See help for "undotify" for further explanation and examples.

Examples

redotify('nearly 50%25 use google%2ecom daily') === 'nearly 50% use google.com daily'

See Also

undotify


set

Syntax

@set = set(@set, ['keys', ...])

Description

Creates a "set" under @set by assigning sub-elements with the value true for each key. The returned value is the input @set reference.

Notice that this function does not erase any existing elements under @set so you may call this function repeatedly to incrementally build a set. Use prune() on @set to delete it.

One practical use for sets is to efficiently check if a value belongs to a group of values. Initially you create the group of values with this function and later you can call exists(@set[key]).

Examples

set(@validColors, 'red', 'green', 'blue', 'yellow')
exists(@[set(@smallPrimes, 2, 3, 5, 7, 11, 13, 17, 19)][13])

See Also

foreach, map, prune


undotify

Syntax

'zzz%2e%2e%2e' = undotify('zzz...')

Description

Simply replaces all '.' with '%2e' and all '%' with '%25'. The purpose of this function is to allow arbitrary strings to work as keys in multi-dimensional arrays / deep containers. Without "undotifying" keys, any '.' in the keys would be interpreted as separators. E.g. "files['name.ext'].size" is the same as "files.name.ext.size", which is probably not what you want. Instead use "files[undotify('name.ext')].size". To return the original key from an "undotified" key, use redotify().

Examples

undotify('nearly 50% use google.com daily') === 'nearly 50%25 use google%2ecom daily'
redotify(undotify('a.b.c%d.e.f')) === 'a.b.c%d.e.f'

See Also

redotify