Jump to content

Teach Me Arrays


Recommended Posts

Okay, so I have struggled with arrays for the last few months, and I don't feel like I have gotten much better in understanding them. So, I am hoping the community could pass on some knowledge to me. I would like to fully grasp this concept, so that I can spend less time posting questions, and more time posting answers. I want to become a larger contributer to the community.

Maybe this can even turn into a tutorial one day. Here is what I know of arrays, pasted from Wikipedia. What I need to know is how and when to use them. And how to identify, ahead of time, how and when to use them. I will have lots of questions as the 'post' goes on, but I just would like to start the dialogue.

Let's start with,

'What is an array?'

'How is an array used in AutoIt?'

Array

From Wikipedia, the free encyclopedia

In computer programming, an array, also known as a vector or list (for one-dimensional arrays) or a matrix (for two-dimensional arrays), is one of the simplest data structures. Arrays hold a series of data elements, usually of the same size and data type. Individual elements are accessed by their position in the array. The position is given by an index, which is also called a subscript. The index usually uses a consecutive range of integers, (as opposed to an associative array) but the index can have any ordinal set of values. Some arrays are multi-dimensional, meaning they are indexed by a fixed number of integers, for example by a tuple of four integers. Generally, one- and two-dimensional arrays are the most common.

Most programming languages have arrays as a built-in data type. Some programming languages (such as APL, some versions of Fortran, and J) generalize the available operations and functions to work transparently over arrays as well as scalars, providing a higher-level manipulation than most other languages, which require loops over all the individual members of the arrays.

Thanks in Advance!

Edited by litlmike
Link to comment
Share on other sites

Hi,

OKay I'll start. :D

'What is an array?'

Like explained above: It is a list (one dimension) a table (two dimension) ...

'How is an array used in AutoIt?' It is used like in every other langauge too.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Think of a basic Array as just a commonal everyday list of items: (1 single column of data)

This list (array) can hold anything you wish.

From names, phone numbers, coords, product codes, absolutely anything you want.

2d arrays (Multidimensional) are just like an Excel spreadsheet, many columns & many rows. These are a little more complex, but straight forward once you understand the single array fundamentals.

An element within an array is just like a Cell on the Excel spreadsheet.

Basic explanation

HardCopy

Contributions: UDF _DateYearFirstChildren are like Farts, you can just about stand your own.Why am I not a Vegetarian?...Well...my ancestors didn't fight & evolve to the Top of the food chain for me to survive on Salad

Link to comment
Share on other sites

  • Moderators

Hmm, the easiest way I can explain an Array is rather than having to have Multiple named variables, you creat elements (eg... [1] [2] [3] etc...) with one core variable name.

So...

;Not an array and could get messy if you don't know what your doing
$Var1 = 'Some Text'
$Var2 = 'Some More Text'
$Var3 = 'The end of my Text'

For $i = 1 to 3; Notice we have to know how many loops we are going to do
    MsgBox(64, 'Info1:', Eval('Var' & $i)); Have to use Eval in this instance or the loop is useless
Next

$Creat_A_1_Dimensional_Array = StringSplit('Some Text,Some More Text,The end of my Text', ',')
;Using StringSplit we don't have to declare global/local/dim (although I usually do)
;We also don't have to count how many elements because it's done for us
;StringSplit starts the elements at 1

For $i = 1 To UBound($Creat_A_1_Dimensional_Array) - 1; Using Ubound tells us the number of elements in the array which needs to be reduced by one to work
    MsgBox(64, 'Info2:', $Creat_A_1_Dimensional_Array[$i]);Using the brackets '[' number of count ']' we are able to get the element value
Next

Global $Array1[4];When creating a regular 1 dimensional or multi dimensional array without the use of functions you must declare them local/global/dim
$Array1[0] = 3;Elements we will use
$Array1[1] = 'Some Text'
$Array1[2] = 'Some More Text'
$Array1[3] = 'The end of my Text'
;Take note you "don't" have to use [0] in this case as the number of elements, you could actually give that a value
;But since most of the functions use the [0] to retain how many elements there are, I usually do

For $i = 1 To $Array1[0]; Note here I didn't use Ubound()
    MsgBox(64, 'Info3:', $Array1[$i])
Next

Global $Array2[4] = [3,'Some Text', 'Some More Text', 'The end of my Text'];With beta you can make making the arrays even easier by defining them this way
;Same rules apply as above

