[futurebasic] Re: [FB] Containers and arrays

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

From: Ken Shmidheiser <k.shmidheiser@...>
Date: Tue, 17 Jul 2001 17:44:48 -0400
Explaining how to pass CONTAINERs to functions, Alain wrote:

>Containers are global in nature so they can be accessed without
>passing those variables as parameters.
>However, you cannot use them with functions in local mode without
>passing them as parameters.
>I cannot guarantee it is the correct way to handle this but here an
>example that adds a string to a container within a function in local mode:
>
>begin globals
>   dim gC as container
>end globals
>
>local mode
>local fn addStringToContainer(@strPtr as ^str255,@CPtr as ptr)
>   long if [CPtr] = _nil
>     & CPtr,fn NewHandle(0)
>   end if
>end fn = fn PtrAndHand(strPtr + 1,[CPtr],|strPtr|)
>
>
>fn addStringToContainer("This is a test",gC)
>
>print gC
>
>do
>until fn button
>
>gC = ""
>end
>
>It may look ridiculous since the same thing is performed doing this:
>
>gC = "This is a test"
>
>but you can get the idea on how to work on containers with generic functions.


Using Alain's, suggestion, this modified code now works:

'--------- BEGIN FB^3 CODE -------------

/*
This demo counts the number of times a specified character
appears in a CONTAINER

Run under CONSOLE (Fast)
*/

BEGIN GLOBALS
DIM my1Container   AS CONTAINER
DIM my2Container   AS CONTAINER
DIM temp1Container AS CONTAINER
DIM temp2Container AS CONTAINER
END GLOBALS

LOCAL FN countContainerChars( @CPtr AS PTR, delimStr AS STR255 )
DIM newPos   AS LONG
DIM maxPos   AS LONG
DIM elements AS LONG

LONG IF [CPtr] = _nil
& CPtr, fn NewHandle(0)
END IF

temp1Container = CPtr
temp2Container = delimStr

IF temp1Container = "" THEN elements = 0 : EXIT FN
IF temp2Container = "" THEN elements = 0 : EXIT FN

maxPos   = LEN( temp1Container )
newPos   = 1
elements = 1

WHILE ( newPos < maxPos ) AND ( newPos <> 0 )
newPos = INSTR( newPos, temp1Container, temp2Container )
LONG IF newPos <> 0
INC( elements)
INC( newPos  )
END IF
WEND

// Empty temporary container handles
temp1Container = ""
temp2Container = ""

END FN = elements

// Handle first container:
my1Container =  ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
my1Container += ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"

DIM findChar AS STR255

// Search for commas
findChar = ","
PRINT:PRINT "my1Container has"; ¬
FN countContainerChars( my1Container,findChar ); " commas ("","")"

// Handle second container:
my2Container =  "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"
my2Container += "@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@"

// Search for # signs
findChar = "#"
PRINT:PRINT "my2Container has"; ¬
FN countContainerChars( my2Container,findChar ); " number signs (""#"")"

// Search for # signs
findChar = "@"
PRINT:PRINT "my2Container has"; ¬
FN countContainerChars( my2Container,findChar ); " number signs (""@"")"


'--------- END CODE -------------