User Tools

Site Tools


re-dimensioning_20arrays

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
re-dimensioning_20arrays [2018/03/31 13:19]
127.0.0.1 external edit
re-dimensioning_20arrays [2019/04/09 22:08]
richardrussell Added BBCSDL procedures
Line 1: Line 1:
 =====Re-dimensioning arrays===== =====Re-dimensioning arrays=====
  
-//by Richard Russell, May 2014//\\ \\  BBC BASIC does not have a **REDIM** or **REDIM PRESERVE** statement, as found in some other dialects of BASIC (REDIM allows you to increase the dimensions of an existing array - the contents are lost - and REDIM PRESERVE allows you to increase the dimensions of an array without destroying its contents). However it is not difficult to provide an equivalent functionality using user-defined procedures:​\\ ​+//by Richard Russell, May 2014//\\ \\  BBC BASIC does not have a **REDIM** or **REDIM PRESERVE** statement, as found in some other dialects of BASIC (REDIM allows you to increase the dimensions of an existing array - the contents are lost - and REDIM PRESERVE allows you to increase the dimensions of an array without destroying its contents). However it is not difficult to provide an equivalent functionality using user-defined procedures ​(these are for BB4W, for BBCSDL see below):\\  
 +<code bb4w>
         DEF PROCredim1d(RETURN P%,S%,D%)         DEF PROCredim1d(RETURN P%,S%,D%)
         LOCAL A%         LOCAL A%
Line 49: Line 50:
         P% = A%         P% = A%
         ENDPROC         ENDPROC
 +</​code>​
 The above procedures support 1-dimensional and 2-dimensional arrays; equivalent routines for arrays with more dimensions can be derived if required. If you REDIM PRESERVE a 2-dimensional (or more) array you should normally only change the **first** dimension; if you change another dimension the data will be '​preserved'​ but will appear to have **moved**. Note that it isn't usually sensible to **reduce** the size of an array.\\ \\  In each case the parameters to the procedure are the name of the array, the size of each element of the array (see table below) and the required dimension(s). As with the normal **DIM** statement, the available indices run from zero to the specified value.\\ \\  Here is a simple demonstration program:​\\ ​ The above procedures support 1-dimensional and 2-dimensional arrays; equivalent routines for arrays with more dimensions can be derived if required. If you REDIM PRESERVE a 2-dimensional (or more) array you should normally only change the **first** dimension; if you change another dimension the data will be '​preserved'​ but will appear to have **moved**. Note that it isn't usually sensible to **reduce** the size of an array.\\ \\  In each case the parameters to the procedure are the name of the array, the size of each element of the array (see table below) and the required dimension(s). As with the normal **DIM** statement, the available indices run from zero to the specified value.\\ \\  Here is a simple demonstration program:​\\ ​
 +<code bb4w>
         DIM array(100)         DIM array(100)
         FOR I% = 0 TO 100         FOR I% = 0 TO 100
Line 63: Line 66:
         PRINT "Test completed successfully"​         PRINT "Test completed successfully"​
         END         END
 +</​code>​
 Note that if you REDIM (not REDIM PRESERVE) a **string array** you must explicitly empty the array first:​\\ ​ Note that if you REDIM (not REDIM PRESERVE) a **string array** you must explicitly empty the array first:​\\ ​
 +<code bb4w>
         array$() = ""​         array$() = ""​
         PROCredim1d(array$(),​ ^array$(1)-^array$(0),​ newsize%)         PROCredim1d(array$(),​ ^array$(1)-^array$(0),​ newsize%)
-\\  ​The most reliable way of determining the element size is to calculate it at run-time, as in the above examples, but for reference here are the appropriate sizes for each type of array:\\ \\ +</​code>​ 
 + 
 +Here are equivalent procedures for //BBC BASIC for SDL 2.0//: 
 + 
 +<code bb4w> 
 +      DEF PROCredim1d(RETURN p%%,​S%,​D%) 
 +      LOCAL a%%,N% 
 +      IF ?​p%%<>​1 ERROR 14, "Bad use of array"​ 
 +      N% = 5+S%*(D%+1) 
 +      IF p%%<​LOMEM OR p%%>​HIMEM SYS "​free",​ p%% 
 +      SYS "​malloc",​ N% TO a%% 
 +      IF @platform% AND &40 ELSE a%%=!^a%% 
 +      IF a%%=0 ERROR 11, "DIM space"​ 
 +      SYS "​memset",​ a%%, 0, N% 
 +      ?a%%=1 : a%%!1=D%+1 
 +      p%% = a%% 
 +      ENDPROC 
 + 
 +      DEF PROCredim2d(RETURN p%%,​S%,​D%,​E%) 
 +      LOCAL a%%,N% 
 +      IF ?​p%%<>​2 ERROR 14, "Bad use of array"​ 
 +      N% = 9+S%*(D%+1)*(E%+1) 
 +      IF p%%<​LOMEM OR p%%>​HIMEM SYS "​free",​ p%% 
 +      SYS "​malloc",​ N% TO a%% 
 +      IF @platform% AND &40 ELSE a%%=!^a%% 
 +      IF a%%=0 ERROR 11, "DIM space"​ 
 +      SYS "​memset",​ a%%, 0, N% 
 +      ?a%%=2 : a%%!1=D%+1 : a%%!5=E%+1 
 +      p%% = a%% 
 +      ENDPROC 
 + 
 +      DEF PROCredimpreserve1d(RETURN p%%,​S%,​D%) 
 +      LOCAL a%%,N%,O% 
 +      IF ?​p%%<>​1 ERROR 14, "Bad use of array"​ 
 +      N% = 5+S%*(D%+1) 
 +      O% = 5+S%*p%%!1 
 +      SYS "​malloc",​ N% TO a%% 
 +      IF @platform% AND &40 ELSE a%%=!^a%% 
 +      IF a%%=0 ERROR 11, "DIM space"​ 
 +      IF N%>O% SWAP N%,O% 
 +      SYS "​memmove",​ a%%, p%%, N% 
 +      a%%!1=D%+1 
 +      IF p%%<​LOMEM OR p%%>​HIMEM SYS "​free",​ p%% 
 +      p%% = a%% 
 +      ENDPROC 
 + 
 +      DEF PROCredimpreserve2d(RETURN p%%,​S%,​D%,​E%) 
 +      LOCAL a%%,N%,O% 
 +      IF ?​p%%<>​2 ERROR 14, "Bad use of array"​ 
 +      N% = 9+S%*(D%+1)*(E%+1) 
 +      O% = 9+S%*p%%!1*p%%!5 
 +      SYS "​malloc",​ N% TO a%% 
 +      IF @platform% AND &40 ELSE a%%=!^a%% 
 +      IF a%%=0 ERROR 11, "DIM space"​ 
 +      IF N%>O% SWAP N%,O% 
 +      SYS "​memmove",​ a%%, p%%, N% 
 +      a%%!1=D%+1 : a%%!5=E%+1 
 +      IF p%%<​LOMEM OR p%%>​HIMEM SYS "​free",​ p%% 
 +      p%% = a%% 
 +      ENDPROC 
 +</​code>​ 
 + 
 +The most reliable way of determining the element size is to calculate it at run-time, as in the above examples, but for reference here are the appropriate sizes for each type of array:\\
  
 | **byte** e.g. array&​()\\ | size=**1**\\ | | **byte** e.g. array&​()\\ | size=**1**\\ |
Line 76: Line 143:
 | **64-bit integer** e.g. array%%() \\ | size=**8***\\ | | **64-bit integer** e.g. array%%() \\ | size=**8***\\ |
 | **80-bit float** e.g. array()\\ | size=**10***\\ | | **80-bit float** e.g. array()\\ | size=**10***\\ |
- * BB4W version 6 only+ * BB4W version 6 or BBCSDL 
re-dimensioning_20arrays.txt · Last modified: 2019/04/09 22:08 by richardrussell