pselect()

Read(2145) Label: sequence member, position,

Here’s how to use pselect() function.

A. pselect()

Description:

Get positions of the specified members from a sequence.

Syntax:

A.pselect(x,k)

Search from the kth member when parameter k is present

A.pselect(x1:y1,x2:y2,......xi:yi)

The simplified syntax for the composite query using "&&" . It is equal to A.pselect(x1==y1 && x2==y2 &&...... xi==yi)

Note:

The function gets the position of a member in sequence A and returns the ordinal number of a member that fulfils condition x, or returns an empty sequence/null if it cannot find the eligible member. The return value is also subject to the option working with the function.

Option:

@a

Return a sequence composed of ordinal numbers of all the members that fulfill the condition x; when it is not supplied, return the ordinal number of the first eligible member

@z

Perform the search backwards from the last member; by default, perform the search from the first member

@b

Perform the binary search when A is by default a sequence in ascending order. Note that in this case xi must be in ascending order; if A is not an ascendingly ordered sequence, this option could return incorrect result; when it is used with A.pselect(x1:y1,x2:y2,......xi:yi) to find out members that make cmp(x,y) return 0, simply write A.pselect@b(x1:y1,x2:y2,......xi:yi) without including cmp()

@s

It requires that members in A are ascendingly ordered for expression x; with the binary search, if no members in A can make the expression x return 0, then return the opposite number of the position where x can be inserted

@n

If no sequence member is found, return the result of the length of A plus 1; this option and @a are mutually exclusive

@0

Return 0 when the eligible member isn’t found; this option and @n option are mutually-exclusive

Parameter:

A

A sequence

x

A Boolean expression, whose value can be null. When using @b option, x should be an expression whose return value is an integer

k

Search from the kth member

xi:yi

xi is an expression, and yi is a comparing value

Return value:

The ordinal numbers of the member that fulfils condition x or that makes the result of expression x equals y in the given sequence. @a option works to return a sequence composed of all the ordinal numbers of the member that fulfils condition x, and return a sequence composed of the ordinal numbers of all the members when x is null.

Example:

 

A

 

1

[2,5,4,3,2,9,4,9,3]

 

2

=A1.pselect(~>4)

2

3

=A1.pselect@a(~>4)

[2,6,8]

4

=A1.pselect@z(~>4)

8

5

=A1.pselect@az(~>4)

[8,6,2]

6

=[1,3,5,7,9].pselect@b(~-5)

3; here the syntax is equal to A7’s, and both A6 and A7 return the same result.

7

=[1,3,5,7,9].pselect@b(~:5)

When colon is used, @b won’t change the syntax.

8

=[1,3,5,7,9].pselect@s(~:4)

-3; cannot find 4 and return the opposite number of the position where 4 can be inserted.

9

=[1,3,5,7,9].pselect@n(~:4)

6

10

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

 

11

=A10.pselect(GENDER:"M",DEPT:"Sales")

6

12

=demo.query("select * from EMPLOYEE where GENDER='M' order by EID asc")

EID and GENDER are in the same order.

13

=A12.pselect@b(EID:10,GENDER:"M")

2

14

=A12.pselect@s(EID:3,GENDER:"M")

-1; cannot find the record satisfying EID=3 and GENDER=M and return the opposite number of the position where the record can be inserted.

15

=A12.pselect@n(EID:3,GENDER:"M")

239

16

=[2,5,4,3,2,9,4,9,3].pselect(~>4,3)

6

17

=[2,5,4,3,2,9,4,9,3].pselect@0(~>9)

0

Note:

A must be an ascendingly ordered sequence and xi in ascending order when @b or @s is used, otherwise they will bring about incorrect results.

Related function:

A.select()