Page 1 of 1

How to identify array length in Basic

Posted: Mon Jan 12, 2004 9:45 pm
by girishoak
Hi,

I want to find out the length of an array is their any way to find out the array length in Basic. Is there anything like Ubound?

Also wanted to know can I return an array as an output of routine.
and how to read this array.

Thanks in advance

Girish Oak

Posted: Mon Jan 12, 2004 10:23 pm
by kcbland
A dynamic array is a text string. Using LEN(array) will return the entire number of characters in the array, including @AM characters which delimit single dimension array elements.

So, experiment with a DS function using the following code:

Code: Select all

A=""
A<-1> = 1
A<-1> = 2
A<-1> = 3
A<-1> = 4

ElementCount = DCOUNT(A, @AM)
LengthOfA = LEN(A)
Ans = "Element Count ":ElementCount:" Length of A ":LengthOfA
Yes, you can return an array. Use the above experiment and you'll see that A is passed back as array. You will have to be aware that it will look like:

Code: Select all

1@AM2@AM3@AM4@AM  (where @AM is ASCII CHAR 254)
If you would rather return it as a delimited string, do a:

Code: Select all

CONVERT @AM TO "|" IN A
This replaces the @AM characters with a pipe, effectively making it a pipe delimited array. Also, you can reverse a delimited string an array using:

Code: Select all

CONVERT "|" TO @AM IN A

Posted: Tue Jan 13, 2004 12:00 am
by ray.wurlod
The size of a dimensioned array is returned by the INMAT() function in DataStage BASIC. This returns two values (the extents of each of the two dimensions) separated by a value mark (@VM).

Code: Select all

Dimension Rainfall(3,12)
Print INMAT(Rainfall)   ; * prints 3}12
Dimension ColumnCollection(8)
Print INMAT(ColumnCollection)   ; * prints 1}8
Dynamic arrays can be converted to dimensioned arrays using the MATPARSE statement.
Dimensioned arrays can be converted to dynamic arrays using the MATBUILD statement.
You should rarely, if ever, need either of these statements in DataStage BASIC.

As Ken said, the number of elements at a particular level (fields, values or sub-values) can be obtained using the DCOUNT() function. Where you have more than one kind of delimiter this is a bit trickier - it depends on exactly what it is you want to know.