Global Local: Difference between revisions

From Macros Wiki
Jump to navigationJump to search
ConnerO (talk | contribs)
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...
 
ConnerO (talk | contribs)
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<br>
{|
! the passing of arguments to sub macros<br>
|!&nbsp;&nbsp;&nbsp; show the use of local and global variables and
|-
|!&nbsp;&nbsp;&nbsp; the passing of arguments to sub macros
|-
|&nbsp;
|-
|!&nbsp;&nbsp;&nbsp; it also creates a file with the results and
|-
|!&nbsp;&nbsp;&nbsp; then displays them when it has finished
|-
|&nbsp;
|-
|a1$ = "This macro shows the use of local and global variables"
|-
|a2$ = "and the passing of arguments to other macros"
|-
|&nbsp;
|-
|message "%a1$\n%a2$"
|-
|&nbsp;
|-
|global inum, iunit, a1$, a2$, a3$, a4$
|-
|local  i, result
|-
|&nbsp;
|-
|!&nbsp;&nbsp;&nbsp; open a file for the results
|-
|!&nbsp;&nbsp;&nbsp; ---------------------------
|-
|&nbsp;
|-
|errmac$ = "[[GlobalLocal_FileError.mac]]"
|-
|&nbsp;
|-
|file_open 1, "temp.txt", write
|-
|if FileError <> 0 then call errmac$, "Error opening file for read", 0
|-
|&nbsp;
|-
|inum = 4
|-
|&nbsp;
|-
|for i = 2 to inum
|}
:a1$ = "Call Sub Macro, i = "
:a2$ = format$(i,"#0")
<br>
<br>
! it also creates a file with the results and<br>
:file_write_text 1, a1$+a2$
! then displays them when it has finished<br>
:if FileError <> 0 then call errmac$, "Error writing file", 1
<br>
<br>
a1$ = "This macro shows the use of local and global variables"<br>
:call "[[GlobalLocal_Sub.mac]]", i, result
a2$ = "and the passing of arguments to other macros"<br>
:if FileError <> 0 then call errmac$, "Error writing file", 1
<br>
<br>
message "%a1$\n%a2$"<br>
:a1$ = "Back in main Macro, i = "
:a2$ = format$(i,"#0")
:a3$ = "  result = "
:a4$ = format$(result, "###0")
<br>
<br>
global inum, iunit, a1$, a2$, a3$, a4$<br>
:file_write_text 1, a1$+a2$+a3$+a4$
local  i, result<br>
:if FileError <> 0 then call errmac$, "Error writing file", 1
<br>
next i
! open a file for the results<br>
{|
! ---------------------------
|&nbsp;
<br>
|-
errmac$ = "[[GlobalLocal_FileError.mac]]"
|file_close 1
<br>
|-
file_open 1, "temp.txt", write<br>
|if FileError <> 0 then call errmac$, "Error closing file", 0
if FileError <> 0 then call errmac$, "Error opening file for read", 0<br>
|-
<br>
|&nbsp;
inum = 4<br>
|-
<br>
|file_is_open = 0
for i = 2 to inum<br>
|-
:a1$ = "Call Sub Macro, i = "<br>
|&nbsp;
:a2$ = format$(i,"#0")<br>
|-
  <br>
|!&nbsp;&nbsp;&nbsp; reopen the file and read the contents
:file_write_text 1, a1$+a2$<br>
|-
:if FileError <> 0 then call errmac$, "Error writing file", 1<br>
|!&nbsp;&nbsp;&nbsp; -------------------------------------
  <br>
|-
:call "GlobalLocal_Sub.mac", i, result<br>
|&nbsp;
:if FileError <> 0 then call errmac$, "Error writing file", 1<br>
|-
<br>
|file_open 1, "temp.txt", read
:a1$ = "Back in main Macro, i = "<br>
|-
:a2$ = format$(i,"#0")<br>
|if FileError <> 0 then call errmac$, "Error re-opening file", 0
:a3$ = "  result = "<br>
|-
:a4$ = format$(result, "###0")<br>
|&nbsp;
<br>
|-
:file_write_text 1, a1$+a2$+a3$+a4$<br>
|file_is_open = 1
:if FileError <> 0 then call errmac$, "Error writing file", 1<br>
|-
next i<br>
|&nbsp;
<br>
|-
file_close 1<br>
|a1$ = ""
if FileError <> 0 then call errmac$, "Error closing file", 0<br>
|-
<br>
|inum = 0
file_is_open = 0<br>
|-
<br>
|&nbsp;
! reopen the file and read the contents<br>
|-
! -------------------------------------<br>
|<nowiki>:</nowiki>read
<br>
|-
file_open 1, "temp.txt", read<br>
|file_read_text 1, a2$
if FileError <> 0 then call errmac$, "Error re-opening file", 0<br>
|-
<br>
|if FileError <> 0 then goto close  !&nbsp;&nbsp;&nbsp; end of file
file_is_open = 1<br>
|-
<br>
|&nbsp;
a1$ = ""<br>
|-
inum = 0<br>
|a1$ = a1$+a2$+"\n"
<br>
|-
<nowiki>:</nowiki>read<br>
|&nbsp;
file_read_text 1, a2$<br>
|-
if FileError <> 0 then goto close  ! end of file<br>
|inum = inum + 1
<br>
|-
a1$ = a1$+a2$+"\n"<br>
|if inum < 20 then goto read  !&nbsp;&nbsp;&nbsp; read a max of 20 lines
<br>
|-
inum = inum + 1<br>
|&nbsp;
if inum < 20 then goto read  ! read a max of 20 lines<br>
|-
<br>
|<nowiki>:</nowiki>close
<nowiki>:</nowiki>close<br>
|-
file_close 1<br>
|file_close 1
if FileError <> 0 then call errmac$, "Error closing file", 0<br>
|-
<br>
|if FileError <> 0 then call errmac$, "Error closing file", 0
file_is_open = 0<br>
|-
<br>
|&nbsp;
! delete the temp file<br>
|-
! --------------------<br>
|file_is_open = 0
<br>
|-
file_delete "temp.txt"<br>
|&nbsp;
if FileError <> 0 then call errmac$, "Error deleting file", 0<br>
|-
<br>
|!&nbsp;&nbsp;&nbsp; delete the temp file
message a1$, "Results"<br>
|-
|!&nbsp;&nbsp;&nbsp; --------------------
|-
|&nbsp;
|-
|file_delete "temp.txt"
|-
|if FileError <> 0 then call errmac$, "Error deleting file", 0
|-
|&nbsp;
|-
|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"