Jump to content

define 2d array in a for loop


Recommended Posts

I'm working on a script and I'm trying to define a 2d array inside a for loop.

Is it possible to do it?

Eg: 

For $i = 0 to 50
    local $array[50][2] = [[$i,$var[$i]]
Next

If this is possible how would I get the information from the array?

Something like: $array[5][1]?

I'm really confused on this and any help will be awesome.

(If you can not do this how would I go about doing it a different way?)

Thanks in advance!

Link to comment
Share on other sites

#include<array.au3>

local $array[50][2]
For $i = 0 to ubound($array) - 1
     $array[$i][0] = $i
     $array[$i][1] = "var" & $i
Next

_ArrayDisplay($array)

 

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

Link to comment
Share on other sites

It is best to define or declare the array outside of any loop. Then, assign values to the elements of the array inside of a loop.

#include <Array.au3>

Local $array[50][2]
Local $var[50] = [49, 48, 47, 46, 45, 44, 43, 42, 41, 40]

For $i = 0 To 49
    $array[$i][0] = $i
    $array[$i][1] = $var[$i]
Next

MsgBox(0, "Result", "$array[5][0] = " & $array[5][0] & @CRLF & @CRLF & "$array[5][1] = " & $array[5][1])
_ArrayDisplay($array)

See tutorial on arrays https://www.autoitscript.com/wiki/Arrays

Link to comment
Share on other sites

Thanks for the replies, I put it in a loop because the [50] can change since I am grabbing the information from a website that updates regularly.

Malkey, your example works but boththose is the one i used since it is more condensed and works aswell.

Thanks for all your help guys!

Link to comment
Share on other sites

 Under no circumstances should you use my solution over Malkey's, at all, ever. In this case my script is only more condensed because I am lazy and was not about to make $var for you, he was kind enough to do so. They are, for all intents, the same (this time, but thats just luck).

Edited by boththose

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

Link to comment
Share on other sites

<zap> ....  since I am grabbing the information from a website that updates regularly. ....<\zap>

p.s.

... don't know if this can be useful for your case, anyway, just for the record, if data that you want to grab from the web site are in an html Table, then you could also consider the _IETableWriteToArray() function, that simply grabs all the data from the table for you and returns them in an ready made array.

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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...