Normal Topic Generic array emptier? (Read 1264 times)
jfreeman
Member
*
Offline


No personal text

Posts: 23
Location: KZN, South Africa
Joined: Apr 10th, 2005
Generic array emptier?
Jul 20th, 2013 at 3:41pm
Print Post Print Post  
Before I reinvent the wheel, does anyone have a generic subroutine that will empty an array like vMyArray[x, y, z]).  I actually only need 2 dimensions, but generic is more useful.

John F
  

John F
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Generic array emptier?
Reply #1 - Jul 21st, 2013 at 5:33am
Print Post Print Post  
If you have a known number of dimensions, known data type, and in-line code, the loop within a loop method is common-place. If, however, you are working inside of function or subroutine and the array is passed in as an argument, you will need to use the array access functions:

"function dims( var a as array ) as int"
"function dimLimit( n as int, var a as array ) as int"
"function elementType( var a as array ) as string"

"function intElement( var a as array, ... as int ) as int"
"function doubleElement( var a as array, ... as int ) as double"
"function moneyElement( var a as array, ... as int ) as money"
"function booleanElement( var a as array, ... as int ) as boolean"
"function charElement( var a as array, ... as int ) as char"
"function dateElement( var a as array, ... as int ) as date"
"function timeElement( var a as array, ... as int ) as time"
"function stringElement( var a as array, ... as int ) as string"

"subroutine setIntElement( newValue as int, var a as array, ... as int )"
"subroutine setDoubleElement( newValue as double, var a as array, ... as int )"
"subroutine setMoneyElement( newValue as money, var a as array, ... as int )"
"subroutine setBooleanElement( newValue as boolean, var a as array, ... as int )"
"subroutine setCharElement( newValue as char, var a as array, ... as int )"
"subroutine setDateElement( newValue as date, var a as array, ... as int )"
"subroutine setTimeElement( newValue as time, var a as array, ... as int )"
"subroutine setStringElement( newValue as string, var a as array, ... as int )"

These allow you to access arrays without having to pass in the number of dimensions or even the data type of the array. You could write a single subroutine that "empties" (I assume you mean initializes) all of your arrays.

If you have a need for truly arbitrary dimensions, I recommend that you consider using a one dimensional array, and use read and write access functions that "flatten" the additional dimensions:

2D:
position = (width * y) + x

3D:
position = (height * width * z) + (width * y) + x;

etc...

If you use an array to hold the boundaries for each dimension, you can literally create an array as many dimensions as your memory capacity will allow.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
PianoMan
Member
Members
*
Offline


No personal text

Posts: 44
Joined: Oct 13th, 2004
Re: Generic array emptier?
Reply #2 - Jul 22nd, 2013 at 8:17pm
Print Post Print Post  
I have tried "subroutine setStringElement( newValue as string, var a as array, ... as int )". The documentation says that ...as int can accept a list of dimensions. I have tried various 'integer lists' - 1,2,3 or 1;2;3, or "1;2;3" or "1", "2", "3" but none of these work. Please advise.
Thanks
  
Back to top
IP Logged
 
jfreeman
Member
*
Offline


No personal text

Posts: 23
Location: KZN, South Africa
Joined: Apr 10th, 2005
Re: Generic array emptier?
Reply #3 - Jul 22nd, 2013 at 8:23pm
Print Post Print Post  
Finally realized that 3-D is the way to go (still pretty generic!): -
Subroutine EmptyArray(var sAR as array)

/*An investment management application contains string, money and int LE's. 3-D arrays
housed in Global Code are used as 'warehouses' for manipulating and parsing
information entered on one form of a 3-tier relational database to several others,
where each open form may have a result set to be populated.
LE Names are on the x-axis: [x, 1, 1], [x, 1, 2], [x, 1, 3] and so on.
The z-axis represents the several forms, the result set totals of each of which are on the y-axis.
These are entered in Global Code. The subroutine leaves the LE Names unaltered.*/

var vType as string
var vZ as int
var vY as int
var vX as int
var line as int
var col as int
var deep as int

vX = DimLimit(1, sAR)      //The first form (z = 1) is chosen as having the greatest no. of LE's
vY = DimLimit(2, sAR)      //Result set total for the form with the greatest number of instances open
vZ = DimLimit(3, sAR)   //The mumber of different forms being processed

