[futurebasic] Re: [FB] How to declare (dim) and init an array at once

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2001 : Group Archive : Group : All Groups

From: Jay Reeve <jktr@...>
Date: Mon, 29 Oct 01 18:00:53 -0600
>In C, we can do this simply:
>
>short myArray[8] = {1,2,3,4,5,6,7,8};
>
>Is there any cool way in FB^3?

Osamu,

This may not be exactly what you are looking for, but it will allow you to populate a dimmed XREF@ or DYNAMIC array with one statement, like this:

XREF@ myArray(_maxInt) as LONG
lastElemID = FN arrayLONG(myArray,"1234,5678,-1298,3,9876")

-or-
DYNAMIC myArray(_maxInt) as INT
temp = myArray(0) 'A dynamic array must be initialized before filling
lastElemID = FN dynamicArray(myArray,"1234,5678,-1298,3,9876")

By first initializing a dynamic array, the fn can tell what size the elements are, allowing any common type of dynamic array to use the same wrapper fn dynamicArray. To avoid the initialization step, just use the specific arrayXXX fn like this:

DYNAMIC myArray(_maxInt) as INT
lastElemID = FN arrayINT(myArray,"1234,5678,-1298,3,9876")

There are a whole mess of similar FNs here. Normally you would only need to incorporate 1 or 2 of them, but you could plop them all into an INCL and have them whenever you wanted.

Enjoy.
 0"0
 =J= a  y
  "

'===================
goto "End FN arrayXXX.INCL"
'~'6
/*
These FNs allow you to fill an XREF@ or DYNAMIC
array from a string, using just one line of code.
There is one FN for each common type of XREF@ array,
and a wrapper FN for any type of DYNAMIC array.
*/
local mode
local fn arrayMem(H as HNDL, size)
if H then sethandlesize(H,size) else H = fn newhandle(size)
if fn memerr then stop "Can't change or create handle."
end fn = H

local mode
dim elemSize,start,count
local fn arrayBYTE(@arrayHP as ptr, InputStr as str255)
xref@ array(_maxLong) as byte
elemSize = sizeof(array(0))
array = fn arrayMem(arrayHP.0&,elemSize*InputStr[0])
start = 1:count = 0
do
array(count) = val&(mid$(InputStr,start))
start = instr(start,InputStr,",") + 1
count++
until start = 1
sethandlesize(arrayHP.0&,elemSize*count)
end fn = count-1

local mode
dim elemSize,start,count
local fn arrayINT(@arrayHP as ptr, InputStr as str255)
xref@ array(_maxLong) as int
elemSize = sizeof(array(0))
array = fn arrayMem(arrayHP.0&,elemSize*InputStr[0])
start = 1:count = 0
do
array(count) = val&(mid$(InputStr,start))
start = instr(start,InputStr,",") + 1
count++
until start = 1
sethandlesize(arrayHP.0&,elemSize*count)
end fn = count-1

local mode
dim elemSize,start,count
local fn arrayLONG(@arrayHP as ptr, InputStr as str255)
xref@ array(_maxLong) as long
elemSize = sizeof(array(0))
array = fn arrayMem(arrayHP.0&,elemSize*InputStr[0])
start = 1:count = 0
do
array(count) = val&(mid$(InputStr,start))
start = instr(start,InputStr,",") + 1
count++
until start = 1
sethandlesize(arrayHP.0&,elemSize*count)
end fn = count-1

local mode
dim elemSize,start,count
local fn arraySINGLE(@arrayHP as ptr, InputStr as str255)
xref@ array(_maxLong) as single
elemSize = sizeof(array(0))
array = fn arrayMem(arrayHP.0&,elemSize*InputStr[0])
start = 1:count = 0
do
array(count) = val(mid$(InputStr,start))
start = instr(start,InputStr,",") + 1
count++
until start = 1
sethandlesize(arrayHP.0&,elemSize*count)
end fn = count-1

local mode
dim elemSize,start,count
local fn arrayDOUBLE(@arrayHP as ptr, InputStr as str255)
xref@ array(_maxLong) as double
elemSize = sizeof(array(0))
array = fn arrayMem(arrayHP.0&,elemSize*InputStr[0])
start = 1:count = 0
do
array(count) = val(mid$(InputStr,start))
start = instr(start,InputStr,",") + 1
count++
until start = 1
sethandlesize(arrayHP.0&,elemSize*count)
end fn = count-1

local mode
dim elemSize,start,count,L
local fn arraySTR255(@arrayHP as ptr, InputStr as str255)
xref@ array(_maxLong) as str255
elemSize = sizeof(array(0))
array = fn arrayMem(arrayHP.0&,elemSize*InputStr[0])
start = 1:count = 0
do
L = instr(start,InputStr,",")
if L then L -= start else L = 255
array(count) = mid$(InputStr,start,L)
if L < 255 then start += L + 1
count++
until L = 255
sethandlesize(arrayHP.0&,elemSize*count)
end fn = count-1

local mode
dim c, size
local fn dynamicArray(@arrayP as ptr,@inputP as ptr)
if arrayP.AutoXREFHndl& = 0 then ¬
  shutdown "Array must be initialized before calling FN dynamicArray"
size = fn gethandlesize(arrayP.AutoXREFHndl&) / arrayP.AutoXREFMax&
select size
case 1 : c = fn arrayBYTE(arrayP.AutoXREFHndl&,inputP.0$)
case 2 : c = fn arrayINT (arrayP.AutoXREFHndl&,inputP.0$)
case 4
long if instr(1,InputP.Nil$,".")'Make educated guess
c = fn arraySINGLE(arrayP.AutoXREFHndl&,inputP.0$)
xelse
c = fn arrayLONG(arrayP.AutoXREFHndl&,inputP.0$)
end if
case 8 : c = fn arrayDOUBLE(arrayP.AutoXREFHndl&,inputP.0$)
case 256 : c = fn arraySTR255(arrayP.AutoXREFHndl&,inputP.0$)
case else : shutdown "FN DynamicArray cannot use this type of array."
end select
c++
arrayP.AutoXREFCurr& = c
arrayP.AutoXREFMax&  = c
end fn = c - 1
"End FN arrayXXX.INCL"
'~ DEMO MAIN
dynamic gArray(20) as long//CHANGE THIS TO ANY STANDARD TYPE!
//(i.e., BYTE, INT, LONG, SINGLE, DOUBLE, STR255)
/*
 Note that FN dynamicArray will look at the
 data to choose between LONG and SINGLE.
 Remove decimal pts from data to test LONG.
 Put a decimal pt in data to test SINGLE.
*/
dim r,c
r = @gArray(0)'Initialize array before filling
c = fn arrayLONG(gArray,"1.876543,2,3,4,5,6,7,87654321")

for r = 0 to c
print gArray(r)
next