' Dim2.iBas
DIM %IMAX%,%JMAX%,%INDEX%

'-------------
'What is Dim2?
'-------------
'
' Dim2 is a very simple include file
' example to show how to deal with
' 2 dimensions arrays in iziBasic, 
' like Array(10,5), when iziBasic
' "only" offers access to the A(n)
' array (1 dimension).
'
' Practice exercise for you: 
'  adapt this source code to deal 
'  with Array(3,4,5)


'----------
'How to use
'----------
'
' Step #1: set the values in the
' Parametrization part of this source
' code (see comments on how to set)
'
' Step #2: add {INCLUDE "Dim2.iBas"}
' in your project headers, BEFORE
' defining any variable (so, before
' any DIM definition)
' 
' Step #3a: store values by doing a
' GOSUB _PutInArray after setting the
' I,J indexes and the V value to 
' store in Array(I,J)
' If I or J is out of range, then 
' an error will be returned in E
' 
' Step #3b: retrieve values by doing
' a GOSUB _GetFromArray after setting
' the I,J indexes and V will be 
' updated with the value found in
' Array(I,J) 
' If I or J is out of range, then 
' an error will be returned in E
'
' Example:
'  {INCLUDE "Dim2.iBas"}
'  DIM %MyVar% 'AFTER Include (see Step #1)
'  BEGIN
'   I=1 : J=2 : V=3
'   GOSUB _PutInArray 'Array(1,2)=3
'   I=4 : J=5 
'   GOSUB _GetFromArray 'V=Array(4,5)
'   PRINT V
'  END


'---------------
'Parametrization
'---------------

CONST %IMAX%=10 'Array(%IMAX%,%JMAX%)
CONST %JMAX%=5
DIM A(85) 'Value=30+(%IMAX%+1)*%JMAX%
{DEFINE "PARSER_ON"} 'Comment this
'line if PARSER OFF
'Note: PARSER status should ALWAYS be
'the same when calling _PutInArray or
'_GetFromArray. So, make sure to
'define it once on top of your source
'code or each time before you call
'these GOSUBs if you come to swap it
'from ON to OFF in your code.


'-------------
'Public GOSUBs
'-------------

_PutInArray:
'-----------------------
'Array(I,J)=V, E=Error
'Input : I,J,V
'Output: E
'-----------------------
 GOSUB _CalcArrayIndex
 IF E=FALSE LET A(%INDEX%)=V
RETURN

_GetFromArray:
'-----------------------
'V=Array(I,J), E=Error
'Input : I,J
'Output: V,E
'-----------------------
 GOSUB _CalcArrayIndex
 IF E=FALSE LET V=A(%INDEX%)
RETURN


'--------------
'Private GOSUBs
'--------------

_CalcArrayIndex:
'-----------
'Input : I,J
'Output: E
'-----------
 {PARSER ON}
 E=(I<1) OR (I>%IMAX%) OR (J<1) OR (J>%JMAX%)
 IF E=FALSE LET %INDEX%=30+I*%JMAX%+J
 {IFNDEF "PARSER_ON"}
  {PARSER OFF}
 {ENDIF}
RETURN
