Extracted from Pike v7.3 release 14 at 2002-02-15.
pike.roxen.com
[Top]

Method filter()


Method filter

array filter(array arr, function fun, mixed ... extra)
mixed filter(mixed arr, void|mixed fun, void|mixed ... extra)

Description

Map a function over elements and filters.

Calls the given function fun for all elements in arr, and keeps the elements in arr that resulted in a non-zero value from the function.

arr can have any of the following types:
array

If fun is an array:

for (i=0; i<sizeof(arr); i++) {
   		    if (fun[i]) res += ({ arr[i]});
   	    
otherwise:
keep = map(arr, fun, @extra);
   		  for (i=0; i < sizeof(arr); i++) {
   		    if (keep[i]) res += ({ arr[i]});
   	    

multiset

(multiset)filter((array)arr, fun, @extra)

mapping|program|function

ind = indices(arr);
   		val = values(arr);
   		keep = map(val, fun, @extra);
   		for (i=0; i<sizeof(keep); i++) 
   		  if (keep[i]) res[ind[i]] = val[i];
   	  

string

(string)filter((array)arr, fun, @extra)

object

if

arr->cast
, try in turn:
filter((array)arr, fun, @extra)
filter((mapping)arr, fun, @extra)
filter((multiset)arr, fun, @extra)


Returns

Returns the same datatype as given, the exceptions are program and function that give a mapping back.

See also

map, foreach()