Jim:

ITEMCNT ... certainly meets the 'elegance' requirement.

Appreciate very much a new (to me) tool, Jim.

Bruce Chitiea
SafeSectors, Inc.
909.238.9012 Mobile

------ Original Message ------
Sent: 2/17/2017 7:07:39 AM
Subject: Re: [RBASE-L] - DECLARE CURSOR Source Data Array
From: "'James Bentley' via RBASE-L" <[email protected]>
To: "[email protected]" <[email protected]>
Cc:

don't forget the ITEMCNT function

If sequence is unimportantSET VAR tList TEXT = ('1,2,3,4,5')
SET VAR tCnt INTEGER = (ITEMCNT(.tLIST))  -- THIS RETURNS VALUE OF 5
WHILE tCnt > 0 THEN
  SET VAR vText = (SSUB(.tList,.tCnt))
-- do calculations
   SET VAR tCnt = (.tCnt - 1)
ENDWHILE

otherwise if sequence is important
SET VAR tList TEXT = ('1,2,3,4,5')
SET VAR tCnt INTEGER = (ITEMCNT(.tLIST))  -- THIS RETURNS VALUE OF 5
SET VAR tIndex INT = 0
WHILE tCnt > 0 THEN
  SET VAR tIndex = (.tIndex + 1)
  SET VAR tTextTerm = (SSUB(.tList, .tIndex))
-- do calculations
   SET VAR tCnt = (.tCnt - 1)
ENDWHILE

Both cases establish a definitive number of times to loop which
can be better optimized. It also eliminates the if test to terminate
the loop thus speeding up execution.


Jim Bentley, American Celiac Society 1-504-305-2968


--------------------------------------------------------------------------------
From: 'Lawrence Lustig' via RBASE-L <[email protected]>
To: "[email protected]" <[email protected]>
Sent: Friday, February 17, 2017 7:15 AM
Subject: Re: [RBASE-L] - DECLARE CURSOR Source Data Array

<<
Is there an "elegant" way to do this with a simple array of values:


DECLARE CursorName CURSOR FOR (1,2,3,4,5)
... where we're stepping through the array WITHOUT drawing upon a source table?
>>

Is this expected to produce five iterations with one value each time, or one iteration with five values?

If you want five iterations of the loop with one value each time, and the values are guaranteed not to contain commas (which will be true if they're numeric values) then you can do this:

SET VAR tList TEXT = ('1,2,3,4,5')
SET VAR tIndex INT = 0

WHILE 1 = 1 THEN

    SET VAR tIndex = (.tIndex + 1)
    SET VAR tTextTerm = (SSUB(.tList, .tIndex))
    IF tTextTerm IS NULL THEN
       BREAK
    ENDIF

    -- Cast to appropriate type if not TEXT
    SET VAR tIntTerm INT = (INT(.tTextTerm))

    -- Meaningful code here

ENDWHILE

CLEAR VAR tList, tIndex, tTextTerm, tIntTerm
RETURN

You can construct more complex scenarios by pulling two or three terms off the list for each iteration, or using a different character to separate fields and records in the list. This is also suitable for use with LISTOF(), getting a list of values from a table and then iterating over the list without the cursor.
--
Larry
--
You received this message because you are subscribed to the Google Groups "RBASE-L" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.



--
You received this message because you are subscribed to the Google Groups "RBASE-L" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"RBASE-L" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to