-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path570.html
More file actions
212 lines (212 loc) · 19.7 KB
/
Copy path570.html
File metadata and controls
212 lines (212 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SmallBASIC | DIM</title>
<meta name="description" content="Reserves storage space for an array.">
<link rel="canonical" href="570">
<link rel="keywords" href="DIM var([lower TO] upper [, ...]) [, ...]">
<link rel="stylesheet" href="/css/style.css">
<link rel="icon" type="image/png" href="/images/sb-desktop-32x32.png">
<script src="/clipboard.js"></script>
</head>
<body>
<button onclick="topFunction()" id="BackToTopBtn" title="Go to top">⯅</button>
<script src="/backtotop.js"></script>
<div class="wrapAll clearfix">
<nav class="navigation">
<div class="logo">
<a href="/"><img src='/images/sb-logo.png?v=2' alt="logo"></a>
</div>
<div class="navlinks">
<a href="/pages/download.html">Download</a>
<a href="/pages/news.html">News</a>
<a href="/pages/community.html">Community</a>
<a href="/pages/articles.html">Resources</a>
<a class='active' href="/pages/reference.html">Language Reference</a>
<a href="/pages/guide.html">SmallBASIC Manual</a>
</div>
</nav>
<div class="mainsection">
<div class="tabs clearfix">
<div class="tabsRight">
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/blob/master/_build/reference/570-data-dim.markdown">Edit</a>
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/commits/master/_build/reference/570-data-dim.markdown">History</a>
</div>
</div>
<div class="article">
<h1>DIM</h1>
<blockquote>DIM var([lower TO] upper [, …]) [, …]</blockquote>
<div class="siteSub">
<p>
<a href="/">Home</a> >
<a href="/pages/reference.html">Reference</a> >
<a href="/pages/data.html">Data</a>
</p>
</div>
<p>Reserves storage space for an array.</p>
<p>The array will have (upper-lower)+1 elements. If <code>lower</code>
is not specified, and <code>OPTION BASE</code> hasn’t used, elements
start at 0.</p>
<h2 id="example-1-one-dimensional-arrays">Example 1: One dimensional
arrays</h2>
<div class="sourceCode" id="cb1"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">REM One dimensional array of 7 elements, starting from 0</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="co">REM with elements A(0), A(1), ... , A(6)</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> A(<span class="dv">6</span>)</span></code></pre></div>
<div class="sourceCode" id="cb2"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co">REM One dimensional array of 7 elements, starting from 3</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="co">REM with elements A(3), A(4), ... , A(9)</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> A(<span class="dv">3</span> <span class="kw">TO</span> <span class="dv">9</span>)</span></code></pre></div>
<div class="sourceCode" id="cb3"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co">REM One dimensional array of 6 elements, starting from 1</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="co">REM with elements A(1), A(2), ... , A(6)</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="pp">option</span> base <span class="dv">1</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> A(<span class="dv">6</span>)</span></code></pre></div>
<h2 id="example-2-multi-dimensional-arrays">Example 2: Multi dimensional
arrays</h2>
<div class="sourceCode" id="cb4"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co">REM Two dimensional array</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> A(<span class="dv">3</span>, <span class="dv">4</span>)</span></code></pre></div>
<div class="sourceCode" id="cb5"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co">REM Three dimensional array</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> A(<span class="dv">2</span> <span class="kw">TO</span> <span class="dv">6</span>, <span class="dv">5</span> <span class="kw">TO</span> <span class="dv">9</span>, <span class="dv">1</span> <span class="kw">TO</span> <span class="dv">8</span>)</span></code></pre></div>
<h2 id="example-3-empty-array">Example 3: Empty array</h2>
<div class="sourceCode" id="cb6"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co">REM Allocating zero-length arrays:</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> z()</span></code></pre></div>
<h2 id="example-4-creating-and-accessing-arrays">Example 4: Creating and
accessing arrays</h2>
<div class="sourceCode" id="cb7"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="pp">Option</span> Base <span class="dv">1</span> <span class="co">' Set default lower bound of arrays to 1</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a>? <span class="st">"Showing x,y,z elements of index 5:"</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a>?</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="co">' Reserve space for a single 1-dimensional array:</span></span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a>x = <span class="dv">0</span></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a>y = <span class="dv">10</span></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a>z = <span class="dv">20</span></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a><span class="dt">Dim</span> a(<span class="dv">1</span> <span class="kw">To</span> <span class="dv">30</span>)</span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a><span class="kw">For </span>i = <span class="dv">1</span> <span class="kw">To</span> <span class="dv">10</span></span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> a(i + x) = i</span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> a(i + y) = i + <span class="dv">10</span></span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> a(i + z) = i + <span class="dv">100</span></span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a><span class="kw">Next</span></span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a>? a(<span class="dv">5</span> + x), a(<span class="dv">5</span> + y), a(<span class="dv">5</span> + z),, <span class="st">"(1-dimensional array)"</span></span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a><span class="co">' Reserve the same space for three 1-dimensional arrays:</span></span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a><span class="dt">Dim</span> a_x(<span class="dv">1</span> <span class="kw">To</span> <span class="dv">10</span>), a_y(<span class="dv">1</span> <span class="kw">To</span> <span class="dv">10</span>), a_z(<span class="dv">1</span> <span class="kw">To</span> <span class="dv">10</span>)</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a><span class="kw">For </span>i = <span class="dv">1</span> <span class="kw">To</span> <span class="dv">10</span></span>
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a> a_x(i) = i</span>
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a> a_y(i) = i + <span class="dv">10</span></span>
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a> a_z(i) = i + <span class="dv">100</span></span>
<span id="cb7-23"><a href="#cb7-23" aria-hidden="true" tabindex="-1"></a><span class="kw">Next</span></span>
<span id="cb7-24"><a href="#cb7-24" aria-hidden="true" tabindex="-1"></a>? a_x(<span class="dv">5</span>), a_y(<span class="dv">5</span>), a_z(<span class="dv">5</span>),, <span class="st">"(three 1-dimensional arrays)"</span></span>
<span id="cb7-25"><a href="#cb7-25" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-26"><a href="#cb7-26" aria-hidden="true" tabindex="-1"></a><span class="co">' Reserve the same space for a single 2-dimensional array:</span></span>
<span id="cb7-27"><a href="#cb7-27" aria-hidden="true" tabindex="-1"></a>x = <span class="dv">1</span></span>
<span id="cb7-28"><a href="#cb7-28" aria-hidden="true" tabindex="-1"></a>y = <span class="dv">2</span></span>
<span id="cb7-29"><a href="#cb7-29" aria-hidden="true" tabindex="-1"></a>z = <span class="dv">3</span></span>
<span id="cb7-30"><a href="#cb7-30" aria-hidden="true" tabindex="-1"></a><span class="dt">Dim</span> a(<span class="dv">1</span> <span class="kw">To</span> <span class="dv">10</span>, x <span class="kw">To</span> z)</span>
<span id="cb7-31"><a href="#cb7-31" aria-hidden="true" tabindex="-1"></a><span class="kw">For </span>i = <span class="dv">1</span> <span class="kw">To</span> <span class="dv">10</span></span>
<span id="cb7-32"><a href="#cb7-32" aria-hidden="true" tabindex="-1"></a> a(i, x) = i</span>
<span id="cb7-33"><a href="#cb7-33" aria-hidden="true" tabindex="-1"></a> a(i, y) = i + <span class="dv">10</span></span>
<span id="cb7-34"><a href="#cb7-34" aria-hidden="true" tabindex="-1"></a> a(i, z) = i + <span class="dv">100</span></span>
<span id="cb7-35"><a href="#cb7-35" aria-hidden="true" tabindex="-1"></a><span class="kw">Next</span></span>
<span id="cb7-36"><a href="#cb7-36" aria-hidden="true" tabindex="-1"></a>? a(<span class="dv">5</span>, x), a(<span class="dv">5</span>, y), a(<span class="dv">5</span>, z),, <span class="st">"(2-dimensional array)"</span></span>
<span id="cb7-37"><a href="#cb7-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-38"><a href="#cb7-38" aria-hidden="true" tabindex="-1"></a><span class="co">' Reserve the same space for a single nested array:</span></span>
<span id="cb7-39"><a href="#cb7-39" aria-hidden="true" tabindex="-1"></a><span class="dt">Dim</span> a(<span class="dv">1</span> <span class="kw">To</span> <span class="dv">10</span>)</span>
<span id="cb7-40"><a href="#cb7-40" aria-hidden="true" tabindex="-1"></a><span class="kw">For </span>i = <span class="dv">1</span> <span class="kw">To</span> <span class="dv">10</span></span>
<span id="cb7-41"><a href="#cb7-41" aria-hidden="true" tabindex="-1"></a> a(i) = [,,] <span class="co">' convert this element to nested array</span></span>
<span id="cb7-42"><a href="#cb7-42" aria-hidden="true" tabindex="-1"></a> a(i)(x) = i</span>
<span id="cb7-43"><a href="#cb7-43" aria-hidden="true" tabindex="-1"></a> a(i)(y) = i + <span class="dv">10</span></span>
<span id="cb7-44"><a href="#cb7-44" aria-hidden="true" tabindex="-1"></a> a(i)(z) = i + <span class="dv">100</span></span>
<span id="cb7-45"><a href="#cb7-45" aria-hidden="true" tabindex="-1"></a><span class="kw">Next</span></span>
<span id="cb7-46"><a href="#cb7-46" aria-hidden="true" tabindex="-1"></a>? a(<span class="dv">5</span>)(x), a(<span class="dv">5</span>)(y), a(<span class="dv">5</span>)(z),, <span class="st">"(nested array)"</span></span>
<span id="cb7-47"><a href="#cb7-47" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-48"><a href="#cb7-48" aria-hidden="true" tabindex="-1"></a><span class="co">' Reserve the same space for a single map array (see also: ARRAY, ISMAP):</span></span>
<span id="cb7-49"><a href="#cb7-49" aria-hidden="true" tabindex="-1"></a><span class="dt">Dim</span> a(<span class="dv">1</span> <span class="kw">To</span> <span class="dv">10</span>)</span>
<span id="cb7-50"><a href="#cb7-50" aria-hidden="true" tabindex="-1"></a><span class="kw">For </span>i = <span class="dv">1</span> <span class="kw">To</span> <span class="dv">10</span></span>
<span id="cb7-51"><a href="#cb7-51" aria-hidden="true" tabindex="-1"></a> a(i).x = i</span>
<span id="cb7-52"><a href="#cb7-52" aria-hidden="true" tabindex="-1"></a> a(i).y = i + <span class="dv">10</span></span>
<span id="cb7-53"><a href="#cb7-53" aria-hidden="true" tabindex="-1"></a> a(i).z = i + <span class="dv">100</span></span>
<span id="cb7-54"><a href="#cb7-54" aria-hidden="true" tabindex="-1"></a><span class="kw">Next</span></span>
<span id="cb7-55"><a href="#cb7-55" aria-hidden="true" tabindex="-1"></a>? a(<span class="dv">5</span>).x, a(<span class="dv">5</span>).y, a(<span class="dv">5</span>).z,, <span class="st">"(map array)"</span></span></code></pre></div>
<div class="lavenderBox">
<div class="header">Code samples using DIM</div>
<div class="linklist">
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/getting started/006 arrays+.bas">006 arrays+.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 3/100lines.bas">100lines.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 3/2048.bas">2048.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/misc/Misc Bpf/3d block lettes.bas">3d block lettes.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 1/3d_palmx.bas">3d_palmx.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 1/3d_torus.bas">3d_torus.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/3dtictac.bas">3dtictac.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 2/3dtorus.bas">3dtorus.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 3/3dttt.bas">3dttt.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/mobile/3dttt.bas">3dttt.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/7gables.bas">7gables.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/ai.bas">ai.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/network/anomail.bas">anomail.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 2/aquarium v2.bas">aquarium v2.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/misc/Misc Bpf/ascii 3d.bas">ascii 3d.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/mathematics/bairstow.bas">bairstow.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/units/base64.bas">base64.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/betrayal: crows ii.bas">betrayal: crows ii.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 1/bezier_pen.bas">bezier_pen.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/blackbox.bas">blackbox.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 3/Blackbox.bas">Blackbox.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/blackjack.bas">blackjack.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 3/BlackJack.bas">BlackJack.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 4/block.bas">block.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 2/boing.bas">boing.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 3/bolmo.bas">bolmo.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 3/bomb.bas">bomb.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 4/bonkers.bas">bonkers.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 3/bopit.bas">bopit.bas </a>
</div>
</div>
<div class="lavenderBox">
<div class="header">Data</div>
<div class="linklist">
<a href="/reference/581.html">APPEND </a>
<a href="/reference/1432.html">ARRAY </a>
<a href="/reference/569.html">DATA </a>
<a href="/reference/542.html">DELETE </a>
<a href="/reference/570.html"><strong>DIM</strong> </a>
<a href="/reference/543.html">EMPTY </a>
<a href="/reference/571.html">ERASE </a>
<a href="/reference/544.html">INSERT </a>
<a href="/reference/555.html">ISARRAY </a>
<a href="/reference/556.html">ISDIR </a>
<a href="/reference/557.html">ISFILE </a>
<a href="/reference/558.html">ISLINK </a>
<a href="/reference/1430.html">ISMAP </a>
<a href="/reference/559.html">ISNUMBER </a>
<a href="/reference/560.html">ISSTRING </a>
<a href="/reference/561.html">LBOUND </a>
<a href="/reference/562.html">LEN </a>
<a href="/reference/546.html">READ </a>
<a href="/reference/547.html">REDIM </a>
<a href="/reference/572.html">RESTORE </a>
<a href="/reference/548.html">SEARCH </a>
<a href="/reference/549.html">SORT </a>
<a href="/reference/550.html">SWAP </a>
<a href="/reference/567.html">UBOUND </a>
</div>
</div>
</div>
<div class="pagefooter">
This page was last edited on Thu, 26 Jan 2023 20:57:17 +0100
|
<a href="https://en.wikipedia.org/wiki/Markdown" target="_blank" rel="nofollow">Markdown</a>
processed with
<a href="https://pandoc.org/MANUAL.html#pandocs-markdown" target="_blank" rel="nofollow">pandoc 3.1.12.1</a>
</div>
</div>
</div>
</body>
</html>