Sign in to follow this  
Followers 0
mistersquirrle

Declaring a variable with a variable...

6 posts in this topic

I have a script that creates an array, and I want to create a second dimension in the array, with set values, but the amount of variables in the original array can change, and I don't want to have to manually set up creating the second dimension, and get errors, or not having all the variables have a second dimension. This is the manual set up:

Dim $Values[6] = [13, 20, 25, 12, 13, 21]
Dim $Order[6][2] = [[$Values[0], 1],[$Values[1], 2],[$Values[2], 3],[$Values[3], 4],[$Values[4], 5],[$Values[5], 6]]

and this is kinda what I was thinking of having:

For $i = 0 To UBound($Value) - 1
        $Value[$i] = StringReplace($Value[$i], ",", "")
        $Dim &= ", [$Value[" & $i & "], " & $i & "]"
    Next
    $Dim = StringTrimLeft($Dim, 2)
    $Dim = "[" & $Dim & "]" ;This outputs: [[$Value[0], 0], [$Value[1], 1], [$Value[2], 2], [$Value[3], 3], [$Value[4], 4], [$Value[5], 5], [$Value[6], 6], [$Value[7], 7], [$Value[8], 8], [$Value[9], 9]]

    Dim $Dim[UBound($Split)][2] = $Dim

is there any way to do what I'm trying to?


RAWR! I'm hungry :( Feed the panda squirrle

Share this post


Link to post
Share on other sites



Take a look at the ReDim keyword. It allows you to resize an array, even a 2 dimensional one. Pretty much the only restriction is that you can't change the number of dimensions.


David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

This doesn't do what I want... I don't want to resize an array. I want to create a second dimension, with just 0 - however many array subscripts there are, corresponding to the first dimension array it's being paired with, so I can sort the first dimension, and still know the order of things. Does that make sense? It's kinda related to my previous topic: http://www.autoitscript.com/forum/index.php?showtopic=101352


RAWR! I'm hungry :( Feed the panda squirrle

Share this post


Link to post
Share on other sites

...........

is there any way to do what I'm trying to?

I am not certain what you are trying to do.

Here is a solution to my best guess.

;
#include <Array.au3>

Dim $Value[6] = [13, 20, 25, 12, 13, 21]
Dim $Order[UBound($Value)][2]
;Dim $Order[6][2] = [[$Value[0], 1],[$Value[1], 2],[$Value[2], 3],[$Value[3], 4],[$Value[4], 5],[$Value[5], 6]]

For $i = 0 To UBound($Value) - 1
    $Order[$i][0] = $Value[$i]
    $Order[$i][1] = $i + 1
Next

_ArrayDisplay($Order);
;

Share this post


Link to post
Share on other sites

I am not certain what you are trying to do.

Here is a solution to my best guess.

That's actually just about exactly what I was trying to do. Thank you. I wasn't sure how to do that in a for loop, cause I thought it'd just keep redoing the first array subscript. I guess I just needed to test it a bit. Just a bit of tweaking to that, and that'll work just great :D


RAWR! I'm hungry :( Feed the panda squirrle

Share this post


Link to post
Share on other sites

I think you mean:

This doesn't do what I want... I don't want to resize an array. I want to create a second array, with just 0 - however many array subscripts there are, corresponding to the first dimension array it's being paired with, so I can sort the first array, and still know the order of things. Does that make sense?

It sounds like you want to create an index array that you will rearrange in order to access the original array.

Local $aArray[300][10]    ; Assuming that you have 300 rows with up to 10 fields per row.
Local $aIndex[300]        ; Index for each row.
Local $I                  ; For-loop control variable
Local $bSwapped           ; Flag used during bubble sort.
Local $sDisplay           ; 

; Read all the data into $aArray here by whatever method you want.

; Set up the index array
For $I = 0 To 299
    $aIndex[$I] = $I
Next $I

; Sorting time.
; We sort not by sorting the original array, but the index array.  This is a version of bubble sort.
Do
    $bSwapped = False
    For $I = 0 To 298
        ; The function OutOfSequence($aArray, $N1, $N2) returns true if $aArray[$N1][*] and $aArray[$N2][*] is out of sequence.
        If OutOfSequence($aArray, $aIndex[$I], $aIndex[$I+1]) Then
            ; Out of order - swap the indexes
            $vTmp = $aIndex[$I]
            $aIndex[$i] = $aIndex[$I+1]
            $aIndex[$I+1] = $vTmp
            $bSwapped = True
        Endif
    Next
Until $bSwapped = False

; The original array remains the same.  The index array $aIndex, gives the order of the array.  
; You could set up multiple indexes based on different sequence criteria.
; To access the records in sequence use the index.
For $I = 0 To 299
    $sDisplay += $aArray[$aIndex[$I]] & ", "
Next

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

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
Sign in to follow this  
Followers 0