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

Status
Not open for further replies.

flea333

Technical User
Sep 4, 2003
47
US
How do you find the amount of elements of an array?
 
When you created the array, you declare it's size.
Ex: string myarray[20] ;contain 20 elements
 
You can use the sizeof operator to get the size of the array in bytes. You could then divide by four (four bytes holds an integer or long) or eight (for a float) to get the number of elements. If the array contains strings, then the above won't work since strings are not a standard size in ASPECT.


aspect@aspectscripting.com
 
Yes tknight, but I don't want to have to change all these for loops throughout my code because I added an array element..thanks knob.
 
Flea333,

I didn't understand you question. You array must be type integer. I'm not able to figure out why you would be concerned in it's size?
 
Its not the size of the array, its the number of elements in the array.
 
As I understand it Aspect does not allow for truely dynamic arrays. So you really just have to:
1. Dimension your array plenty large when you first define it.
* Note that if you like to keep your variables locals like Computer Sci types push you'll run into a problem of limited memory space reserved by Procomm.
* The cure for that is to make large arrays global as global variables are sent to a less restricted space.

2. You could write your own quasi-dynamic allocation routine using the conditional compilation and compiling from within a running script.
(probably not worth the bother)


If you are interested in knowing how many elements of the array have been actively used at some point in your program that is something your own code will have to do.

A suggestion is to have a counter, integer variable, to increment decrement as array elements are utilized.
Another convention that you could implement for integer arrays would be to designate the element [0] as your counter for that purpose. Just need to dimension one larger than otherwise needed and use the array like it is [1] based. If you don't like having an offset between a 1 based convention and a zero based array this can make things clearer without any significant problems...Even if you don't track the array use using [0].


Another way to determine if elements are used would be to initialize all elements with a value that would not be otherwise used -1, 0, integer's maximum or minimum value (which would effectively reduce the max or min by one in that case) either define the value as NL or MT or make it a global variable of that value. Then you can refer to it much the same as EOF is referred to in file access or NULL is sometimes used when looking at strings.

ie.
#define MT -2147483648
myArray[10001] ; 10000 array elements using it [1] based

proc main
integer cntr
for cntr = 0 UPTO 10001 BY 1
myArray[cntr] = MT
endfor
myArray[0] = 0 ; no array elements not empty

if myArray[2] == MT
termwrites "myArray[2] is empty"
endif
endproc

hope these ideas helped / Happy Halloween DT
 
One more thought.

If you are just saying there is a number you have setup your script to accommodate a number of array elements say 20. Instead of hard coding the array size throughout,
try:

integer arrayLimit = 20

proc main

integer myArray[arrayLimit]
string myArrayDescriptns[arrayLimit]
integer myArrayResults[2*arrayLimit]

endproc

Then anywhere where you need an array based on the arraylimit size you just use arrayLimit.
Then if you need to make it bigger all you have to change is the arrayLimit variable and recompile.

Again hope this helps, DT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top