Prev: Fundamental Modules
Module : Builtin Functions and Constants
The functions listed here are the standard
m> functions available without importing any module. They can be called without a module or alias prefix, or with an empty prefix (a dot).
print date();
print .date()
|
Both statements have the same effect.
.append
• function append(array, element, ...) → null
Append one or more elements to the the end of array. The length of array is increased by the number of elements appended.
arr=[];
append(arr, 17, "x");
print arr
→ [17,x]
|
.cd
• function cd() → String
• function cd(newpath) → String
Gets and sets the current (default) directory. This is the directory all file or directory operations relate to. See also section * ().
Without an argument, cd returns the current directory without modifying it. With a single argument, it changes the current directory to newpath and returns the previously set current directory. newpath can be absolute, or relative to the current directory.
cd("c:\\");
print cd("system")
→ c:\
print cd("apps")
→ c:\system\
print cd()
→ c:\system\apps\
|
See also: files.mkdir, files.rmdir
.char
• function char(array) → String
• function char(code) → String
Converts the array of numbers array to a string, interpreting each number as a UNICODE® BMP character code, or converts the number code to a single character string.
The codes must be numbers between 0 and 216-1 = 65535.
print char([72,101,108,108,111])
→ Hello
|
See also: .code
.cls
• function cls() → null
Clears the screen, deleting all console output produced so far.
.code
• function code(text) → Array
• function code(text, pos) → Number
With a single argument, converts text to an array containing the UNICODE® number for each character. With two arguments, returns the code for the character at position pos of text.
print code("Hello")
→ [72,101,108,108,111]
print code("Hello", 1)
→ 101
|
See also: .char.
.collate
• function collate(s1, s2) → Number
Compare the two strings s1 and s2, correctly ordering accents and umlauts depending on the current locale. Returns a negative number if s1 < s2, zero if s1 = s2, a positive number if s1 > s2.
// Flüge comes before Flugzeug in lexical ordering
print collate("Flüge", "Flugzeug")
→ -1
// simple raw ordering produces the wrong result
print "Flüge" < "Flugzeug"
→ false
|
See also: constant array.collate.
.date
• function date() → String
Get the current local date and time in the format YYYY-MM-DD hh:mm:ss. See also module time.
print date()
→ 2005-02-21 12:18:55
|
.equal
• function equal(a, b) → Boolean
Compares two values a and b for equality and returns true if they are equal, false if they are not equal. Unlike the m> language = operator, this function compares arrays elementwise: two arrays are identical if they have the same length and all their elements are equal.
a=[1, 2, [3, 4]]
b=a;
print a=b, equal(a, b)
→ true true
b=[1, 2, [3, 4]];
print a=b, equal(a, b)
→ false true
|
Note that the function will crash m> if you pass two identical recursive arrays for which equality or inequality cannot be determined.
a=[0]; a[0]=a;
b=[0]; b[0]=b;
equal(a, b) // this will crash m
|
.delete
• function delete(text, start) → String
• function delete(text, start, length) → String
Deletes the substring from text from position start, either to the end of text, or the next length characters. The first character has position 0.
Throws ExcStringPosOutOfRange if not 0 <= start <= len(text), or if not 0 <= length <= len(text) - start.
print delete("Hello world!", 6)
→ Hello
print delete("Hello world!", 3, 5)
→ Helrld!
|
See also: .substr
.hexnum
• function hexnum(text) → Number
Converts the string text representing a hexadecimal integer value into the value. The value can be signed. Uppercase and lowercase digits are allowed, and leading and trailing blanks are ignored.
print hexnum("1fff");
→ 8191
print hexnum(" -ABACADA ");
→ -180013786
|
See also: .num
.hexstr
• function hexstr(number, width=0) → String
Formats number into an integer hexadecimal value. If necessary, zeros are added before the string until its length is at least width.
print hexstr(8191)
→ 1fff
print hexstr(-180013786, 12)
→ -0000abacada
|
See also: .str, io.format
.index
• function index(text, pattern, start=0, folded=false) → Number
Searches the string text for the first occurence of the string pattern at or after start and returns the position. If pattern does not occur, -1 is returned. If folded=true, the comparison between text and pattern ignores case.
Throws ExcStringPosOutOfRange if not 0 <= start <= len(text).
print index("To be, or not to be", "to be")
→ 14
print index("To be, or not to be", "to be", 0, true)
→ 0
print index("To be, or not to be", "to be", 1, true)
→ 14
print index("To be, or not to be", "to be or not")
→ -1
|
See also: .rindex
.isarray
• function isarray(expression) → Boolean
Returns true if expression is an array, false if it is any other type.
print isarray([])
→ true
print isarray("String")
→ false
|
.isboolean
• function isboolean(expression) → Boolean
Returns true if expression is a boolean (i.e. true or false), false if it is any other type.
print isboolean(4 > 5)
→ true
print isboolean(4+5)
→ false
|
.isfunction
• function isfunction(expression) → Boolean
Returns true if expression is a function reference, false if it is any other type.
print isfunction(&cd)
→ true
print isfunction(cd())
→ false
|
.isinst
• function isinst(expression) → Boolean
Returns true if expression is a class instance, false if it is any other type.
isinst(x) is equivalent to x is .Instance and x # null.
print isinst(.Instance())
→ true
print isinst(null)
→ false
|
.isinstfunc
• function isinstfunc(expression) → Boolean
Returns true if expression is an instance function reference, false if it is any other type.
x:.Instance=.Instance()
print isinstfunc(x.&init)
→ true
print isinstfunc(&cd)
→ false
|
.isnative
• function isnative(expression) → Boolean
Returns true if expression is a native object, false if it is any other type.
print isnative(io.create("sample.xml"))
→ true
print isnative([])
→ false
|
.isnum
• function isnum(expression) → Boolean
Returns true if expression is a number, false if it is any other type.
print isnum(13.26)
→ true
print isnum("13.26")
→ false
print isnum(num("13.26"))
→ true
|
.isstr
• function isstr(expression) → Boolean
Returns true if expression is a string, false if it is any other type.
print isstr("Hello")
→ true
print isstr(null)
→ false
|
.keys
• function keys(array) → Array
Returns an array of length len(array), with each element set to the string key of the element at this position in array, or set to null if the element at this position has no key.
a=["one":1, "two":2, 3, "four":4, 5];
print keys(a)
→ ["one", "two", null, "four", null]
|
.len
• function len(array) → Number
• function len(text) → Number
Returns the length (number of elements) of the array array, or the length (number of characters) of the string text.
print len("Hello")
→ 5
print len("")
→ 0
print len([7, 8, 9])
→ 3
print len([])
→ 0
|
.lower
• function lower(text) → String
Returns a copy of text, with all uppercase characters converted to their lowercase equivalent.
print lower("Hello")
→ hello
print lower("WATCH OUT!")
→ watch out!
|
.num
• function num(text) → Number
Converts the string text representing a numeric value into the value. The syntax for the number is the same as for numeric literals (see * (Reference)). Leading and trailing blanks are ignored.
print 21+num('21')
→ 42
print num(" -15.8e4 ")
→ -158000
|
.replace
• function replace(text, old, new) → String
Replaces all occurences of old in text by new, and returns the string with replacements made. old and new need not have the same length.
print replace("Hello world!", "l", "ll")
→ Hellllo worlld!"
print replace("Hello world!", "l", "")
→ Heo word!"
|
.rindex
• function rindex(text, pattern, start=len(text)-1, folded=false) → Number
Searches the string text for the last occurence of the string text at or before start and returns the position. If pattern does not occur, -1 is returned. If folded=true, the comparison between text and pattern ignores case.
Throws ExcStringPosOutOfRange if not -1 <= start < len(text).
print rindex("To be, or not to be", "To be")
→ 0
print rindex("To be, or not to be", "To be", 18, true)
→ 14
print rindex("To be, or not to be", "To be", 13, true)
→ 0
print rindex("To be, or not to be", "to be or not")
→ -1
|
See also: .index
.sleep
• function sleep(milliseconds) → null
Pauses execution for (at least) the number of milliseconds (1/1000 of a second) before returning. If milliseconds is negative or zero, execution continues immediately, but other m processes immediately get a chance to run, before they are preempted by the scheduler.
Throws ExcValueOutOfRange if milliseconds exceeds 2147483 (35 minutes and 47.483 seconds).
sleep(500) // wait for 1/2 s
|
.split
• function split(text) → Array
• function split(text, separator) → Array
With one argument, splits text into words separated by any amount of white space[1].
With two arguments, splits text into substrings at each occurrence of separator. separator can be of any positive length.
Throws ErrArgument if separator is the empty string.
print split(" To be, or not to be?")
→ [To,be,,or,not,to,be?]
print split("Line 1<BR><BR><B>Line 3</B><BR>", "<BR>")
→ [Line 1,,<B>Line 3</B>,]
|
.str
• function str(expression, width=0) → String
• function str(number, width, decimals) → String
Converts an expression or a number to string:
- The first form converts an expression to a string, using the same rules as the print statement (see * (Reference)):
- If width >= 0, spaces are added before the string until its length is at least width. The result is thus right adjusted.
print str(1 < 3, 8)
→ true
|
- If width < 0, spaces are added after the string until its length is at least -width. The result is thus left adjusted.
print str("hello", -8) + "world"
→ hello world
|
- The second form formats number into a fixed or floating point representation, depending on decimals:
- If decimals = 0, the number is represented without decimal positions and without decimal point, as if it were an integer:
print str(10000/7, 6, 0)
→ 1429
|
- If decimals > 0, the number is represented with decimal point and the given number of decimal positions:
print str(10000/7, 0, 3)
→ 1428.571
|
- If decimals < 0, the number is represented with floating point and the given number of significant digits:
print str(10000/7, 10, -3)
→ 1.43E+03
print str(10000/7, 0, -1)
→ 1E+03
|
See also:
.hexstr,
io.format
.substr
• function substr(text, start) → String
• function substr(text, start, length) → String
Extracts a substring from text from position start, either to the end of text, or the next length characters. The first character has position 0.
Throws ExcStringPosOutOfRange if not 0 <= start <= len(text), or if not 0 <= length <= len(text) - start.
print substr("Hello world!", 6)
→ world!
print substr("Hello world!", 3, 5)
→ lo wo
|
.trim
• function trim(text) → String
Returns a copy of text, with leading and trailing blanks removed.
print trim("Hello")
→ Hello
print trim(" world! ")
→ world!
|
.upper
• function upper(text) → String
Returns a copy of text, with all lowercase characters converted to their uppercase equivalent.
print upper("Hello")
→ HELLO
print upper("watch out!")
→ WATCH OUT!
|
Constants
• const version = 3.07
The current version of m. Of course, for a different version this number will be different from 3.07.
Next: Module array: Array Functions© 2004-2011 airbit AG, CH-8008 Zürich
Document AB-M-LIB-888