For $i = 1 To UBound($Array2) - 1; I could use $Array2[0] here, but showing you you can use Ubound() as well
    MsgBox(64, 'Info4:', $Array2[$i])
Next
Hopefully that helps a tad, and someone else can get into multi dimensional ones.

Edit:

Replaced code tags with Code AutoIt tags.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

You can use arrays to create a gui although...

Here is an example

#include <guiconstants.au3>

Global $Button[11][11] ;Declare the array 

GUICreate("" )

For $a = 1 to 10 ;Variable for button left
    For $b = 1 to 10 ;Variable for button right
        $Button[$a][$b] = GUICtrlCreateButton( "Button[" & $a & "][" & $b & "]" , $a * 20 , $b * 20 , 20 , 20)
    Next
Next

GUISetState( @SW_SHOW )

Do
    $msg = GUIGetMsg()
Until $msg = $GUI_EVENT_CLOSE
Edited by Daniel W.

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

You can use arrays to create a gui although...

Here is an example

#include <guiconstants.au3>

Global $Button[11][11] ;Declare the array 

GUICreate("" )

For $a = 1 to 10 ;Variable for button left
    For $b = 1 to 10 ;Variable for button right
        $Button[$a][$b] = GUICtrlCreateButton( "Button[" & $a & "][" & $b & "]" , $a * 20 , $b * 20 , 20 , 20)
    Next
Next

GUISetState( @SW_SHOW )

Do
    $msg = GUIGetMsg()
Until $msg = $GUI_EVENT_CLOSE
That's cool, now I have bad ideas.
Link to comment
Share on other sites

I have a question to add - how do you create a one or more dimensional array when you do not have the last number? Like you were to read a list of files and you are not sure how many files are going to come across.

EDIT - fixed spelling of dimensional

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

You can first get the number of files then Global the array and then add the elements to it..

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

You can first get the number of files then Global the array and then add the elements to it..

I have in the past tried to creat a variable to be able to change the subscript, but was unable - can you give a small example.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

if an array is like this:

Dim $array[3]oÝ÷ Ø   ÝÊ°j{m¡§]¶ayªëk+,7®±æ®¶­se&TFÒb33c¶'&µV&÷VæBb33c¶'&³Ð

where 1 is the number of elements you want to add

Edited by RazerM
My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

if an array is like this:

Dim $array[3]oÝ÷ Ø   ÝÊ°j{m¡§]¶ayªëk+,7®±æ®¶­se&TFÒb33c¶'&µV&÷VæBb33c¶'&³Ð

where 1 is the number of elements you want to add

I assume in your example that the subscript you are adding could be a varialbe? or even a call to a funtion like countFiles()?

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

I have a question to add - how do you create a one or more dimensional array when you do not have the last number? Like you were to read a list of files and you are not sure how many files are going to come across.

EDIT - fixed spelling of dimensional

check out, 'Ubound($avArray)'.

Link to comment
Share on other sites

Right from the help file

Dim $myArray[10][20]   ;element 0,0 to 9,19
$rows = UBound($myArray)
$cols = UBound($myArray, 2)
$dims = UBound($myArray, 0)

MsgBox(0, "The " & $dims & "-dimensional array has", _
    $rows & " rows, " & $cols & " columns")

;Display $myArray's contents
$output = ""
For $r = 0 to UBound($myArray,1) - 1
    $output = $output & @LF
    For $c = 0 to UBound($myArray,2) - 1
        $output = $output & $myArray[$r][$c] & " "
    Next
Next
MsgBox(4096,"Array Contents", $output)

is the second msgbox supposed to contain anything (text - $output) - on my pc it is a vertical empty box.

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

It should be empty because no data has been put in the array

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

It should be empty because no data has been put in the array

Then why is the variable given a value? I am now more confused then ever - but I am glad that I am not the only one. Sorry to steal your topic litlmike - but I think everyone can learn from the masters.

EDIT - I will be going home and logging on in about an hour and a half.

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Wow! Thanks everyone for the contributions.

@ gamepin126

Thanks, it's helpful to see an easy example. It is easier to pick apart.

@HardCopy

Okay, now we are talking a bit more my language. I can understand the Excel example, no problem.

@nitekram

No worries about 'stealing the topic', I think this is helpful for everyone here.

@Daniel W.

Very cool example, but I don't think I quite understand how to use it yet. I often try to make GUIs that need evenly spaced buttons, and I think your example will be quite useful in the future.

