http://beebwiki.mdfs.net/index.php?title=Forcing_a_variable_to_exist&feed=atom&action=historyForcing a variable to exist - Revision history2024-03-28T20:37:51ZRevision history for this page on the wikiMediaWiki 1.30.0-rc.0http://beebwiki.mdfs.net/index.php?title=Forcing_a_variable_to_exist&diff=2826&oldid=prevJgharston at 20:23, 3 July 20162016-07-03T20:23:07Z<p></p>
<table class="diff diff-contentalign-left" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr style="vertical-align: top;" lang="en">
<td colspan="2" style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: white; color:black; text-align: center;">Revision as of 20:23, 3 July 2016</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l1" >Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>[[Category:BASIC]]</div></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>[[Category:BASIC]]</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:Programming tips]]</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>Programs frequently need to claim some resource, such as an open file or an</div></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>Programs frequently need to claim some resource, such as an open file or an</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>area of memory, and need to be able ensure the resources are not repeatedly</div></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>area of memory, and need to be able ensure the resources are not repeatedly</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l33" >Line 33:</td>
<td colspan="2" class="diff-lineno">Line 34:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>exist it is created and its value is set to zero.</div></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>exist it is created and its value is set to zero.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>So armed with this knowledge we can now modify ''PROCCloseFiles'' so that it</div></td><td class='diff-marker'>+</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>So<ins class="diffchange diffchange-inline">, </ins>armed with this knowledge we can now modify ''PROCCloseFiles'' so that it</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>never fails with a ''No such variable'' error:</div></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>never fails with a ''No such variable'' error:</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td></tr>
</table>Jgharstonhttp://beebwiki.mdfs.net/index.php?title=Forcing_a_variable_to_exist&diff=1526&oldid=prevWikiSysop: 1 revision2015-03-08T18:12:42Z<p>1 revision</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<tr style="vertical-align: top;" lang="en">
<td colspan="1" style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: white; color:black; text-align: center;">Revision as of 18:12, 8 March 2015</td>
</tr><tr><td colspan="2" style="text-align: center;" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>WikiSysophttp://beebwiki.mdfs.net/index.php?title=Forcing_a_variable_to_exist&diff=296&oldid=prevWikiSysop: 1 revision2015-03-08T00:13:06Z<p>1 revision</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<tr style="vertical-align: top;" lang="en">
<td colspan="1" style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: white; color:black; text-align: center;">Revision as of 00:13, 8 March 2015</td>
</tr><tr><td colspan="2" style="text-align: center;" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>WikiSysophttp://beebwiki.mdfs.net/index.php?title=Forcing_a_variable_to_exist&diff=25&oldid=prevWikiSysop: .2015-03-07T23:57:52Z<p>.</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<tr style="vertical-align: top;" lang="en">
<td colspan="1" style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: white; color:black; text-align: center;">Revision as of 23:57, 7 March 2015</td>
</tr><tr><td colspan="2" style="text-align: center;" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>WikiSysophttp://beebwiki.mdfs.net/index.php?title=Forcing_a_variable_to_exist&diff=1525&oldid=prevWikiSysop: 1 revision2013-08-28T22:58:14Z<p>1 revision</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<tr style="vertical-align: top;" lang="en">
<td colspan="1" style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: white; color:black; text-align: center;">Revision as of 22:58, 28 August 2013</td>
</tr><tr><td colspan="2" style="text-align: center;" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>WikiSysophttp://beebwiki.mdfs.net/index.php?title=Forcing_a_variable_to_exist&diff=295&oldid=prevJgharston at 23:43, 23 June 20072007-06-23T23:43:11Z<p></p>
<p><b>New page</b></p><div>[[Category:BASIC]]<br />
Programs frequently need to claim some resource, such as an open file or an<br />
area of memory, and need to be able ensure the resources are not repeatedly<br />
claimed when not needed, or to be able to ''cleanup'' when they are no<br />
longer needed. Code similar to the following is typical:<br />
<br />
DEFPROCCloseFiles<br />
CLOSE#in%<br />
CLOSE#out%<br />
ENDPROC<br />
<br />
The cleanup code will need to be executed not only on the normal exit from<br />
the program but also if a premature exit takes place because of an error<br />
(including Escape). Therefore it is likely that the routine will be called<br />
from ''ON ERROR'' statements:<br />
<br />
ON ERROR PROCCloseFiles:END<br />
<br />
But this immediately causes a problem. What happens if the premature exit<br />
occurs '''before''' one or more of the variables used in ''PROCCloseFiles''<br />
have been created? The result would be a ''No such variable'' error, which<br />
would activate the ON ERROR routine, which would call PROCCloseFiles, which<br />
would result in another error and so on '''ad infinitum'''. This is not<br />
good!<br />
<br />
What is needed is a way of forcing a variable to exist if it doesn't<br />
already, but leaving its value unchanged if it does. Fortunately ''BBC<br />
BASIC'' provides a way of doing this:<br />
<br />
variable = variable<br />
<br />
If ''variable'' already exists it is left unaltered, if ''variable'' doesn't<br />
exist it is created and its value is set to zero.<br />
<br />
So armed with this knowledge we can now modify ''PROCCloseFiles'' so that it<br />
never fails with a ''No such variable'' error:<br />
<br />
DEFPROCCloseFiles<br />
in% =in% :IF in% THEN A%=in% :in%=0 :CLOSE#A%<br />
out%=out%:IF out% THEN A%=out%:out%=0:CLOSE#A%<br />
ENDPROC<br />
<br />
Additionally, this zeros the variable before using a copy of it, so if an<br />
error occurs within the ''PROCCloseFiles'' routine, the variable will have<br />
already zeroed and there won't be a repeated attempt to close the file<br />
again.<br />
<br />
You can use this technique whenever you can't be sure whether a variable has<br />
been created or not. Another example is claiming memory. If you repeatedly<br />
claim memory pointed to by the same variable, you "leak" the previously<br />
claimed memory. Code similar to the following avoids this:<br />
<br />
mem%=mem%:IF mem%=0:DIM mem% size%<br />
<br />
'''By JGH, Jun 2007. Based on article by Richard Russell for BBFW Wiki, May 2006'''</div>Jgharston