vType = ElementType(sAR)
if vType = "string"
{
While line <= vX
     {
     line = 1
     col = 2
     While col <= vY
           {
           deep = 1
           while deep <= vZ
                 {
                 SetStringElement("", sAR, line, col, deep)
                 deep = deep + 1
                 }
           col = col + 1
           }
     line = line + 1
     }
}
if vType = "int"
{
While line <= vX
     {
     line = 1
     col = 2
     While col <= vY
           {
           deep = 1
           while deep <= vZ
                 {
                 SetIntElement(0, sAR, line, col, deep)
                 deep = deep + 1
                 }
           col = col + 1
           }
     line = line + 1
     }
}
if vType = "money"
{
While line <= vX
     {
     line = 1
     col = 2
     While col <= vY
           {
           deep = 1
           while deep <= vZ
                 {
                 SetMoneyElement(0, sAR, line, col, deep)
                 deep = deep + 1
                 }
           col = col + 1
           }
     line = line + 1
     }
}
End Subroutine
  

John F
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Generic array emptier?
Reply #4 - Jul 22nd, 2013 at 8:30pm
Print Post Print Post  
PianoMan wrote on Jul 22nd, 2013 at 8:17pm:
I have tried "subroutine setStringElement( newValue as string, var a as array, ... as int )". The documentation says that ...as int can accept a list of dimensions. I have tried various 'integer lists' - 1,2,3 or 1;2;3, or "1;2;3" or "1", "2", "3" but none of these work. Please advise.
Thanks


The "list" merely indicates that you can keep adding arguments so as to have the right number for the number of dimensions in the array.

For a two dimensional array (access array_name[1][4]):
Code
Select All
SetStringElement("whatever", array_name, 1, 4)
 



For a four dimensional array (access array_name[1][4][9][6]):
Code
Select All
SetStringElement("whatever", array_name, 1, 4, 9, 6)
 


  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
PianoMan
Member
Members
*
Offline


No personal text

Posts: 44
Joined: Oct 13th, 2004
Re: Generic array emptier?
Reply #5 - Jul 22nd, 2013 at 8:40pm
Print Post Print Post  
OK, sure. I get it now.
  
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Generic array emptier?
Reply #6 - Jul 22nd, 2013 at 9:10pm
Print Post Print Post  
While not actually "arbitrary" dimensions, the following code will work nicely with up to five. You pass a one-dimensional array of indexes to accesses the array. That way you can use any array with up to five dimensions (I've been programming for more than 30 years and can't think of a case where I needed more than five) using the same routine, without the math involved with arbitrary dimensions:

Code
Select All
function GetIntFromAny(var aa as array, in_dims as int, var index as array) as int
var r as int
var i as array[5] of int

	r = 0
	if((in_dims > 0) and (in_dims < 6))
	{
		if(in_dims = 5)
		{
			i[1] = IntElement(index, 1)
			i[2] = IntElement(index, 2)
			i[3] = IntElement(index, 3)
			i[4] = IntElement(index, 4)
			i[5] = IntElement(index, 5)
			r = IntElement(aa, i[1], i[2], i[3], i[4], i[5])
		}
		else if(in_dims = 4)
		{
			i[1] = IntElement(index, 1)
			i[2] = IntElement(index, 2)
			i[3] = IntElement(index, 3)
			i[4] = IntElement(index, 4)
			r = IntElement(aa, i[1], i[2], i[3], i[4])
		}
		else if(in_dims = 3)
		{
			i[1] = IntElement(index, 1)
			i[2] = IntElement(index, 2)
			i[3] = IntElement(index, 3)
			r = IntElement(aa, i[1], i[2], i[3])
		}
		else if(in_dims = 2)
		{
			i[1] = IntElement(index, 1)
			i[2] = IntElement(index, 2)
			r = IntElement(aa, i[1], i[2])
		}
		else if(in_dims = 1)
		{
			i[1] = IntElement(index, 1)
			r = IntElement(aa, i[1])
		}
	}
	return(r)
end function

 



It could use a check against "dims" and  "DimLimit" for errors.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged