Global Local: Difference between revisions
From Macros Wiki
Jump to navigationJump to search
New page: ! show the use of local and global variables and<br> ! the passing of arguments to sub macros<br> <br> ! it also creates a file with the results and<br> ! then displays them when it has fi... |
No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
! show the use of local and global variables and | {| | ||
! the passing of arguments to sub macros< | |! show the use of local and global variables and | ||
|- | |||
|! the passing of arguments to sub macros | |||
|- | |||
| | |||
|- | |||
|! it also creates a file with the results and | |||
|- | |||
|! then displays them when it has finished | |||
|- | |||
| | |||
|- | |||
|a1$ = "This macro shows the use of local and global variables" | |||
|- | |||
|a2$ = "and the passing of arguments to other macros" | |||
|- | |||
| | |||
|- | |||
|message "%a1$\n%a2$" | |||
|- | |||
| | |||
|- | |||
|global inum, iunit, a1$, a2$, a3$, a4$ | |||
|- | |||
|local i, result | |||
|- | |||
| | |||
|- | |||
|! open a file for the results | |||
|- | |||
|! --------------------------- | |||
|- | |||
| | |||
|- | |||
|errmac$ = "[[GlobalLocal_FileError.mac]]" | |||
|- | |||
| | |||
|- | |||
|file_open 1, "temp.txt", write | |||
|- | |||
|if FileError <> 0 then call errmac$, "Error opening file for read", 0 | |||
|- | |||
| | |||
|- | |||
|inum = 4 | |||
|- | |||
| | |||
|- | |||
|for i = 2 to inum | |||
|} | |||
:a1$ = "Call Sub Macro, i = " | |||
:a2$ = format$(i,"#0") | |||
<br> | <br> | ||
:file_write_text 1, a1$+a2$ | |||
:if FileError <> 0 then call errmac$, "Error writing file", 1 | |||
<br> | <br> | ||
:call "[[GlobalLocal_Sub.mac]]", i, result | |||
:if FileError <> 0 then call errmac$, "Error writing file", 1 | |||
<br> | <br> | ||
:a1$ = "Back in main Macro, i = " | |||
:a2$ = format$(i,"#0") | |||
:a3$ = " result = " | |||
:a4$ = format$(result, "###0") | |||
<br> | <br> | ||
:file_write_text 1, a1$+a2$+a3$+a4$ | |||
:if FileError <> 0 then call errmac$, "Error writing file", 1 | |||
next i | |||
{| | |||
| | |||
|- | |||
|file_close 1 | |||
|- | |||
|if FileError <> 0 then call errmac$, "Error closing file", 0 | |||
|- | |||
| | |||
|- | |||
|file_is_open = 0 | |||
|- | |||
| | |||
|- | |||
|! reopen the file and read the contents | |||
|- | |||
|! ------------------------------------- | |||
|- | |||
| | |||
|- | |||
|file_open 1, "temp.txt", read | |||
|- | |||
|if FileError <> 0 then call errmac$, "Error re-opening file", 0 | |||
|- | |||
| | |||
|- | |||
:file_write_text 1, a1$+a2$+a3$+a4$ | |file_is_open = 1 | ||
:if FileError <> 0 then call errmac$, "Error writing file", 1 | |- | ||
next i | | | ||
|- | |||
file_close 1 | |a1$ = "" | ||
if FileError <> 0 then call errmac$, "Error closing file", 0 | |- | ||
|inum = 0 | |||
file_is_open = 0 | |- | ||
| | |||
! reopen the file and read the contents | |- | ||
! ------------------------------------- | |<nowiki>:</nowiki>read | ||
|- | |||
file_open 1, "temp.txt", read | |file_read_text 1, a2$ | ||
if FileError <> 0 then call errmac$, "Error re-opening file", 0 | |- | ||
|if FileError <> 0 then goto close ! end of file | |||
file_is_open = 1 | |- | ||
| | |||
a1$ = "" | |- | ||
inum = 0 | |a1$ = a1$+a2$+"\n" | ||
|- | |||
<nowiki>:</nowiki>read | | | ||
file_read_text 1, a2$ | |- | ||
if FileError <> 0 then goto close ! end of file | |inum = inum + 1 | ||
|- | |||
a1$ = a1$+a2$+"\n" | |if inum < 20 then goto read ! read a max of 20 lines | ||
|- | |||
inum = inum + 1 | | | ||
if inum < 20 then goto read ! read a max of 20 lines | |- | ||
|<nowiki>:</nowiki>close | |||
<nowiki>:</nowiki>close | |- | ||
file_close 1 | |file_close 1 | ||
if FileError <> 0 then call errmac$, "Error closing file", 0 | |- | ||
|if FileError <> 0 then call errmac$, "Error closing file", 0 | |||
file_is_open = 0 | |- | ||
| | |||
! delete the temp file | |- | ||
! -------------------- | |file_is_open = 0 | ||
|- | |||
file_delete "temp.txt" | | | ||
if FileError <> 0 then call errmac$, "Error deleting file", 0 | |- | ||
|! delete the temp file | |||
message a1$, "Results" | |- | ||
|! -------------------- | |||
|- | |||
| | |||
|- | |||
|file_delete "temp.txt" | |||
|- | |||
|if FileError <> 0 then call errmac$, "Error deleting file", 0 | |||
|- | |||
| | |||
|- | |||
|message a1$, "Results" | |||
|} | |||
Latest revision as of 15:25, 29 July 2010
| ! show the use of local and global variables and |
| ! the passing of arguments to sub macros |
| ! it also creates a file with the results and |
| ! then displays them when it has finished |
| a1$ = "This macro shows the use of local and global variables" |
| a2$ = "and the passing of arguments to other macros" |
| message "%a1$\n%a2$" |
| global inum, iunit, a1$, a2$, a3$, a4$ |
| local i, result |
| ! open a file for the results |
| ! --------------------------- |
| errmac$ = "GlobalLocal_FileError.mac" |
| file_open 1, "temp.txt", write |
| if FileError <> 0 then call errmac$, "Error opening file for read", 0 |
| inum = 4 |
| for i = 2 to inum |
- a1$ = "Call Sub Macro, i = "
- a2$ = format$(i,"#0")
- file_write_text 1, a1$+a2$
- if FileError <> 0 then call errmac$, "Error writing file", 1
- call "GlobalLocal_Sub.mac", i, result
- if FileError <> 0 then call errmac$, "Error writing file", 1
- a1$ = "Back in main Macro, i = "
- a2$ = format$(i,"#0")
- a3$ = " result = "
- a4$ = format$(result, "###0")
- file_write_text 1, a1$+a2$+a3$+a4$
- if FileError <> 0 then call errmac$, "Error writing file", 1
next i
| file_close 1 |
| if FileError <> 0 then call errmac$, "Error closing file", 0 |
| file_is_open = 0 |
| ! reopen the file and read the contents |
| ! ------------------------------------- |
| file_open 1, "temp.txt", read |
| if FileError <> 0 then call errmac$, "Error re-opening file", 0 |
| file_is_open = 1 |
| a1$ = "" |
| inum = 0 |
| :read |
| file_read_text 1, a2$ |
| if FileError <> 0 then goto close ! end of file |
| a1$ = a1$+a2$+"\n" |
| inum = inum + 1 |
| if inum < 20 then goto read ! read a max of 20 lines |
| :close |
| file_close 1 |
| if FileError <> 0 then call errmac$, "Error closing file", 0 |
| file_is_open = 0 |
| ! delete the temp file |
| ! -------------------- |
| file_delete "temp.txt" |
| if FileError <> 0 then call errmac$, "Error deleting file", 0 |
| message a1$, "Results" |