calc()

Read(307) Label: compute an expression, result,

Here’s how to use calc() function.

A.calc()

Description:

Compute an expression with one or more specified records and return a result.

Syntax:

A.calc(k,x)

Compute x against the kth member of A and return the result

A.calc(p,x)

Compute x against the members of A specified by the integer sequence p and return a sequence

Note:

The function computes an expression against one or more specified records and return a result.

Parameters:

A

A sequence/a record sequence

x

An expression, which is generally a field name or a legal expression composed of field names, and "~" is used to reference the current record.

k

An integer specifying a record

p

An integer sequence specifying multiple records

Return value:

The result of x or a sequence composed of results of x

Example:

Ø  Compute x against the kth member

 

A

 

1

=[1,3,6,2,8]

 

2

=A1.calc(3,~*2)

12

Compute expression "~*2" against the third member of sequence A1, "~" indicates the current member. The result is 12.

Ø  Compute x against the members of A specified by the integer sequence p

 

A

 

1

=[1,3,6,2,8]

 

2

=A1.calc([4,3],~*2)

[4,12]

Compute expression "~*2" against the fourth and the third members of sequence A1 separately and the result is [4,12].

Ø  Do the computation against a table sequence

 

A

 

1

=demo.query("select * from EMPLOYEE")

 

2

=A1.calc(2,age(HIREDATE)+5)

Compute expression against the second record, the result is 11

3

=A1.calc(A1.pselect@a(DEPT=="Administration"),

age(HIREDATE)+5)

Compute "age(HIREDATE)+5" against all the records whose DEPT is "Administration" and return the result sequence  [19,11,11,10]