Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Length 1

Status
Not open for further replies.

LordSpawn

Programmer
Feb 18, 2004
4
US
Hi everyone!

Here's my problem. I am trying to get the row/col of a single OR multidimensional array. Just like the ALEN() Function for Recital/FoxPro. Here is an example:

private array1[10]
private array2[1,5]

array1[1] = "This string has a len of 33 chars"
array1[2] = "woot!"
array2[1,1] = "test test test"
array2[1,2] = "test test test"

LEN(array2) && will equal 1
LEN(array2[1]) && will equal 5. Prefect. just what i want.

LEN(array1) && This will equal 10
LEN(array1[1]) && since this is a one dimensional array it will give me the len of the element 33. I want the len of the number of columns in the array.. for a single dim it would always be 0. I want this because im not sure if the array is a one or multi array.

Any help would be awsome. Im just looking for a compareable functon for Clipper/FlagShip from FoxPro/Recital. Thanks in adv!
 
You should also considder using ValType() on the arrayelement, and if it's an array type ("A") take the length to find the number of elements in the (sub-)array

like:
Code:
private array[3,3]

array[1,1] = 'test'
array[2] = 'another test'
array[3,1] = 'third test'

? valtype(array[1]) (Should give 'A')
? valtype(array[2]) (Should give 'C')
? valtype(array[3]) (Should give 'A')
? len(array[1]) (Should give '3')

HTH
TonHu
 
Yeah, that would work but not in all cases. What if the one dim array had an array in the first element?

What im trying to do here is take a bunch of memory vars and load them into an array. Then i want to display them. If one of the memory vars should be an array i want to display the entire array but i need both dimensions for the counter.

private array1[3,3]
private array2[4]

array2[1] = array1
len(array2[1]) is where i hit my problem.
 
Ok,

Code:
local Array[3,3]

array[1,1] = 'String'
array[1,2] = 3
array[1,3] = 'longer string'
array[2,1] = {'abcde'}
array[2,2] = 'lower'
array[2,3] = Date()
array[3]   = 123456

function ShowArray(Arr_,Lvl_,Lv_)
local n
default Lvl_ to 1
default Lv_ to 1

if valtype(Arr_) = 'A'
  for n := 1 to len(Arr_)
    ShowArray(Arr_[n],Lvl_ + 1)
  next
else
  ? Space(Lvl_ * 2) + '>'
  do case
  case valtype(Arr_)$'CM'
    ?? Arr_
  case valtype(Arr_) = 'N'
    ?? LTrim(Str(Arr_))
  case valtype(Arr_) = 'D'
    ?? DtoC(Arr_)
  case valtype(Arr_) = 'L'
    ?? Iif(Arr_,'.T.','.F.')
  case valtype(Arr_) = 'B'
    ?? '{||codeblock}'
  endcase
endif

Return(nil)
(Untested code!)
That should explain the trick ;-)

HTH
TonHu
 
That is almost exacly what i was looking for. I also wanted it to display what array it was coming from. Im not sure I can modify your code to do that.

The out put would look like this:
array[1,1] = array2
array[1,2] = 3
array[1,3] = 'longer string'
array[2,1] = {'abcde'}
array[2,2] = 'lower'
array[2,3] = Date()
array[3] = 123456

I still want to create a function that will give me the number of rows,columns of an array. If it's a single dim array it will display 0 for number of columns. Do you think you can help me out a little more? heres my shot at the function. works in most cases.

FUNCTION alen2
PARAMETERS palenarr,palenpos
IF LEN(palenarr) # 0
STORE LEN(palenarr) TO nalenrow && Number of rows
STORE LEN(palenarr[1]) TO nalencol && Number of columns
STORE .T. TO alenmulti
STORE 0 TO i0,i1,i2

IF valtype(palenarr[1]) = "A" && May be multidimentional
STORE 1 TO i1
DO WHILE i1 <= LEN(palenarr) && First dim
STORE 1 TO i2
DO WHILE i2 <= LEN(palenarr[1]) && Second dim
x2 = &quot;palenarr[i1,i2]&quot;
IF valtype('&x2') # &quot;A&quot;
++i0
ENDIF
++i2
ENDDO
++i1
ENDDO
IF LEN(palenarr[1]) # i0
alenmulti = .F.
ENDIF
ELSE
alenmulti = .F.
ENDIF
IF alenmulti = .T.
DO CASE
CASE palenpos = 0 && Number of elements
nalenret = nalenrow * nalencol
CASE palenpos = 1 && Number of rows
nalenret = nalenrow
CASE palenpos = 2 && Number of columns
nalenret = nalencol
ENDCASE
ELSE
DO CASE
CASE palenpos = 0 && Number of elements
nalenret = nalenrow
CASE palenpos = 1 && Number of rows
nalenret = nalenrow
CASE palenpos = 2 && Number of columns
nalenret = 0
ENDCASE
ENDIF
ELSE
nalenret = 0
ENDIF
RETURN nalenret
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top