pos()

Read(2422) Label: pos,

Here’s how to use pos() functions.

A.pos(x,k )

Description:

Get the position of a member in a sequence.

Syntax:

A.pos(x,k)

Note:

The function locates member x in sequence A where x may appear repeatedly. Return null if the member cannot be found; return different values according to different options and return the position of the first-found member if no option is present. When parameter k is present, begin the search from the kth member.

Parameter:

A

A sequence object or an expression that returns one

x

A member

k

Search from the kth member

Option:

@b

Use the binary search to query the location. In this case A is by default an ordered sequence. Both increasing and decreasing are applicable

@a

Return an n-integer sequence consisting of sequence numbers of all the members that fulfill the query condition

@z

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

@s

With an ordered A and the binary search, return the position of x if it is a member of A; otherwise, return the opposite number for the sequence number representing the position where x can be inserted sequentially

@p

Find position of the sequence x, which needs to be treated as a single value, in A, which, in this case, is a sequence of sub-sequences

@n

Return the result of the length of A plus 1 if x cannot be found in A. This option and @a are mutually exclusive

Return value:

A sequence number or a sequence composed of sequence numbers

Example:

 

A

 

1

=[1,2,8,4,5,6,7,8]

 

2

=A1.pos(8)

3

3

=A1.pos@a(8)

[3,8]

4

=A1.pos@z(8)

8

5

=A1.pos@az(8)

[8,3]

6

=A1.pos@z(9)

null

7

=[1,2,3,4,5,6,7,8].pos@b(5)

5

8

=[1,2,4,5,6,7,8].pos@s(3)

-3

9

=[[6,2,1,4,6,3,7,8],[1,4,6]].pos@p([1,4,6])

2

10

=[6,2,1,4,6,3,7,8].pos@n(5)

9

11

=[6,2,1,4,6,3,7,8].pos(6,3)

5

Note:

If A is not ordered, then options @b and @s should not be used, or they may bring about the incorrect results. For example: [7,6,1,2].pos@b(3) returns null.

Related function:

A.pos(x)

A.psort()

A.pos( x )

Description:

Get positions of members of a sequence in another sequence.

Syntax:

A.pos(x)

Note:

The function gets positions of all members of sequence x in sequence A and returns null if it cannot find positions for all members of x.

Parameter:

A

A sequence

x

A sequence

Option:

@i

Get a monotonically increasing integer sequence

@c

Get position of the continuous sub-sequence x of sequence A when it appears the first time

@b

A is by default assumed as an ordered sequence and use binary search to get the position increasingly or decreasingly

Return value:

Integer sequence

Example:

[6,2,1,4,6,3,7,8].pos([1,4, 6])

[3,4,1]; get an integer sequence of positions of members in sequence [1,4,6] in sequence [6,2,1,4,6,3,7,8] when they first appear in the latter.

[6,2,1,4,6,3,7,8].pos([1,4, 9])

null; return null as member 9 of sequence [1,4, 9]; cannot be found in sequence [6,2,1,4,6,3,7,8].

[6,2,1,4,6,3,7,8].pos@i([1,4,6])

[3,4,5]; use @i option to get a monotonically increasing integer sequence of positions of members in sequence [1,4,6] in sequence [6,2,1,4,6,3,7,8].

[6,2,1,4,6,3,7,8].pos([3,6,4])

[6,1,4].

[6,2,1,4,6,3,7,8].pos@i([3,6,4])

null; use @i option but return null because sub-sequence [3,6,4] whose members’ positions in sequence [6,2,1,4,6,3,7,8] isn’t a monotonically increasing integer sequence does not exist.

[2,1,4,6,3,7,8,4,6,1].pos@c([4,6])

3; get position of the continuous sub-sequence [4,6] in sequence [2,1,4,6,3,7,8,4,6,1] when it first appears.

[1,2,3,4,6,7,8].pos@b([3,1,4,6])

[3,1,4,5]; [1,2,3,4,6,7,8] is an ordered sequence and use binary search to get the position.

Related function:

A.pos()

A.psort()

pos()

Description:

Get the position of a substring in its parent string.

Syntax:

pos(s1, s2{, begin})

Note:

The function finds the position of substring s2 in the parent string s1, which starts from the position begin, and returns null if the substring cannot be found.

Parameter:

s1

The parent string where you want to search for a substring

s2

Substring of interest

begin

The position from which the search starts; the default search is from the beginning

Option:

@z

Perform the search leftward starting from begin, while the search will be done rightward by default

@c

Case-insensitive

@h

Search for the substring at the beginning of the parent string by ignoring parameter begin

@q

Skip quoted strings when searching the parent string

@w

Only locate a same and whole substring

Return value:

Integer type

Example:

pos("abcdef","def")

4

pos("abcdefdef","def",5)

7

pos("abcdef","defa")

(null)

pos@z("abcdeffdef","def",7)

4

pos("abcdeffdef","def",7)

8

pos@c("abcdef","Def")

4; @c option makes case-insensitive.

pos ("a'b'cbe","b")

3

pos@q("a'b'cbe","b")

6; use @q option to skip quoted strings.

pos("aab,aa","aa")

1

pos@w("aab,aa","aa")

5; use @w option to locate the same and whole substring only.

pos@h("abcdef","abc",3)

1; use @h option to match the parent string from the beginning. That is, ignore parameter begin and search for the substring from the 1st character of parameter s1.