Sign in to follow this  
Followers 0

Array declaration

39 posts in this topic

Posted

Hi,

I have recently started using arrays with AutoIt.

One thing that I'd love to see is an easier syntax for Array definition.

Today (please correct me if I am wrong) if I want to define a 10 element array I have to assign the value of each array element separatelly. For instance:

Dim $v_array[10]

$v_array[0] = 1

$v_array[1] = "hello"

$v_array[2] = 3.14

...

$v_array[9] = "last value"

Although this is clear it is very verbose and a bit annoying.

Perhaps a better and still pretty clear way would be to do something like:

Dim $v_array[10]

$v_array = [1, "hello", 3.14, ... , "last value"]

Or even:

Dim $v_array = [1, "hello", 3.14, ... , "last value"]

There would be no need to specify the array length when defining it this way.

For multi line arrays we could do something like:

Dim $v_matrix = [1, 6, 2.3; -1, "hello", 4];

That is, simply use a semicolon to separate between different lines.

This would make it very easy to define lists, and dictionaries, like an error number/message list. For instance:

Dim $v_error = [ 0, "no error";

-10, "file not found";

-30, "unknown error];

Finally, it would be really cool if it was possible to use this same sintax with subindexes. For instance:

Dim $v_users = ["joe", "ann", "mathew", "andrew"];

Dim $v_indexlist = [1 3 4];

Then doing:

$v_male = $v_users[$v_indexlist]

would put "joe", "mathew" and "andrew" in $v_male

What do you think? AutoIt makes programming easy. This would make programming with arrays very easy! :-)

Cheers,

Angel

Share this post


Link to post
Share on other sites



Posted

Thanks Larry,

however this will only work for string arrays, right?

I still think that AutoIt deserves a more flexible and intuitive array declaration/indexing. Are you satisfied with the current one? I think that many fancy things, like lists and stuff like that could be done much more easily with a simpler syntax.

Cheers,

Angel

P.S.- Thanks again Larry, I will use your solution whenever I will not be using numbers in my arrays :-)

Share this post


Link to post
Share on other sites

Posted

Yes, I see that I could already do what I want (lists, etc) with AutoIt, although perhaps I lack the imagination to become a uber-scripter as you say! :)

The problem is that it is not very good when the language gets "on the way" of what you want to do. As AutoIt is so easy for doing almost everything, the array declaration and addressing seems a bit clunky in comparison.

For instance, if I want to declare an array of 20 numbers, each of them different (perhaps some kind of password or key), it would be much, much more simpler to do:

Dim $v_array = [2.4, -1.2, 4, 1.2, ... -12.3]

or at even

Dim $v_array[20] = [2.4, -1.2, 4, 1.2, ... -12.3]

Than doing:

Dim $v_array[20]

$v_array[0] = 2.4

$v_array[1] = -1.2

...

...

[20 lines later!]...

$v_array[19] = -12.3

The 1st syntax would be by far simpler, shorter and clearer than the current syntax!

Now just imagine that you had to change the order of the elements in your array. Lots of reordering of the declaration code would be needed to keep the code readable. And I don't even want to imagine how hard and long would it be to define a 3x20 array! I would probably end up creating a comma separated file and loading it or something... but if the code must remain private...

Perhaps I am so used to program in Matlab that I miss how easy is to declare and use arrays in it. But I'd definetly love to see this particular part of the AutoIt language improved. Heck, even in C you can declare arrays in this way...

Actually, if I could make a wish list, I would also add native array sum, substraction, product and division (element by element only). But I guess that that is the matlab geek in me talking :)

And what is up with the name of the UBound function? It took me at least 10 minutes to find out that there is no Length or Len os Size function in AutoIt, and that it is called UBound instead! I suppose that it is the same as in VB but I'd vote for adding a UBound wrapper called Length or Size just for the newbies shake!

Cheers,

Angel

Share this post


Link to post
Share on other sites

Posted

Here's a "StringSplit" that converts numbers to numbers....

$myArray = _MySplit("1918,Red,2004,Sox", ",")

For $i = 1 to $myArray[0]
   MsgBox(4096,"Elt:" & $i, $myArray[$i])
Next
Exit

