Skip to content

lenarray

Evaluates the size or number of dimensions of an array.

Syntax

ir = lenarray(karray[, iwhich])
kr = lenarray(karray[, iwhich])
ir lenarray karray[, iwhich]
kr lenarray karray[, iwhich]

Initialization

karray is the array to query. It may have any number of dimensions. Despite the argument name, lenarray accepts arrays of any element type.

iwhich -- selects which dimension to evaluate the size. If zero or negative it selects the number of dimensions. It defaults to 1, as used in a vector.

Performance

kr -- length of vector.

karray -- array to query.

If the dimension requested is larger than the actual array, or the array is not initialised the value -1 is returned.

Replacing lentab

lentab is the deprecated name for queries on k-rate arrays. Change kLength = lentab(kArray, 1) to kLength = lenarray(kArray, 1). The same change works with an i-rate result.

Keep the dimension argument. It starts at 1 and defaults to the first dimension. The default does not return the total number of elements in a multidimensional array.

The k-rate lenarray form also runs at initialization. The old k-rate lentab form runs only during performance. Initialize the array before querying its shape.

Examples

Here is an example of the lenarray opcode. It uses the file lenarray.csd.

Example of the lenarray opcode.
<CsoundSynthesizer>
<CsOptions>
-n -m128 ;no sound output, reduced messages
</CsOptions>
<CsInstruments>
;example by joachim heintz
sr = 44100
ksmps = 32
nchnls = 1
0dbfs = 1

  instr 1 ;simple example
kArr[]  fillarray 1, 2, 3, 4 ;fill array manually
        printks "Length of kArr = %d\n\n", 0, lenarray(kArr) ;print out its length
        turnoff ;only do this in the first k-cycle
  endin

  instr 2 ;random array length
iNumEls random  1, 11 ;create random number between 1 and 10
kArr[]  init    int(iNumEls) ;create array of this length
        printks "Random length of kArr = %d\n", 0, lenarray(kArr) ;print out
        turnoff
  endin

  instr 3 ;fill random array length with random elements
iNumEls random  1, 11 ;create random number between 1 and 10
kArr[]  init    int(iNumEls) ;create array of this length
        printks "Random length of kArr = %d\n", 0, lenarray(kArr) ;print out

;fill
kIndx   =       0 ;initialize index
  until kIndx == lenarray(kArr) do
kArr[kIndx] rnd31 10, 0 ;set element to random value -10...10
kIndx   +=      1 ;increase index
  od

;print
kIndx   =       0 ;initialize index
  until kIndx == lenarray(kArr) do
printf("kArr[%d] = %f\n", kIndx+1, kIndx, kArr[kIndx])
kIndx   +=      1 ;increase index
  od

        turnoff
  endin
</CsInstruments>
<CsScore>
i 1 0 .1
i 2 0 .1 
i 2 0 .1
i 3 0 .1
</CsScore>
</CsoundSynthesizer>

See also

Array opcodes

Credits

Author: John ffitch
Codemist Ltd
2013

New in version 6.00

Extended to multi-dimensions in 6.04