functions examples are added

This commit is contained in:
programarivm 2019-09-29 11:01:06 +01:00
parent ffb4e2d3e5
commit 7d368b805b
7 changed files with 64 additions and 73 deletions

View File

1
02-functions/Ascii.m Normal file
View File

@ -0,0 +1 @@
w "The ASCII code of ""a"" is ",$ascii("a"),".",!

3
02-functions/Char.m Normal file
View File

@ -0,0 +1,3 @@
w "Alphabet: ",!
f i=97:1:122 w $char(i)
w !

26
02-functions/Data.m Normal file
View File

@ -0,0 +1,26 @@
s gtree(1)="Oliver"
s gtree(1,1)="James"
s gtree(1,1,1)="Harry"
s gtree(1,1,2)="Emily"
s gtree(1,2)="Amelia"
s gtree(1,2,1)="Thomas"
s gtree(1,2,2)="Jessica"
; ...
s gtree(1,1,1,1,1)="Robert"
w "Genealogical tree:",!
zwrite gtree
; obtain data about a particular node
; 0, the node does not exist
; 1, the node exists, has no descendant
; 10, the node exists, has no data, has descendant
; 11, the node exists, has data, has descendant
w !,"Data about a few nodes: ",!
w "$d(gtree(""foo"")) is ",$d(gtree("foo")),!
w "$d(gtree(1,1,1,1,1)) is ",$d(gtree(1,1,1,1,1)),!
w "$d(gtree(1,1,1,1)) is ",$d(gtree(1,1,1,1)),!
w "$d(gtree(1)) is ",$d(gtree(1)),!

3
02-functions/Extract.m Normal file
View File

@ -0,0 +1,3 @@
s string="Hello world!"
w "Original string: ",string,!
w "Substring: ",$extract(string,1,5),!

View File

@ -1,57 +0,0 @@
; global array as a tree
w "Genealogical tree:",!
; create global records
s ^gtree(0)="Oliver"
s ^gtree(0,0)="Jack"
s ^gtree(0,1)="Amelia"
s ^gtree(0,0,0)="Harry"
s ^gtree(0,0,1)="Emily"
s ^gtree(0,1,0)="Thomas"
s ^gtree(0,1,1)="Jessica"
s ^gtree(0,0,0,0)="Michael"
s ^gtree(0,0,0,1)="Olivia"
s ^gtree(0,0,1,0)="Robert"
s ^gtree(0,0,1,1)="Mary"
s ^gtree(0,1,0,0)="George"
s ^gtree(0,1,0,1)="Susan"
s ^gtree(0,1,1,0)="Charles"
s ^gtree(0,1,1,1)="Elizabeth"
; ...
s ^gtree(0,0,0,0,0,0)="Thomas"
; TODO: traverse the tree and display information
; ...
; retrieve a global record
w "^gtree(0,1,1) is ",^gtree(0,1,1),!
; obtain data about a particular node
; 0, the node does not exist
; 1, the node exists, has no descendant
; 10, the node exists, has no data, has descendant
; 11, the node exists, has data, has descendant
w "$data(^gtree(0,2)) is ",$data(^gtree(0,3)),!
w "$data(^gtree(0,1,1,1)) is ",$data(^gtree(0,1,1,1)),!
w "$data(^gtree(0,0,0,0,0)) is ",$data(^gtree(0,0,0,0,0)),!
w "$data(^gtree(0)) is ",$data(^gtree(0)),!
; remove the global array
; 0, the node does not exist
; 0, the node does not exist
; 0, the node does not exist
; 0, the node does not exist
w !,"Removing tree...",!
k ^gtree
w "$data(^gtree(0,2)) is ",$data(^gtree(0,3)),!
w "$data(^gtree(0,1,1,1)) is ",$data(^gtree(0,1,1,1)),!
w "$data(^gtree(0,0,0,0,0)) is ",$data(^gtree(0,0,0,0,0)),!
w "$data(^gtree(0)) is ",$data(^gtree(0)),!

View File

@ -157,7 +157,37 @@ Then run the example:
## 2. Functions ## 2. Functions
> TODO. ### [`Ascii.m`](https://github.com/programarivm/mumps-examples/blob/master/02-functions/Ascii.m)
The ASCII code of "a" is 97.
### [`Char.m`](https://github.com/programarivm/mumps-examples/blob/master/02-functions/Char.m)
Alphabet:
abcdefghijklmnopqrstuvwxyz
### [`Data.m`](https://github.com/programarivm/mumps-examples/blob/master/02-functions/Data.m)
Genealogical tree:
gtree(1)="Oliver"
gtree(1,1)="James"
gtree(1,1,1)="Harry"
gtree(1,1,1,1,1)="Robert"
gtree(1,1,2)="Emily"
gtree(1,2)="Amelia"
gtree(1,2,1)="Thomas"
gtree(1,2,2)="Jessica"
Data about a few nodes:
$d(gtree("foo")) is 0
$d(gtree(1,1,1,1,1)) is 1
$d(gtree(1,1,1,1)) is 10
$d(gtree(1)) is 11
### [`Extract.m`](https://github.com/programarivm/mumps-examples/blob/master/02-functions/Extract.m)
Original string: Hello world!
Substring: Hello
## 3. Utility Routines ## 3. Utility Routines
@ -196,21 +226,6 @@ Then run the example:
## 6. Databases ## 6. Databases
### [`GenealogicalTree.m`](https://github.com/programarivm/mumps-examples/blob/master/06-databases/GenealogicalTree.m)
Genealogical tree:
^gtree(0,1,1) is Jessica
$data(^gtree(0,2)) is 0
$data(^gtree(0,1,1,1)) is 1
$data(^gtree(0,0,0,0,0)) is 10
$data(^gtree(0)) is 11
Removing tree...
$data(^gtree(0,2)) is 0
$data(^gtree(0,1,1,1)) is 0
$data(^gtree(0,0,0,0,0)) is 0
$data(^gtree(0)) is 0
### [`basic-sql-crud/Main.m`](https://github.com/programarivm/mumps-examples/blob/master/06-databases/basic-sql-crud/Main.m) ### [`basic-sql-crud/Main.m`](https://github.com/programarivm/mumps-examples/blob/master/06-databases/basic-sql-crud/Main.m)
User successfully created! User successfully created!