Func _MySplit($arg, $sep)
   Local $i, $a = StringSplit($arg, $sep)
   For $i = 1 to UBound($a)-1
      If IsNumber($a[$i]) Then $a[$i] = Number($a[$i])
   Next
   Return $a
EndFunc

Share this post


Link to post
Share on other sites

Posted

Yes, if UBound is the standard VB function name I can understand why it was used, but still it was not very easy to find on the documentation.

Regarding declaration of arrays values I do not understand what you mean. Do you mean that it would get complicated with the current syntax? If that is so I must agree with you, of course! :-D

CyberSlug, thanks for the function. Actually I just did my own following Larry's suggestion and it looks very similar to yours. :) Thanks!

However, this seems like a pretty convoluted way of achieving a pretty simple objective: declaring an array.

If C can do it (albeit only with numbers), how comes that AutoIt cannot? (I am referring to the square bracket syntax ala $v = [1, 3, 7]).

If I might appeal to a higher authority, :), Jon, what do you think?

Nobody else would like to see an easier way of declaring/accessing arrays?

Angel

Share this post


Link to post
Share on other sites

Posted

I see nothing wrong with adding a new way to define arrays, but we have a way already. The problem is as Larry brought up, what do you do with multi-dimension arrays? There is just no good answer. Maybe going to Jon would be a good idea, but as Larry said before "What gets put into AutoIt is a popularity contest." Find the support and maybe something will get added.

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

Posted

Matt,

what is the problem with multidimensional arrays? It would be much more simple with the syntax I propose. For a 2 dimensional array you'd do:

Dim $v[2][3] = [1 2 3; 4 5 6]

For a 3 dimensional array:

Dim $v[2][2][3] = [[1 2 3; 4 5 6]; [2 -1 3; 1 0 0]]

How would you do something like this with the current syntax? It would require nothing less than 12 statements!

Also this syntax can be used even for 4 dimensional arrays, etc.

By the way, this is a very standard syntax, used in many engineering languages (like Matlab). Even perl has a similar syntax.

But as you said it all depends on whether other people find this interesting, and on whether the developpers like the idea of course! :)

Cheers,

Angel

I see nothing wrong with adding a new way to define arrays, but we have a way already. The problem is as Larry brought up, what do you do with multi-dimension arrays? There is just no good answer. Maybe going to Jon would be a good idea, but as Larry said before "What gets put into AutoIt is a popularity contest." Find the support and maybe something will get added.

*** Matt @ MPCS

<{POST_SNAPBACK}>

Share this post


Link to post
Share on other sites

Posted

I was been lurking for a time since I found about AutoIt 3 weeks ago (this language I like a lot! thanks!), but this thread make me register in forum!

I also use Matlab sometime and I like this syntax for definition of arrays. It is very useful! It will be great if this is integrated in this great language! I hope it is.

I want to thank the makers of AutoIt for this language so useful again!

Manuel

Share this post


Link to post
Share on other sites

Posted

But as you said it all depends on whether other people find this interesting, and on whether the developpers like the idea of course! :)

<{POST_SNAPBACK}>

Hi Angel,

although I'm not a matlab-user :) I would support your request for defining arrays in that way. As you mentioned It would make the source code much simpler, shorter and clearer.

Share this post


Link to post
Share on other sites

Posted

Hi Angel,

although I'm not a matlab-user :) I would support your request for defining arrays in that way.  As you mentioned It would make the source code much simpler, shorter and clearer.

<{POST_SNAPBACK}>

I also suppor this request. :)

Share this post


Link to post
Share on other sites

Posted (edited)

Regarding Angel's proposal (taken from two posts):

Dim $v_array = [2.4, -1.2, 4, 1.2, ... -12.3]or at even

Dim $v_array[20] = [2.4, -1.2, 4, 1.2, ... -12.3]

For a 2 dimensional array you'd do:

Dim $v[2][3] = [1 2 3; 4 5 6]

For a 3 dimensional array:

Dim $v[2][2][3] =  [[1 2 3; 4 5 6]; [2 -1 3; 1 0 0]]

<{POST_SNAPBACK}>

Since I'm most familiar with C I'd prefer C syntax, but working from the proposal I'd change it as follows:

