Sign in to follow this  
Followers 0
Freeze128

Redim Array By Variable

5 posts in this topic

I might be completely barking up the wrong tree here, so let me know if I am...

I'm trying to populate a multidimensional array with values that are coming from one string. The values are separated by a delimiter (like just a pipe |). I started writing a function to count the delimiters, and every time it hits one, it increases the element number. If it hits 2 delims in a row, that means increase the number of dimensions.

Well, it just occurred to me that I don't really know how I can programmatically redim an array with a number of dimensions that is stored in a variable. I mean, it's not like there is a function like:

CreateArray($elements, $dimensions)

The only reason I'm doing this is to populate a multidimensional array quickly with a static set of data. For example:

red::fire

blue::water

white::wind

green::earth

I was trying to avoid doing it like:

$array[1][1] = "red"

$array[1][2] = "fire"

$array[2][1] = "blue"

$array[2][2] = "water"

$array[3][1] = "white"

...

Is there any way I can just stream it into an array with a variable like:

$String="red|fire||blue|water||white|wind||green|earth"

Share this post


Link to post
Share on other sites



sounds like stringsplit()

$days = StringSplit("Sun,Mon,Tue,Wed,Thu,Fri,Sat", ",")

;$days[1]contains "Sun" ... $days[7] contains "Sat"

see help

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

The only reason I'm doing this is to populate a multidimensional array quickly with a static set of data.

[...]

Is there any way I can just stream it into an array with a variable like:

$String="red|fire||blue|water||white|wind||green|earth"

If you have the Beta version, try this:

Dim $String[3][2]=[[red,fire],[blue,water],[white,wind],[green,earth]]

look in the beta docs for the Dim function to get the syntax

Edited by blindwig

Share this post


Link to post
Share on other sites

sounds like stringsplit()

see help

8)

I did read up on StringSplit. While it's very clear how I could use that to populate a one dimensional array, I wasn't quite sure how I could use that to populate a multidimensional array. The help page makes no reference to anything other than a single dimension.

You know, it's kinda funny, I almost used that exact same example above, but decided against it because the problem was too easy to solve with a 1 dimensional array.

Share this post


Link to post
Share on other sites

If you have the Beta version, try this:

Dim $String[3][2]=[[red,fire],[blue,water],[white,wind],[green,earth]]

look in the beta docs for the Dim function to get the syntax

Beautiful! While it isn't quite as flexible as reading it from a string, I think the above syntax will do nicely.

Thanks a million!

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