@SmOke_N

Thanks a ton for that explanation! That really helped to bridge the gap in my understanding.

After looking at gamepin and Smoke's examples, I have a few questions:

1a) Is $i used to notate any given 'element' in an array?

1b) What is it actually telling the compiler to do?

1c) Is there a time when I should not use $i?

2)The following examples do the same things, correct?

Global $Array1[4];
$Array1[0] = 3;Elements we will use
$Array1[1] = 'Some Text'
$Array1[2] = 'Some More Text'
$Array1[3] = 'The end of my Text'oÝ÷ Ù«­¢+Ù±½°ÀÌØíÉÉäÉlÑtôlÌ°ÌäíM½µQáÐÌäì°ÌäíM½µ5½ÉQáÐÌäì°ÌäíQ¡¹½µäQáÐÌäít

Thanks Again.

Edited by litlmike
Link to comment
Share on other sites

  • Moderators

$i is the variable that contains the number of the loop, $i can be any variable, you'll often see me use $iCount some use $y whatever it doesn't matter.

The loop is the For/Next

1 To 10 so each loop it passes from the For to the Next $i increases by 1

10 To 1 Step - 1 going backwards, now $i = 10 and will decrease by 1 every loop from the For to the Next.

Edit:

To answer your other question, yes both do the same thing, just the 2nd one that needs beta is easier in my opinion. It had been around a while, but Gary pointed it out to me a few months ago when I had no idea.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Make arrays part of your system. This will be really benefitial when you get into more advanced scripts. I'm using a 4-dimensional in my RPG project.

First dimension is the window ( 4 windows in 1 App )

Second is X position

Third is Y position

Fourth is File/Texture ( Data )

$Grid[$x][Int($Mouse[0]/50)][Int($Mouse[1]/50)][0]
Edited by Manadar
Link to comment
Share on other sites

Hi,

I have been studying/using arrays for a few years now, and they are very useful for doing some things. I like autoit very much, and I realise that compared to other programming languages like matlab or fortran, the support for arrays is pretty limited now. I am woring with randall c at the moment to extend the functionality, so that it will be possible to do more things with them.

Anyway... Arrays are useful when you need to work with a large amount of related information and you want to access it and process it in an efficient way.

The possibilities are only limited by your imagination really but i will try to give an example.

This is not something that you would do with autoit, but i think it is good to understand how two dimensional arrays work

For example if you wanted to do stuff with an image (black & white) you would access it as an array.

So if you divide the image into little black or white squares (pixels) you can define their colour using one value, usually 0 if the pixel is black and 1 if the pixel is white.

Then, you can define their position using two values: the first pixel in the top left could be in position [1][1] the nearby pixel towards the right would then perhaps be [1][2] and the nearby pixel towards the bottom would then be [2][1]... then you can give an [x][y] coordinate of each pixel in the image x counting pixels from top to bottom and y from left to right.

...........x.......x

...........|........|

...........v.......v

y-> [1][1] [1][2] ...

y-> [2][1] ...

y-> ...

So if the image is 640x480 the bottom right pixel would be [640][480] and the centre roughly [320][240]

So, you have your array

$image[x][y] 

oÝ÷ Ù©ÝÊj{É«­¢+ØÀÌØíÁ¥á±}¥¹}Ñ¡}ѽÁ}±Ñ¡¹}½É¹ÈôÀÌØí¥µlÅulÅt(ÀÌØíÁ¥á±}ѽ}¥ÑÍ}É¥¡ÐôÀÌØí¥µlÅulÉtoÝ÷ ÚØ^½©nzØZ·*.Â¥vØ^­Â¥u·µûaz±z³bµæ§wHnV*"*.Á©íyÛhz0­rhº»az±zX§¶¦×ezíè¦j¢³*.r¥uø§v­o'!yÉ"azö¥¹è§Ó~¢¨ßm6ãDáz'ò¢ì׶Ê,Ûaz±zX§¶¢Ûh¸ ÛhmæåiÉ2¢ì(ºW]¡«­¢+ØÀÌØí¥µlØÐÁulÐàÁtôÀoÝ÷ ÚÚºÚ"µÍÜ    ÌÍÞHHHÈPÝ[
    ÌÍÚ[XYÙKHHHÝB  ÌÍÚ[XYÙVÌÌVÉÌÍÞWHH^

I hope you get the idea.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...