Sign in to follow this  
Followers 0
leuce

How does one create an array?

4 posts in this topic

G'day everyone

Sorry for the silly question, but how does one create an array in which the [0] item shows the number of items in the array?

I know that when I use StringSplit, the [0] item is the number of items in the array, but when I create an array using e.g. "Local $foo[10]", then the [0] item is not the number of items in the array, but is in fact the first item in the array.

Alternatively, how can I find out what the number of items are in an array that was created using e.g. "Local $foo[10]" to which I had added more items in the array? Is this simple or complex?

Thanks

Samuel

Share this post


Link to post
Share on other sites



Hello leuce,

When creating your own arrays, and you want the number of items to show in $array[0] you will need to set it manually. If it contains the first item in your array, then you will need to change how you add the items so it starts at [1] rather than [0] or you will be overwriting the first element of your array when setting the array count number.

Ubound($array) will return the size of $array. this will count [0] as an item too. So if you want just the items from [1] to [10] you would manually create your count with this example below:

$array[0] = Ubound($array)-1

Personally I like my arrays to just contain the useful data, and when I find a need to check the array size, I call it with Ubound(), and assign it to a variable if need be.

Hope this Helps.

Realm


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

Personally I like my arrays to just contain the useful data, and when I find a need to check the array size, I call it with Ubound(), and assign it to a variable if need be.

Thanks, that was a useful answer.

Share this post


Link to post
Share on other sites

Thanks, that was a useful answer.

My Pleasure!

I noticed your using StringSplit and if you find it to be more useful to just have the data in your arrays, StringSplit has an option to start at [0] rather than [1] with an element count in [0].

StringSplit($myString, '|',2)

adding a 2 in your third parameter of StringSplit will return your array without the element count, starting the first element at [0] of your array.

Realm


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0