Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread How to get upper limit of an array?
Fri, Mar 28 2014 6:15 PMPermanent Link

Barry

I have two questions about uninitialized arrays.

1) Can I initialize an entire array to a value?

set _vResult = 'x';  --??

2) How can I loop through the array without knowing the upper limit of the array? Cardinality will be zero since nothing is stored in the array.

Here is an example:

SCRIPT
BEGIN
 Declare _vResult VarChar(2) Array[10];
 Declare _i       Integer    Default 1;

 while _i <= High(_vResult) do    --There is no High() function to give me the upper limit of the array
   set _vResult[_i] = 'x';
   set _i = _i + 1;
 end while;
END

TIA
Barry
Sun, Mar 30 2014 10:45 PMPermanent Link

Raul

Team Elevate Team Elevate


<<
Barry wrote:

I have two questions about uninitialized arrays.

1) Can I initialize an entire array to a value?
>>

Use the default clause when declaring:

  DECLARE vResult VARCHAR(2) ARRAY[10] DEFAULT 'xx';

<<
2) How can I loop through the array without knowing the upper limit of the array? Cardinality will be zero since nothing is stored in the array.
>>

You just declared the array so you would know the upper limit - wouldn't you ?

You can use CARDINALITY function but that one returns the highest referenced element. if you have default inited the array though then all elements have been referenced so you can write

SCRIPT
BEGIN
  DECLARE vResult VARCHAR(2) ARRAY[10] DEFAULT 'xx';
  DECLARE i Integer DEFAULT 1;
  
  SET LOG MESSAGE TO 'Max referenced element = ' + CAST(CARDINALITY(vResult) AS VARCHAR);
 
  while i <= CARDINALITY(vResult) do
   SET LOG MESSAGE TO CAST(i as VARCHAR) + ':' + vResult[i];
   set vResult[i] = 'x';
   set i = i + 1;
 end while;
END


Raul
Mon, Mar 31 2014 2:06 AMPermanent Link

Barry

Raul wrote:

>Use the default clause when declaring:

  DECLARE vResult VARCHAR(2) ARRAY[10] DEFAULT 'xx';
<

Great. I didn't know I could initialize the array in the Declare statement which sets the upper bound for Cardinality() function.

>You just declared the array so you would know the upper limit - wouldn't you ?<

I prefer to have the same code work even if the array size changes later on.
I don't like to have to update the array's upper limit in 2 different locations.


>You can use CARDINALITY function but that one returns the highest referenced element. if you have default inited the array though then all elements have been referenced so you can write<

Yes, that was what I'm looking for.
Thanks Raul

Barry
Image