Numerical Functions
(NOTE: Some of the techniques shown or described on this page--marked in purple--require new features in the latest official PIKT 1.19.0 release (pikt-current.tar.gz) that are unavailable in any previous version.)
Pikt offers a wide variety of built-in functions, both numerical and string. An unusual feature of Pikt functions is that they are data-typed: their return value is signified by either the "$" (for string) or "#" (for number) prefix. (Functions never return a preceding or previous value, so no functions are of the type () or %foo().) (Note: Whitespace between the function name and the opening left parenthesis is illegal.)
Note that many of the functions have synonymous names, in some cases abbreviations, in other cases names by which they are known in other popular programming languages.
In the function descriptions below, "s" signifies any string expression; "i" any integer expression; "n" any numerical expression; "d" any datevalue; "t" any timevalue; "F" any filehandle; and "x" any expression that evaluates to either TRUE (non-zero) or FALSE (zero).
Where inappropriate in any of the following functions, a non-integer numerical argument will have the function return the string "<
For any of the hyperlinked functions, follow the links to view detail pages, with additional explanation and examples.
The numerical functions (functions that return a number value) include:
#abs(n) #absval(n) #absolutevalue(n) returns the absolute value of n #age(s1,s2,s3) #age(s) #fileage(s1,s2,s3) #fileage(s) returns the age in days since s1/s2/s3, whether s1 is a full month name or its three-letter equivalent, and whether s3 is a four-digit year (yyyy) or a time (hh:mm) (s2 must be a date); or returns the age in days of file or directory s #and(x1,x2,...) returns TRUE if expressions x1, x2, ... are all TRUE (non-zero); FALSE otherwise (if one or more expressions are FALSE or zero) #ascii(s) [see #code()] #avg(n1,n2,...) #average(n1,n2,...) #mean(n1,n2,...) returns the average (mean) of the values n1, n2, ... #ceiling(n) returns n rounded up to the nearest integer #code(s) #ascii(s) #ord(s) returns the ASCII value of the first char in s #datevalue(i1,i2,i3) returns the datevalue for the date i3/i2/i1 (dd/mm/yyyy) #day() returns the current calendar day (1-31) #day(d) returns the calendar day (1-31) of the datevalue d #daynumber() returns the current weekday (1-7) #daynumber(s) returns the weekday (1-7) for the string s ("Sunday", "Monday", ...; "Sun", "Mon", ...) #daysbetween(d1,d2) returns the number of days between the datevalues d1 and d2 #defined(x) #defined(s) #defined(n) for x, s, or n; returns TRUE (1) if x resolves to a value, or in the case of variables has appeared in a previous dat or set statement, or has a previous or preceding value; FALSE (0) otherwise; #defined(ERR), #defined(NIL), #defined("<Refer to the sample alarms.cfg for examples.>"), and #defined("< >") are always FALSE (0) #eoi() #endofinput() returns TRUE if there is no input, if at the last input line, or if in the end section; FALSE otherwise #err() returns ERR #error(s) #error(n) returns TRUE (1), if evaluating s or n produces an error; FALSE (0) otherwise #even(i) returns TRUE, if i is even; FALSE, if i is odd #false() returns FALSE (0) #fclose(F) closes file F; returns TRUE if successful, ERR if unsuccessful #fileage() [see #age()] #fileino(s) for path s, returns its inode #filelinks(s) for path s, returns its link count #fileuid(s) for path s, returns the uid of its owner #filegid(s) for path s, returns the gid of its group owner #fileatime(s) for path s, returns its atime (as a timevalue) #filectime(s) for path s, returns its ctime (as a timevalue) #filemtime(s) for path s, returns its mtime (as a timevalue) #filesize(s) for path s, returns its size (in bytes) #find() [see #search()] #fopen(F,s1,s2) opens file s1 with mode s2 (any of "r", "w", or "a"); returns TRUE if successful, ERR if unsuccessful #grgid(s) #grgid(i) #groupgid(s) #groupgid(i) for group name s, or gid i, returns its corresponding gid (as recorded in the system group file) #hour() returns the current hour (0-23) #hour(d) returns the hour portion (0-23) of the datevalue d #hour(t) returns the hour portion (0-23) of the timevalue t #if(x,n1,n2) returns n1, if x is TRUE; else n2, if x is FALSE #if(x,n) returns n, if x is TRUE; else the special numerical value NIL, if x is FALSE #index() [see #search()] #inlen() #inputlength() returns the length (number of chars) of the current input line; 0 if outside the input processing loop #innum() #inputnumber() returns the line number of the current input line; 0 if still in the begin section; the line number of the last input line if in the end section #int(n) #integer(n) returns n rounded down to the nearest integer #isalnum(s) returns TRUE, if the first char in s is alphanumeric #isalpha(s) returns TRUE, if the first char in s is a letter #iscntrl(s) returns TRUE, if the first char in s is a control char #isdigit(s) returns TRUE, if the first char in s is a digit #isgraph(s) returns TRUE, if the first char in s is any printing char, except space #islower(s) returns TRUE, if the first char in s is a lower-case letter #isprint(s) returns TRUE, if the first char in s is any printing char, including space #ispunct(s) returns TRUE, if the first char in s is any printing char but neither a space nor alphanumeric #isspace(s) returns TRUE, if the first char in s is a whitespace char #isupper(s) returns TRUE, if the first char in s is an upper-case letter #keys(s) returns the number of keys in the associative array s; maps those keys to a string var when used as 'for in #keys(s)'; the keys are sequenced in the order encountered during the script run #keys(s1,s2) returns the number of keys in the associative array s2; maps those keys to string var s1 when used as 'foreach #keys(s1, s2)'; the keys are sequenced in the order encountered during the script run #length(s) returns the length (number of chars) in string s #level() returns the current alarm's severity level, 0 through 7 (representing EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG) #max(n1,n2,...) #maximum(n1,n2,...) returns the maximum value among n1, n2, ... #mean() [see #average()] #median(n1,n2,...) returns the median value among n1, n2, ... #members(s2,s3,s4,...) like #keys() (which see), except that returns the number of associative array keys in the sequence s2, s3, s4, ...; maps those keys to a string var when used as 'for in #members(s2,s3,s4,...); the keys are sequenced in the order specified in the #members() function #members(s1,s2,s3,s4,...) like #keys() (which see), except that returns the number of associative array keys in the sequence s2, s3, s4, ...; maps those keys to s1 when used as 'foreach #members(s1,s2,s3,s4,...); the keys are sequenced in the order specified in the #members() function #min(n1,n2,...) #minimum(n1,n2,...) returns the minimum value among n1, n2, ... #minute() returns the current minute (0-59) #minute(d) returns the minute portion (0-59) of the datevalue d #minute(t) returns the minute portion (0-59) of the timevalue t #mode(n1,n2,...) returns the mode value among n1, n2, ... #month() returns the current month (1-12) #month(d) returns the month (1-12) of the datevalue d #monthnumber() returns the current month (1-12) #monthnumber(s) returns the month (1-12) for the string s ("January", "February", ...; "Jan", "Feb", ...) #nil() returns NIL #not(x) returns TRUE if x is FALSE (0); else FALSE if x is TRUE (non-zero) #now() returns the current timevalue (number of seconds since the Unix Epoch, January 1, 1970) #odd(i) returns TRUE, if i is odd; FALSE, if i is even #or(x1,x2,...) returns TRUE if any of expressions x1, x2, ... are TRUE (non-zero); FALSE otherwise (if none of x1, x2, ... are TRUE or non-zero) #ord() [see #code()] #parse() [see #split()] #passwduid() [see #pwuid()] #passwdgid() [see #pwgid()] #pclose(F) closes process F; returns TRUE if successful, ERR if unsuccessful #pid(s) #procid(s) #processid(s) returns the process id (pid) of process s #popen(F,s1,s2) opens process s1 with mode s2 (any of "r", "w", or "a"); returns TRUE if successful, ERR if unsuccessful #ppid(s) #pprocid(s) #pprocessid(s) returns the parent process id (ppid) of process s #pwuid(s) #pwuid(i) #passwduid(s) #passwduid(i) for user name s, or uid i, returns the corresponding uid (as recorded in the system passwd file) #pwgid(s) #pwgid(i) #passwdgid(s) #passwdgid(i) for user name s, or uid i, returns the corresponding gid (as recorded in the system passwd file) #random() returns a random fraction 0 <= r < 1 #random(i) returns a random integer from 1 to i #read(F) reads in the next line of input from the file handle F associated with some open file or process; the input line is assigned to the special variable $rdlin (also $rdline or $readline); returns the number of chars read, else ERR on error #rkeys(s) #rkeys(s1,s2) #reversekeys(s) #reversekeys(s1,s2) like #keys() (which see), except that the array keys are sequenced in alphabetically reverse sorted order #rindex() [see #rsearch()] #rfind() [see #rsearch()] #round(n) returns n rounded up or down to the nearest integer (where #round(2.5) --> 3, #round(-2.5) --> -3) #rsearch(s1,s2) #rindex(s1,s2) #rfind(s1,s2) return the index position (beginning at 1) of s2 within string s1, with the search starting at the end of the string, else 0 if not found #rule() #rulenumber() returns 0 when called from within a Pikt script begin section, 1 when called within the first rule, 2 when called within the second rule, and so on; if there are x rules within a Pikt script and #rule() is called from within an end section, x+1 is returned #search(s1,s2,i) #index(s1,s2,i) #find(s1,s2,i) returns the index position (beginning at 1) of s2 within string s1, with the search starting at position i; else 0 if not found #search(s1,s2) #index(s1,s2) #find(s1,s2) returns the index position (beginning at 1) of s2 within string s1, with the search starting at the beginning of the string; else 0 if not found #second() returns the current second (0-59) #second(d) returns the second portion (0-59) of the datevalue d #second(t) returns the second portion (0-59) of the timevalue t #skeys(s) #skeys(s1,s2) #sortkeys(s) #sortkeys(s1,s2) like #keys() (which see), except that the array keys are sequenced in alphabetically sorted order #split(s) #parse(s) decomposes string s into parts assigned to the variables $1, $2, ..., where whitespace (space or tab) separates the constituent parts; returns the number of parts #split(s1,s2) #parse(s1,s2) decomposes string s1 into parts assigned to the variables $1, $2, ...; if s2 is not a regular expression, any of the chars in s2 serve as field separators for the decomposition; else if s2 is a regular expression containing one or more () pairs, the matching constituent parts are assigned to $1, $2, ..., and the entire expression match is assigned to $0; returns the number of parts, or matched parts in the regular expression case #split(s1,s2,s3) #parse(s1,s2,s3) decomposes string s2 into parts assigned to the variables s1[1], s1[2], s1[3], ...; s3 is the field separator(s) for the decomposition; returns the number of parts #system(s) executes the command string s, returning its exit value #timevalue(i1,i2,i3) returns the timevalue for the time i1:i2:i3 (hh:mm:ss) #today() returns the timevalue of the current day's beginning (midnight) #true() returns TRUE (1) #trunc(n) #truncate(n) returns n as an integer with its fractional part (if any) chopped off #val(s) #value(s) returns the numerical value of s; or ERR, if s doesn't resolve to a number #weekday() returns the current weekday (1-7) #weekday(d) returns the weekday (1-7) of the datevalue d #write(F,s) writes string s to the file handle F associated with some open file or process; returns the number of chars written; else ERR on error #year() returns the current year (as a four-digit number) #year(d) returns the year (as a four-digit number) of the datevalue d #yearday() returns the current yearday (1-366) #yearday(d) returns the yearday (1-366) of the datevalue d #yearweek() returns the current yearweek (1-53) #yearweek(d) returns the yearweek (1-53) of the datevalue d
prev page | 1st page | next page |