For a 1 dimensional array (same as one of Angel's proposals):

Dim $v[3] = [1, 2, 3]

For a 2 dimensional array:

Dim $v[2][3] = [[1, 2, 3], [4, 5, 6]]

For a 3 dimensional array;

Dim $v[2][2][3] = [[[1, 2, 3], [ 4, 5, 6]], [[2, -1, 3], [1, 0, 0]]] (edit: left out a '[')

I know, I'm making it very C'ish. The point is that a multi-dimensional array is really an array of arrays and, since the elements in a 1 dimensional array are contained in square brackets, to be consistant the 1 dimensional arrays that are elements of a multi-dimensional array should continue to be contained in square brackets. Likewise, I've used commas to separate the elements in a 1 dimensional array and also used commas to separate the 1 dimensional arrays in a 2 dimensional array and used commas to separate the 2 dimensional arrays in a 3 dimensional array. Semi-colons could replace the comma's but I'd like to see a single way to contain array elements and a single way to separate elements.

That said, I know I'd get used to whatever the syntax ends up being (if it does end up being). The main point, having an easy method to intialize arrays, is excellent.

john925

Edited by john925

Share this post


Link to post
Share on other sites

Posted

What about improving StringSplit allowing select more chars and in the order they are wrote split the string in a multi dimensional array?

Eg:

$sString = "c:\" & @lf & "my homedrive" & @cr & "e:\games" & @lf & "my games folder"

$a = NewSplit($sString,@lf & @cr)

will create a[3][2] array with in a[0][0] and a[0][1] the numbers of element in the 'rows'.

Share this post


Link to post
Share on other sites

Posted

@ezzetabi

I love the idea.

It would be better to seperate the chars like this:

$sString = "c:\" & @lf & "my homedrive" & @cr & "e:\games" & @lf & "my games folder"
$a = NewSplit($sString, @lf, @cr)

Share this post


Link to post
Share on other sites

Posted

@ezzetabi, I think that is on the ToDo list... or maybe I just read it in a post. I think someone was working on that. I don't know if it splits it multidimensional but they were working on multiple character stringsplit.

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

Posted

Well. I made a udf for this in the past. Maybe you read that? :)

Share this post


Link to post
Share on other sites

Posted

That very well could be it. I just remember the topic, it was a while back now.

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

Posted

@ezzetabi, I think that is on the ToDo list... or maybe I just read it in a post. I think someone was working on that. I don't know if it splits it multidimensional but they were working on multiple character stringsplit.

*** Matt @ MPCS

<{POST_SNAPBACK}>

Correct..

The TODO list is a sticky at the top of the developers forum.

This is in it for stringsplit: StringSplit() full string delimiter option

Share this post


Link to post
Share on other sites

Posted

Alright, I knew I read it a couple places. I am not quite sure what that means. Is that what we are talking about here? Or is it allowing full words to delimit string lists?

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

Posted (edited)

Hey you all!

That is different. ATM StringSplit accept more than one char as delimeter but doing that will split for every char.

E.g.

$e = "I am a string"

$a = StringSplit($e,' s')

would become

$e[0]=5

$e[1]='I'

$e[2]='am'

$e[3]='a'

$e[4]='' ;for the dividing 's'

$e[5]='tring'

Jon wants to allow users using a string as delimeter.

$e = 'I am a string'

$a = StringSplit($e,'am')

Will become:

a[0]=2

a[1]='I '

a[2]=' a string'

I am pretty sure. At least it was so sometime ago.

Edit: typo compiling by mind is not easy :)

Edited by ezzetabi

Share this post


Link to post
Share on other sites

Posted

Ezzetabi,

I don't think that this really solves the underlying problem, which is de definition of multidimensional numeric arrays.

How would you use split string to define something like:

Dim matrix[3][2] = [1 2 4; 2 4 8] ? (or [[1 2 4], [2 4 8]] if we used C-like syntax)

And what about a 3 dimension array (like Dim [3][2][5])?

But the worst part is that it would NOT work for numeric arrays. You would simply split a string into an array of strings.

Finally, it would be very weird for a novice to need to use a string to define an array!

Compared with the elegance of the rest of AutoIt3 this would feel like a hack to overcome a limitation of the language.

We can use a for loop and convert every element to a number of course, but as I said my idea was to simplify the scripts by defining a simple array definition syntax instead of using loops, strings, etc.

Cheers,

Angel

Share this post


Link to post
Share on other sites
Sign in to follow this  
Followers 0