Sign in to follow this  
Followers 0
weasel127

Help with Arrays

11 posts in this topic

Hi, I'm trying to make a program that deals with splitting strings and storing them to arrays.

First, I need to split a string into StringA and StringB. So my code looks like this:

$String = StringSplit("StringA,StringB",",")
For $n = 1 to $String[0]
    MsgBox(0,"String", $String[$n])
NextoÝ÷ Ù8Z·¥ÈhÃc,1zËÊ)àßÔ­®)àZÝýJÚâwôÚ0#§¶Ú,¦X­¶¬²Úâ(騭觶7è·­µêì×[hí©ÝÓ~zÛ^®Íµ¶æjH§Ø^Ê'µ¨§Ó~zÛ^®Íu

When I tried running this code, I got an error "Array variable has incorrect number of subscripts or subscript dimension range exceeded.: "

I don't understand why the ArrayDisplay would work, but the individual MsgBoxes would not.

Share this post


Link to post
Share on other sites



The problem is that you're using a 2 dimensional array here:

For $b = 1 to $Letters[1][0]

while your array is 1 dimensional

$Letters[$n] = StringSplit($String[$n],"")

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

But if I do a StringSplit, then shouldn't the array turn into 2 Dimentional?

So this

$Letters[1] = StringSplit($String[1],"")

would become $Letters[1][1 to 7] ?

Share this post


Link to post
Share on other sites

Well ... not really because you declared your array as being 1 dimensional

Global $Letters[$String[0] + 1]

My guess it is that you will need to put $Letters[1] into another variable and to tweak a little bit your script.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Hi, I'm trying to make a program that deals with splitting strings and storing them to arrays.

First, I need to split a string into StringA and StringB. So my code looks like this:

When I tried running this code, I got an error "Array variable has incorrect number of subscripts or subscript dimension range exceeded.: "

I don't understand why the ArrayDisplay would work, but the individual MsgBoxes would not.

You entered the weird, wonderful world of arrays stored in arrays... :rolleyes:

You are storing the entire array returned by StringSplit() in a single element of the $Letters array. $Letters is still a 1D array. The array stored in one of its elements CANNOT be accessed directly. It must read out to its own varible ($avTemp in mine) first, and then used.

The working code:

#include <Array.au3>

$String = StringSplit("StringA,StringB",",")
For $n = 1 to $String[0]
    MsgBox(0,"String", $String[$n])
Next

Global $Letters[$String[0] + 1]

For $n = 1 to $String[0]
$Letters[$n] = StringSplit($String[$n],"")
Next

For $n = 1 to $String[0]
    $avTemp = $Letters[$n]
    For $b = 1 to $avTemp[0]
        MsgBox(0,"Letters",$avTemp[$b])
    Next
Next

Record this on your iPod and play it back to yourself all night as you sleep... "Arrays are our friends, arrays are our friends, arrays..."

:rambo:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Another working version of your code - lol

#include <Array.au3>

$String = StringSplit("StringA,StringB",",")
For $n = 1 to $String[0]
    MsgBox(0,"String", $String[$n])
Next

For $n = 1 to $String[0]
    $Letters = StringSplit($String[$n],"")
    For $k=1 to $Letters[0]
        MsgBox(0,"Letters",$Letters[$k])
    Next
Next

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Sorry, I don't understand what you mean by put it into another variable. Now I tried declaring it as a 2 Dimensional array but just running this gives me an error.

#include <Array.au3>

$String = StringSplit("StringA,StringB",",")

Global $Letter[$String[0] + 1][8]   ;Here I try declaring it 2 dimensional

For $n = 1 to 2
$Letter[$n] = StringSplit($String[$n],"")   ;Now I try to store it into $Letter[1][1 to 7] and $Letter[2][1 to 7]
Next

I don't really understand using arrays, all I have figured out is that if you want an array $Array[1] to $Array[7], then you declare $Array[8] instead if $Array[7], so that's why I declared it as Global $Letter[$String[0] + 1][8]

Share this post


Link to post
Share on other sites

Look at the previous posts :rolleyes:


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Thank you for the code, but I need to store all the data into some variable, but that code will overwrite the data after each run. I think the problem is how I declare the arrays because I'm not sure how you would declare 2 dimensional arrays and then store information into it.

EDIT: Here is an example of the problem:

$String = StringSplit("StringA,StringB",",")
;So now after this the data is:
;String[1] = 'StringA'
;String[2] = 'StringB'

Global $Letter[2][8] ;Now declaring so I can put data into $Letter[1][1 to 7]


$Letter[1] = StringSplit($String[1],"") ;Now I am just testing storing $Letter[1][1 to 7] but I get an error
Edited by weasel127

Share this post


Link to post
Share on other sites

Sorry, I don't understand what you mean by put it into another variable. Now I tried declaring it as a 2 Dimensional array but just running this gives me an error.

Look at my working code. I only changed the final loop.

I don't really understand using arrays, all I have figured out is that if you want an array $Array[1] to $Array[7], then you declare $Array[8] instead if $Array[7], so that's why I declared it as Global $Letter[$String[0] + 1][8]

A 1D array is just a numbered list, and it is numbered from 0 (zero-based is the term of art). To list eight items, zero-based, on pen and paper, you would number them 0 thru 7. A 1D array is just like that.

Sometimes, like with StringSplit(), the first entry [0] is used to store the count, so you need one extra element. To store eight items this way, you declare the array with an extra element:

Dim $Array[9]
$Array[0] = 8 ; Count

A 2D array is just row/column table, like any spreadsheet. Displaying a 2D array using _ArrayDisplay() makes this obvious.

It is a strange piece of trivia that AutoIt allows you to store an entire array inside one element of another array. Your code did that (entirely by accident, I gather). I was just trying to explain what happened, and how that technique could be made to work.

:rolleyes:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thanks enaiman and PsaltyDS on clearing up how to use arrays properly, I think I understand how to use arrays correctly now.

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