OScript API/Built-in Package Index

Class: Assoc

The built-in functions in the Assoc package allow you to manipulate the Assoc (Associative) data type. An Assoc is a map for associating arbitrary key/value pairs. An Assoc differs from a Record in that the keys are less expensive to add and remove, the keys can be data types other than Strings, and the keys are sorted by key comparison rather than by some fixed order.

Each Assoc has an "undefined value" that is returned when a nonexistent key is referenced. This "undefined value" is usually Undefined by default, but Assoc.NotSetValue() or any other value may be used.

An Assoc is formally created with Assoc.CreateAssoc(), however OScript has a convenience feature in that this is automatically invoked when an Assoc is declared so that it can be used right away. This does not happen with a Record since, while Assoc is meant to be a tool for relating data in an OScript program, Record is meant to be a tool for accessing database table data.

Class Attributes

The constant type identifier for the Assoc data type.

Class Methods

Copy( Assoc src )

Returns a duplicate Assoc.

CreateAssoc( [Dynamic value] )

Returns a new Assoc.

CreateFromPairs( Dynamic key1, Dynamic value1, Dynamic key2, Dynamic value2 )

Returns a new Assoc.

Delete( Assoc a, Dynamic key )

Removes a key/value pair from an Assoc.

FromRecord( Record rec )

Converts a Record to an Assoc.

IsCyclic( Assoc a )

Returns an assoc containing key/value pairs from the argument list key.

IsKey( Assoc a, Dynamic key )

Returns true if the Assoc contains key.

Items( Assoc a )

Returns a List of all the values in an Assoc.

Keys( Assoc a )

Returns a List of all the keys in an Assoc

Merge( Assoc assoc1, Assoc assoc2 )

Merges assoc2 into assoc1.

Returns a unique data type constant which can be used as the "undefined value".

ToRecord( Assoc a )

Converts an Assoc into a Record.

UndefinedValue( Assoc a, [Dynamic value] )

Returns and optionally sets the "undefined value" of an Assoc.

Class Attributes

Integer AssocType

The type number for the Assoc data type.

Class Methods

Copy

Assoc Copy( Assoc src )

Creates and returns a shallow copy of the specified source Assoc. This is necessary since Assocs are passed by reference, meaning that simple assignment does not copy an Assoc as it does with an Integer. The copying process follows the same conventions used in function parameter passing. Thus the contents of the copy will contain copies of those values in the source which are passed by value, and and references to those values in the source which are passed by reference. The following example illustrates this:

Parameters

src

The Assoc to copy.

Returns:

The new Assoc copy.

CreateAssoc

Assoc CreateAssoc( [Dynamic value] )

Returns a new, empty Assoc whose "undefined value" will be Undefined or value if value is specified. The "undefined value" is the value returned by an Assoc when referenced by a key it does not contain.

Parameters

value

The "undefined value" of the returned Assoc, which will be Undefined if this argument is not specified.

Returns:

A new, empty Assoc.

CreateFromPairs

Assoc CreateFromPairs( Dynamic key1,
                       Dynamic value1,
                       Dynamic key2,
                       Dynamic value2 )

Returns a new Assoc with values as specified in the input arg list.

Parameters

key1

First key

value1

First value

key2

Second key

value2

Second value

Returns:

A new Assoc.

Delete

Dynamic Delete( Assoc a,
                Dynamic key )

Removes the specified key from the Assoc if that Assoc contains the key.

Parameters

a

The Assoc to remove a key/value pair from.

key

The key for the key/value pair to remove.

Returns:

The value removed, or the "undefined value" if the key was not found.

FromRecord

Assoc FromRecord( Record rec )

Creates and returns a new Assoc containing the field names and corresponding columns of the specified Record as its String keys and associated values. As with Assoc.Copy(), the new Assoc returned will be a shallow copy of the Record.

Parameters

rec

The Record from which an Assoc will be created.

Returns:

A new Assoc, copied from the Record.

IsCyclic

Boolean IsCyclic( Assoc a )

Returns true if this assoc contains any self-referencing cycles.

Parameters

a

The Assoc to be checked.

Returns:

True if the Assoc contains any self-referencing cycles.

IsKey

Boolean IsKey( Assoc a,
               Dynamic key )

Returns true if the specified Assoc contains the specified key.

Parameters

a

The Assoc whose key set is tested.

key

The key to test.

Returns:

True if the Assoc contains the key.

Items

List Items( Assoc a )

Returns a List of the keyed values in a.

Parameters

a

The Assoc whose values are returned in a List.

Returns:

A List of the keyed values in the Assoc.

Keys

List Keys( Assoc a )

Returns a List of the keys in a.

Parameters

a

The Assoc whose keys are returned in a List.

Returns:

A List of keys in the Assoc.

Merge

Assoc Merge( Assoc assoc1,
             Assoc assoc2 )

The two Assocs are merged by copying the contents of assoc2 into assoc1. The copy process is the same as used for Assoc.Copy(). If a key is common to both Assocs, then the associated value in assoc1 will be replaced by that in assoc2, as shown by the following example:

Parameters

assoc1

Destination Assoc for merge.

assoc2

Assoc whose contents are copied into assoc1.

Returns:

A reference to the first assoc passed in.

NotSetValue

NotSet NotSetValue()

Returns a unique data type constant useful as the "undefined value" for an Assoc when Undefined or other values will not work since they might be keys within the Assoc. Use with Assoc.CreateAssoc() or Assoc.UndefinedValue().

Returns:

A unique data type constant useful as the "undefined value" for an Assoc.

ToRecord

Record ToRecord( Assoc a )

Creates a new Record and returns it after copying the values of the given Assoc into it, translating String keys and values into field names and column entries. The Assoc should only contain String keys. As with Assoc.Copy(), the new Assoc returned will be a shallow copy of the Record.

Parameters

a

The Assoc to convert into a new Record.

Returns:

A new Record copied from the Assoc.

UndefinedValue

Dynamic UndefinedValue( Assoc a,
                        [Dynamic value] )

Returns the "undefined value" for the specified Assoc. If value is specified than the "undefined value" for the Assoc will be changed to value (meaning value will be returned).

Parameters

a

The Assoc whose "undefined value" is queried and optionally set.

value

If specified, then the "undefined value" of _a _will be set to this.

Returns:

The "undefined value" for the Assoc.

 Copyright © 2021 OpenText Corporation. All rights reserved.