Pike v8.0 release 1738

Method `[]()


Method `[]

mixed `[](object arg, mixed index)
mixed `[](object arg, string index)
function(:void) `[](int arg, string index)
int `[](string arg, int index)
mixed `[](array arg, int index)
mixed `[](array arg, mixed index)
mixed `[](mapping arg, mixed index)
bool `[](multiset arg, mixed index)
mixed `[](program arg, string index)
mixed `[](object arg, mixed start, mixed end)
string `[](string arg, int start, int end)
array `[](array arg, int start, int end)

Description

Indexing.

This is the function form of expressions with the [] operator, i.e. a[i] is the same as predef::`[](a,i).

Returns

If arg is an object that implements lfun::`[](), that function is called with the index argument.

Otherwise, the action depends on the type of arg:

arg can have any of the following types:
object

The non-protected (i.e. public) symbol named index is looked up in arg.

int

The bignum function named index is looked up in arg. The bignum functions are the same as those in the Gmp.mpz class.

string

The character at index index in arg is returned as an integer. The first character in the string is at index 0 and the highest allowed index is therefore sizeof(arg)-1. A negative index number accesses the string from the end instead, from -1 for the last char back to -sizeof(arg) for the first.

array

If index is an int, index number index of arg is returned. Allowed index number are in the range [-sizeof(arg)..sizeof(arg)-1]; see the string case above for details.

If index is not an int, an array of all elements in arg indexed with index are returned. I.e. it's the same as doing column(argindex).

mapping

If index exists in arg the corresponding value is returned. Otherwise UNDEFINED is returned.

multiset

If index exists in arg, 1 is returned. Otherwise UNDEFINED is returned.

program

The non-protected (i.e. public) constant symbol index is looked up in arg.

As a compatibility measure, this function also performs range operations if it's called with three arguments. In that case it becomes equivalent to:

`[..] (arg, start, Pike.INDEX_FROM_BEG, end, Pike.INDEX_FROM_BEG)

See `[..] for further details.

Note

An indexing expression in an lvalue context, i.e. where the index is being assigned a new value, uses `[]= instead of this function.

See also

`->(), lfun::`[](), `[]=, `[..]