square65

How do you declare a 2d array?

25 posts in this topic

I know this seems like a question that could easily be handled by the documentation.

Amazingly it doesn't. And googling returns nothing. Not a single person doing this or asking how to do this.

I've tried:

$test[][] = 0

$test[][] = [[]]

 

And a ton of other variations on this.

Share this post


Link to post
Share on other sites



I've figured out how.

You need to preface your array declaration with a scope specifier.

A la:

Local $test[][]

Global $test[][]

Since this is covered nowhere, hopefully when a good programmer is asking how to declare an array without initializing it to arbitrary values, they will come across this post.

Share this post


Link to post
Share on other sites

If only there was a help file

 

Language Reference - Variables

...

Before you can start using Arrays in your script, you must define their bounds using the 'Global/Local/Static' keywords and define the bounds or assign some of the elements.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I agree that arrays are a little nebulous in the au3 documentation.  Here's a little excerpt from the help that clearly shows the mechanism (including the 0-based indexing):

#include <Array.au3>

Local $aArray_Base[10][3]

For $i = 0 To 9
    For $j = 0 To 2
        $aArray_Base[$i][$j] = $i & " - " & $j
    Next
Next

_ArrayDisplay($aArray_Base, "2D - Original")

The best long-term solution would be additions to the help that are clearly labeled as 1-dimensional, 2-dimensional and, say, 3-dimensional arrays ... as separate topics.

Edited by qwert

Share this post


Link to post
Share on other sites

From the Helpfile: Language Reference - Variables

 

Before you can start using Arrays in your script, you must define their bounds using the 'Global/Local/Static' keywords and define the bounds or assign some of the elements.
 

 

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

A good programmer wouldn't knock a language they don't know.. ;)

EDIT: Let me google that for you

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

square65,

There is always the Arrays tutorial in the Wiki which is very comprehensive. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

If only there was a help file

Lmao.

If only consulting a page like:

https://www.autoitscript.com/wiki/Arrays

Was sufficient for just declaring a basic array.

Melba23, that article, which is actually the one I consulted before this post, does not explicitly state you need to give the scope specifier. It just shows that in an example. But you can give any variable that specifier and generally don't need to therefore it makes sense to ignore it in that tutorial.

MikahS, as I said, I consulted that and for logical reasons did not realize the scope specifier was absolutely necessary. I am a good programmer. Just not necessarily in this language yet. But I am a better programmer than you. Also the script I wrote last week, the first week I'd ever even heard of Autoit, is probably more sophisticated than anything you could write. It does full blown image processing, edge detection, and a pseudo image recognition system.

Thanks for the condescending LMGTFY though. Really showed that you lacked the intelligence to recognize that my question was actually legitimate and that obviously, based on my intelligent wording, I'd already consulted that page.

Thanks for the kind meaning advice from three of you. And I hope you learned your lesson, the condescending two of you.

I have a way of putting people in their place when they step out of line professionally. I do it in my job and I have the most productive IT department across every branch of my company. Hope you learned something :)

*snaps fingers, winks, spins 360 degrees and walks away*

Lock this thread please.

Share this post


Link to post
Share on other sites

You have done nothing of the sort,  word walls in coding forums should probably include actual code, otherwise it is nonsense.   Tell us more about this awesome first project......that obviously doesnt use arrays.

1 person likes this

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

It uses arrays :)

Maybe you misunderstood this post.

Share this post


Link to post
Share on other sites

In fact it uses arrays of an arbitrary declared-at-runtime number of dimensions. Ever even heard of something like that?

Share this post


Link to post
Share on other sites

Are you going to post it or keep running your mouth?


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Let me be clear. I'm a civil guy. But long time members on forums develop a VERY condescending attitude to newbies.

I have never abided that. And I never will. You guys got what you asked for.

:)

Edited by square65

Share this post


Link to post
Share on other sites

Flame me if you want, but I have to speak up, here:

The content and tone of the posts on this topic do a real disservice to the OP, the community and the AU3 language.

A LOT of people here are beginners.  The help file is a sprawling and continually-evolving work ... and sometimes the answer isn't obvious to even a veteran of the language.  (In 6 years, I've had my share of those experiences.)

Anyone who doesn't feel like answering a simple question should refrain from posting.  Someone else will probably chime in with a meaningful response.  I don't see the value of "snips".   And they're as unsightly on a forum as litter is on a roadside.

 

Share this post


Link to post
Share on other sites

Thanks. I feel the same way. I appreciate the answers I got.

Share this post


Link to post
Share on other sites

And everyone please take a deep breath before someone oversteps the mark (which has already been approached pretty closely) and I have to put on my Mod hat... :)

square65,

I agree that the documentation does not appear to have a note of the requirement for arrays to be scoped on declaration in all cases - which makes me wonder why no-one has ever raised the point before. Perhaps everyone else did "realize the scope specifier was absolutely necessary", despite not being as good a programmer as your good self. ;)

I will add something to the "Array" section of the "Language Reference - Variables" page and to the "Arrays" Wiki tutorial for the enlightenment of future searchers. :)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Hmm refering to post #2  I'm interested in declaration of empty arrays

I'm running the 3.3.12.0 release

Global $test[][]
Global $test[^ ERROR

Error: Variable subscript badly formatted.

But this works

#Include <Array.au3>

Global $test[][] = [[1, 2], [3, 4]]
 _ArrayDisplay($test)

So IMHO this thread is not completely useless (forgetting messages > #2)

Edit

And I could find nowhere in the helpfile a mention about how to declare an empty array using this kind of syntax  :)

Edited by mikell

Share this post


Link to post
Share on other sites

Declaring a variable like this creates a Map type variable, in 3.3.13.x beta versions.

Global $Test[]

Going above 1 dimension means you HAVE to declare the array with something in it if you don't say how many elements are in the dimensions. If you want your array to BE an array with one dimension, then you have to include the size in the declaration statement.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

BrewManNH,

Thanks, I really thought about something like this

Your message should have been #3  :D

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