Jump to content

Creating arrays on the fly


Kip
 Share

Recommended Posts

Hi.

I want to create variables on the fly.

I use Assign() to create normal variables, which works very well:

Assign ("MyNewVar","mw")

MsgBox(0,"sdsds",Eval("MyNewVar"))

But when I'm trying to create arrays that way, it doesnt work. Which actually makes sense since the helpfile doesnt say it's able to create arrays.

Assign ("MyNewVar[1]","",2)
Assign ("MyNewVar[0]","mw")

MsgBox(0,"sdsds",Eval("MyNewVar[0]"))

Does someone know how to do this? :P

Edited by Kip
Link to comment
Share on other sites

Hi.

I want to create variables on the fly.

I use Assign() to create normal variables, which works very well:

Assign ("MyNewVar","mw")

MsgBox(0,"sdsds",Eval("MyNewVar"))

But when I'm trying to create arrays that way, it doesnt work. Which actually makes sense since the helpfile doesnt say it's able to create arrays.

Assign ("MyNewVar[1]","",2)
Assign ("MyNewVar[0]","mw")

MsgBox(0,"sdsds",Eval("MyNewVar[0]"))

Does someone know how to do this? :P

This is generally THE WRONG WAY to do things... but it can be done (sort of). You cannot make array references to an Eval() statement, but you can copy to a temp variable for the duration of the operation:
#include <Array.au3>

Global $avTemp[3] = [1, 2, 3]

; Create vars
For $n = 1 To 3
    Assign("Test_" & $n, $avTemp, 2)
Next

$avTemp = ""

; Change data
For $n = 1 To 3
    $avTemp = Eval("Test_" & $n)
    For $r = 0 To UBound($avTemp) - 1
        $avTemp[$r] = $avTemp[$r] * $n
    Next
    Assign("Test_" & $n, $avTemp)
Next

; Display results
For $n = 1 To 3
    $avTemp = Eval("Test_" & $n)
    _ArrayDisplay($avTemp, "$Test_" & $n)
Next

After creating them with Assign(), you could also hard-code references to $Test_1 thru $Test_3, but you'll get warning on syntax check that will have to be ignored, and if you know the variable names to hard-code -- what's the point in declaring them with Assign()?

And besides, Assign/Eval are EVIL!

:(

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

you could also hard-code references to $Test_1 thru $Test_3

Well, hardcoding it in the script is not an option for me. :(

and if you know the variable names to hard-code -- what's the point in declaring them with Assign()?

Avoiding the array in array problem :P

This is generally THE WRONG WAY to do things... but it can be done (sort of).

Thanks, Psalty. But that copies the whole array. (And I already knew that)

Is there a better way?

Link to comment
Share on other sites

Thanks, Psalty. But that copies the whole array. (And I already knew that)

Is there a better way?

To quote myself:

You cannot make array references to an Eval() statement...

What, exactly, is your "Avoiding the array in array problem" problem? A better solution than Assign/Eval is surely available.

:P

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

i dont really understand what you are talking about kip but

you need to put #include <Array.au3> at the top of your script to use the _ArrayAdd() function.

dim $Array_Name[1]; creates a blank array

_ArrayAdd($Array_Name, "data"); adds this value to the array

For $i = 1 To UBound($Array_Name, 1) - 1; set $i to 1 to the number of items in the $Array_Name the 1 means the first part of the array e.g. $Array_Name[first_part][second_part] and the -1 is because otherwise we go past the end of the table.
; do something
Next; loop through until $i is equal to UBound($Array_Name, 1) - 1

some information about arrays and some things you can do with them, i dont know if any of that is what you need but it might help.

Interpreters have great power!Although they live in the shadow of compiled programming languages an interpreter can do anything that a compiled language can do, you just have to code it right.

Link to comment
Share on other sites

Global $Array[5][7]

Global $Bla[2][3]
$Bla[1][2] = $Array; Array in array, which slows down the script. If I can believe the helpfile.
How about a memory resident SQLite DB instead?

Or converted strings saved in scripting dictionary?

I still don't see how you got to a requirement for nested arrays AND a requirement for high speed AND are still doing it in AutoIt? It's an interpreted scripting language, man... calculating the negative gravimetric constant of dark energy is not AutoIt's forte.

:P

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

some information about arrays and some things you can do with them, i dont know if any of that is what you need but it might help.

You're right. That's not what I need. :idea:

How about a memory resident SQLite DB instead?

Or converted strings saved in scripting dictionary?

I can't afford to have external files.

I still don't see how you got to a requirement for nested arrays AND a requirement for high speed AND are still doing it in AutoIt? It's an interpreted scripting language, man... calculating the negative gravimetric constant of dark energy is not AutoIt's forte.

:(

Nevermind, I guess I'll have to do it your way. :P

Link to comment
Share on other sites

I can't afford to have external files.

The cool thing about the SQLite.au3 UDF is that it can create the DB entirely in memory, so the operations are pretty fast too. There is no file required.

:mellow:

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