1 *MPSL Quick Reference Sheet
5 v = 10000; Integer assignation
6 v = 3.1416; Real numbers
7 v = 'string1'; Verbatim strings
8 w = "string2\n"; Strings with escape codes
9 l = size(v); Size of v (7)
14 a = [ 100, "hi!", Array assignation (showing
15 3.1416, "yo!" ]; elements with assorted types)
16 w = [ 1, [], a ]; Array elements can also be
17 arrays, hashes or anything
18 (as non-scalar values are
19 always passed by reference)
20 v = a[0]; First element of a (100)
21 a[0] = 200; Assignment of first element of a
22 l = size(a); Number of elements in a (4)
23 x = a; x holds the same array as a
24 x[0] = 6; a[0] is also 6
25 y = clone(a); y holds a clone of a
26 y[0] = 9; a[0] is NOT 9
27 v = a[-1]; Last element of a ("yo!")
28 w = [1 .. 100]; Range (array of 100 elements)
29 push(a, 6543); Add element to the end of a
30 l = size(a); It's now 5
31 v = pop(a); Deletes last element
33 l = size(a); Back to 4
34 ins(a, 'heh', 1); Inserts 'heh' at offset 1, all
35 existing elements moved up
36 l = size(a); It's again 5
37 adel(a, 1); Deletes element at offset 1,
40 l = size(a); It's again 4
45 h = { Hash assignation (showing
46 'key1' => "val1", elements with assorted types).
47 'k2' => 1000, Hash elements can be anything
50 v = h['key1']; Value for 'key1' ('val1')
51 h['k4'] = 3000; Assignment of key 'k4'
53 p = hsize(h); Number of pairs in h (5)
54 i = h; i holds the same hash as h
55 i['key1'] = 4321; h['key1'] is also 4321
56 j = clone(h); j holds a clone of h
57 j['key1'] = 8765; h['key1'] is NOT 8765
58 v = h.KEY3.1; Abridged mode
59 (same as v = h['KEY3'][1])
60 h.KEY3.1 = 10; Abridged mode assignation
61 a = keys(h); Keys of h
62 (['key1','k2','KEY3','k4'])
66 sub greeting { Subroutine definition
69 greeting(); Subroutine call
71 sub sum(a, b) { Subroutine definition, with
72 return(a + b); arguments
74 v = sub(10, 20); Subroutine call with
75 arguments (sets v to 30)
76 s = sub { Anonymous subroutine
77 return(3); }; (assigned to s)
78 s = sub(a) { Anonymous subroutine,
79 print(a); }; with arguments
80 s = greeting; Without parenthesis,
87 if (expr) { If expr is true, execute
88 statement list 1; list 1 (braces can be omitted
89 } if list 1 is one statement);
90 else { otherwise, execute list 2
94 while (expr) { While expr is true, execute
95 statement list; list (braces can be omitted
96 } if list is one statement)
98 foreach (v, a) { Iterate the array a, assigning
99 statement list; each element to v and
102 break; Exits loop in while and foreach
106 i = cmp(str1, str2); Compares two strings, returns
108 del = splice(str1, Inserts str2 inside str1 at
109 str2, offset, del); offset, deleting del characters
110 (returns the deleted string)
111 ary = split(str, sep); Splits str using sep as
113 ary = split(str); Splits str in characters
114 str = join(sep, ary); Joins ary into str, using
115 sep as separator (inverse of
117 str = sprintf(fmtstr, Formats a string
119 char = chr(number); Returns the character
120 represented by number
121 number = ord(str); Returns the numeric value of
122 the first character in str
123 ary = sscanf(fmt, str); Extracts values from a
124 string using a format
128 push(ary, val); Adds val to the end of ary,
129 incrementing its size
130 v = pop(ary); Deletes the last element of ary
132 v = shift(ary); Deletes the first element of
134 ins(ary, val, offset); Inserts val into ary at offset
135 incrementing its size
136 adel(ary, offset); Deletes the element
137 at offset from ary, shrinking
138 i = seek(ary, str); Seeks ary for an exact match of
139 str and returns its offset, or
141 s = sort(ary); Sorts ary alphabetically
142 s = sort(ary, func); Sorts ary using a special
143 sub func(a,b) { ... } sorting function, that accepts
144 two arguments to compare
145 ary2 = map(func, ary); Executes func on each element
146 sub func(a) { ... } of ary and returns an array
147 with the return values
148 ary2 = map(hash, ary); Uses the elements of ary as
149 keys of hash and returns an
150 array with the values
151 ary2 = grep(rx, ary); Matches the regex on all
152 elements in ary and returns
153 all the matching ones
154 ary2 = grep(func, ary); Executes func on all elements
155 sub func(a) { ... } of ary and returns a new array
156 with all elements on which
158 expand(ary, offs, n); Opens room in ary of n
160 collapse(ary, offs, n); Deletes n elements from ary
165 n = hsize(hash); Returns the number of pairs
167 b = exists(hash, key); True if key exists in hash
168 k = keys(hash); Returns the keys of hash as
170 v = hdel(hash, key); Deletes key from hash,
171 returning the old value
173 *Input/Output functions
175 fd = open(fname, mode); Opens fname (mode is "r",
176 "w","r+","w+"...), returns
178 close(fd); Closes a file
179 l = read(fd); Reads a line from a file
180 write(fd, l); Writes a string on a file
181 c = getchar(fd); Reads a char from a file
182 putchar(fd, c); Puts a char on a file
183 unlink(fname); Deletes a file
184 s = stat(fname); Returns information from file
185 (dev, inode, mode, nlink,
186 uid, gid, rdev, size, atime,
188 chmod(fname, mode); Changes the mode of a file
189 chown(fname, owner); Changes the owner of a file
190 ary = glob(pattern); File globbing (returns an
192 encoding(charset); Sets the charset encoding
193 for reading and writing files
194 fd = popen(cmd, mode); Opens a pipe (mode is "r","w",
195 "r+"...), can be read/write
196 pclose(fd); Closes a pipe
197 load(mpslfile); Loads and executes MPSL code
198 chdir(dir); Change current directory
200 *Regular expression functions
202 s = regex(reg, str); Matches the reg regexp into
203 str, returns the matching
205 s = regex(r, str, off); The same, but starting at off
207 ary = regex(); Returns the last matching
210 ary2 = regex(ary, str); Matches an array of regular
211 expressions, returns as many
212 matches as elements in ary
213 res = sregex(reg, str, Matches the reg regexp into
214 str2); str and changes it with str2
215 res = sregex(reg, str, Matches the reg regexp into
216 hash); str and changes it with its
218 res = sregex(reg, str, Matches the reg regexp into
219 func); str and changes with the
220 sub func(m) { ... }; return value of func using
221 the matched string as argument
223 *Miscellaneous functions
225 s = size(str); Returns the length of str
226 s = size(array); Returns the number of elements
227 s = size(hash); Returns the number of buckets
228 val2 = clone(val1); Clones val1
229 dump(val); Dumps val visually on stdout
230 b = is_array(val); Tests if val is an array
231 b = is_hash(val); Tests if val is a hash
232 b = is_exec(val); Tests if val is executable
233 error(str); Generates an error
234 r = eval(str); Compiles and executes str
236 r = eval(code); Executes code
237 print(str, str...); Prints values to stdout
238 tstr = gettext(str); Translates str
239 gettext_domain(dom, Sets the gettext domain
240 directory); and directory
241 getenv(envvar); Returns the value of
242 an environment variable
243 uc(str); Returns str to uppercase
244 lc(str); Returns str to lowercase
245 time(); Returns the time in seconds
246 x = compile(str); Compiles a str of MPSL code
248 *String escapes for double quotes
256 \\x{NNNN} Unicode character (N: hex)
258 *Regular expression flags
262 g Global match or change
263 l Match last occurrence