Sign in to follow this  
Followers 0
Shibuya

Multi Dimension Array

26 posts in this topic

#1 ·  Posted (edited)

I'm trying to do a multi dimension array, hoping that _ArrayCreate could ease declaring th contents

I did something like this:

#include <Array.au3>

Dim $varArray[3]

$varArray[0] = _ArrayCreate("X", "O", "Y")
$varArray[1] = _ArrayCreate("Z", "Q", "T")
$varArray[2] = _ArrayCreate("E", "K", "S")

MsgBox(4096, "Testing", "Content of $varArray[1][2] is " & $varArray[1][2])

I've tried that although I read the help file saying indicated that _ArrayCreate only creates 1-dimension array

So I've expected the above code to fail

But when I replaced MsgBox with _ArrayDisplay, it does display the whole row

So I'm asking where am I missing in the above code, or is there an alternative to declare the rows rather than doing it like:

$varArray[0][0] = "X"

$varArray[0][1] = "O"

...

...

...

Etc...

Edit: forgot to put in the #include

Edited by Shibuya

The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

Share this post


Link to post
Share on other sites



Don't play with the multi dimensional stuff, but don't you have to use _ArrayDisplay() for _ArrayCreate() instead of a typical msgbox?

#include <Array.au3>

Dim $varArray[3]

$varArray[0] = _ArrayCreate("X", "O", "Y")
$varArray[1] = _ArrayCreate("Z", "Q", "T")
$varArray[2] = _ArrayCreate("E", "K", "S")

For $i = 0 To 2
_ArrayDisplay($varArray[$i], "")
Next

[center]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.[/center]

Share this post


Link to post
Share on other sites

I just wanted to extract 1 particular element from the multi dimension array

Sorry that I didn't made that clear in the begining


The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

Share this post


Link to post
Share on other sites

Shibuya keep in mind that all you create with the script was not a multi dimensional array. All you did was to create a 1 dimensional array of 3 elements, then _arraycreate() create another 1 dimensional array of 3 elements inside of the first one and so on. This is why your script did not fail but, create three 1 dimensional array. You can see that if you run runsrules script.

Dim $varArray[1] create a 1 dimensional array

Dim $varArray[1][2] create a 2 dimensional array

Dim $varArray[1][2][3] create a 3 dimensional arrray

.

.

.

Dim $varArray[1][2][3][4].......[64] create a 64 dimensional array


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi,

I found a converter to get one line of Multi Array to a new single array; you can search for it or modify this;

Func _Array2DTo1D(ByRef $ar_Array, $s_Title="Array contents", $n_Index=1,$Line=0,$s_i_Column=0)

; Change Line "X" to 1 dimensional array; [Randallc - I have ***lifted it from Forum at some stage]Local $output = ""

Local $r, $e, $Swap

if $n_Index<>0 then $n_Index=1 ; otherwise I can't cope!

if $s_i_Column<>0 then $s_i_Column=1 ; otherwise I can't cope!

if $Line<0 then $Line=0 ; otherwise I can't cope!

;If $Line>UBound($ar_Array,1+($s_i_Column=0))+($n_Index=0)-2 then $Line=UBound($ar_Array,)+($n_Index=0)-2 ; otherwise I can't cope!

If $Line>UBound($ar_Array,1+($s_i_Column=0))+($n_Index=0)-2 then $Line=UBound($ar_Array,1+($s_i_Column=0))+($n_Index=0)-2 ; otherwise I can't cope!

Dim $Array[uBound($ar_Array,1+$s_i_Column)+($n_Index=0)]

$Array[0]=UBound($ar_Array,1+$s_i_Column)+($n_Index=0)

If Not IsArray($ar_Array) Then Return -1

For $r = $n_Index to UBound($ar_Array,1+$s_i_Column) - 1

$e=$r

$NewLine=$Line

if $s_i_Column=1 then

$NewLine=$r

$e=$Line

EndIf

$Array[$r+($n_Index=0)] = $ar_Array[$e][$NewLine]

Next

_ArrayDisplay($Array,$s_Title&"Line"&$Line)

Return $Array

EndFunc ;==>lf_Array2dDisplay

best, randall Edited by randallc

Share this post


Link to post
Share on other sites

I don't really get what you mean

To my understanding from the help file, _ArrayCreates create a 1 dimensional array

So if I do it like this:

$varArray = _ArrayCreate("X", "O", "Y")

$varArray[1] would return me "O", which I have tested to be working

So I believed that:

$varArray[1] = _ArrayCreate("Z", "Q", "T")

is equivalent to:

$varArray[1][0] = "Z"

$varArray[1][1] = "Q"

$varArray[1][2] = "T"


The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

Share this post


Link to post
Share on other sites

("Z", "Q", "T") are only elements in a single array using _ArrayCreate, that's pretty plain in the example they provide in the help file.

#include <Array.au3>

Dim $avArray
$avArray = _ArrayCreate("JPM", "Holger", "Jon", "Larry", "Jeremy", "Valik", "Cyberslug", "Nutster", "Tylo", "JdeB")

_ArrayDisplay( $avArray, "Array created with _ArrayCreate" )

Shows only single arrays, the way your saying is the way they have it should return:

$avArray[0][1][2][3][4][5][6][7][8][9][10] ?


[center]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.[/center]

Share this post


Link to post
Share on other sites

("Z", "Q", "T") are only elements in a single array using _ArrayCreate, that's pretty plain in the example they provide in the help file.

#include <Array.au3>

Dim $avArray
$avArray = _ArrayCreate("JPM", "Holger", "Jon", "Larry", "Jeremy", "Valik", "Cyberslug", "Nutster", "Tylo", "JdeB")

_ArrayDisplay( $avArray, "Array created with _ArrayCreate" )

Shows only single arrays, the way your saying is the way they have it should return:

$avArray[0][1][2][3][4][5][6][7][8][9][10] ?

Hi ronsrules,

I understood that _ArrayCreate creates a single array

If you refer to my post above yours, you'll notice that instead of using a single $varArray, I declared $varArray[1]

I was thinking, that way it would make it multi-dimension


The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

Share this post


Link to post
Share on other sites

Hi ronsrules,

I understood that _ArrayCreate creates a single array

If you refer to my post above yours, you'll notice that instead of using a single $varArray, I declared $varArray[1]

I was thinking, that way it would make it multi-dimension

I can see where your thought process was. Unfortunately your still only making it a single variable array by adding the [1] etc.. So variable $avArray[1] is the same as $avArray1 with _ArrayCreate() it looks like.

I'll be interested to see where this thread goes though.


[center]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.[/center]

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

#include <Array.au3>

Dim $varArray[3]

$varArray[0] = _ArrayCreate("X", "O", "Y")
$varArray[1] = _ArrayCreate("Z", "Q", "T")
$varArray[2] = _ArrayCreate("E", "K", "S")

MsgBox(4096, "Testing", "Content of $varArray[1][2] is " & $varArray[1][2])

If you refer to my post above yours, you'll notice that instead of using a single $varArray, I declared $varArray[1]I was thinking, that way it would make it multi-dimension

On your script Dim $varArray[3] create a 1 dimension array you are right

then $varArray[0] = _ArrayCreate("X","O","Y") at this point AutoIt believe $varArray[0] is the name of a new 1 dimensional array it doesn't think that is the element of array name $varArray.

If you want to make a multi dimensional array you may used Dim $varArray[1 dimension][2 dimensions][3 dimensions].... all the way to ....[64 dimensions]

and work from there

sorry on my English but is my second language....

see my point about language by the time I type ronsrules allready have the answer in a simple way. B)

Edited by Danny35d

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

Wow~

Neat function you made

This exactly the idea od what I had in mind

Thanks Felix N.!!


The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

Share this post


Link to post
Share on other sites

Felix N. I like your idea with the _ArrayCreateMultiDim but, with this line

$multidim_array = _ArrayCreateMultiDim($array1, $array2, $array3, $array4)

I was under the impresion that $multidim_array will be 4 dimensional but looking at your function don't maner if I add all 9 arrays it only create 2 dimensional array.

then againg you come out with a good idea...


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

the four arrays in "$multidim_array = _ArrayCreateMultiDim($array1, $array2, $array3, $array4)" represent rows

Thanks for explain that $array? represent rows I didn't notice that when I look at your script but like a said in my previews post I like your idea. I did some modification take a look.

#include <Array.au3>

$array1 = _ArrayCreate ("X", "O", "Y")
$array2 = _ArrayCreate ("Z", "Q", "T")
$array3 = _ArrayCreate ("E", "K", "S", "L")
$array4 = _ArrayCreate ("F", "E", "L")

$multidim_array = _ArrayCreateTwoDim($array1, $array2, $array3, $array4)

MsgBox(4096, "Testing", "Content of $multidim_array[0][0] is " & $multidim_array[0][0]);should be X
MsgBox(4096, "Testing", "Content of $multidim_array[1][1] is " & $multidim_array[1][1]);should be Q

Func _ArrayCreateTwoDim($row0, $row1, $row2 = 0, $row3 = 0, $row4 = 0, $row5 = 0, $row6 = 0, $row7 = 0, $row8 = 0, $row9 = 0)
    Local $number_of_rows = @NumParams
    Local $sizes_array[$number_of_rows]
    For $x = 0 To ($number_of_rows - 1)
        If IsArray(Eval('row' & $x)) Then
            $sizes_array[$x] = UBound(Eval('row' & $x))
        Else
            $sizes_array[$x] = 0
        EndIf
    Next
    $biggest = _ArrayMax($sizes_array)
        
    If $number_of_rows < 2 Or $number_of_rows > 10 Then
        SetError(1)
        Return("")
    Else        
        Local $vararray[$number_of_rows][$biggest]
        For $x = ($number_of_rows - 1) To 0 Step -1
            $r = $number_of_rows - 1
            If $r <= $number_of_rows - 1 Then
                For $c = 0 To $sizes_array[$x] - 1
                    $ret = Eval('row' & $x)
                    $vararray[$x][$c] = $ret[$c]
                Next
                $r = $r - 1
            EndIf
        Next    
    EndIf
    SetError(0)
    Return $vararray
EndFunc  ;==>_ArrayCreateTwoDim

I hope you don't mind... B)

Edited by Danny35d

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Hi,

Good work!

This is starting to look useful, I think!

1. Could I request you change it or give an option (as in the display func in attachmeny)so we can get "zero" based for consistency with "StringSplit" and AutoIt?....

2. This "ArrayCreateTwoDim" would work for unlimited numbers of arrays (say, nested in an Array of Arrays!) if it were only a single array at a time, put into a specific spot (UDFs for Append, First, Insert, Delete, etc) in the Multi2-D Array , one at a time; instead of limiting to 10 by using them in parameters; then you can loop through them to create a large Array.

What do you think?

Thanks for your thoughts.

Randall

Edited by randallc

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Felix try this...

Func _ArraySize($array)
    If IsArray($array) Then Return UBound($array)
    Return 0
EndFunc;==>_ArraySize

or you can used UBound() look at this line

$sizes_array[$x] = UBound(Eval('row' & $x))

on the three post above

Edited by Danny35d

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

Hi,

yes, you will see the column "0" in my displayed array has the "array[0]" index of "number of elements" on that row (automatically made in AutoIt if you make the array using "StringSplit" command). I just thought it might be good to have the option to have the row "0" giving a similar index.

You will see a small func in my attachment for changing your initail arrays to "base 1" instead of "base 0"; it would be useful to have one going the other way (eg when transferring an Array to Excel, which, of course, is "base 0" as per tradition.

Point2 ; I'll have to have some time; I will look at it.

Best, Randall)

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Hi,

Here it is, just changing a few lines....

it could be better....

And the "Insert", Array[0] etc could also be put in the func more cleanly, with Multi2 funcs to avoid having to re-write each time?

But it works for now!

Best, Randall

Edited by randallc

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Thank to Feliz N for the idea and Randallc for the suggestions I fininsh the _ArrayCreateTwoDim

;;=====================================================================================
; Function Name:     _ArrayCreateTwoDim
; Description:    Append an one dimensional array into a two dimensional array at any
;                   selected row also you can delete any selected row from the two 
;                   dimensional array and insert a new value to any of the cells.
; Parameter(s):  $avArray   - Two dimensional array.
;                  $aRowArray - One dimensional array to be added or value to insert, 
;                                if $aRowArray is equal to ALL and $nAction is equal 
;                                2 it will delete the entire array.
;                  $nAction   - 1 = To append one dimensional array into any row of
;                                    the two dimmensional array, if no row selected
;                                    it will append array at the last row. (default)
;                               2 = To delete any row from the 2 dimensional array, if
;                                    no row selected it will deleted last row.
;                                3 = To insert a new value at any cell.
;                   $nRow     - To select the row number.
;                   $nColumn   - To select the column number.
; Requirement(s):   None
; Return Value(s):  On Success -  0 if the two dimensional array is modify and @error is
;                                   set to 0.
;                  On Failure -  1 if not modification to the two dimensional array and
;                                   @error is set to 1.
;                                 2 if selected row number is bigger than the biggest row
;                                   on the two dimensional array and @error is set to 2.
;                                 3 if selected column is bigger than the biggest column
;                                   on the two dimwnsional array and @error is set to 3.
;
; Author(s):        Dan Colón
;                  Thanks to Feliz N. for the idea and thanks to Randallc for suggestions
;                   of adding switches for Append, Delete and Insert.
;======================================================================================

Func _ArrayCreateTwoDim(ByRef $avArray, $aRowArray, $nAction = 1, $nRow = '', $nColumn = '')    
    Local $number_of_column = UBound($avArray, 2), $number_of_rows = UBound($avArray), $nSize = 0
    
    If $number_of_column < (UBound($aRowArray) + 1) Then $number_of_column = UBound($aRowArray) + 1
    If $nRow > $number_of_rows - 1 Then
        SetError(2)
        Return(2)
    ElseIf $nColumn > $number_of_column - 1 Then
        SetError(3)
        Return(3)
    EndIf
    
    Select
    Case $nAction = 1 And IsArray($aRowArray);Add 1 dimensional array into a 2 dimensional array, if not row specifed it will added to the end.
        If $avArray[0][0] <> '' Then $number_of_rows = $number_of_rows + 1
        ReDim $avArray[$number_of_rows][$number_of_column]
        If $nRow == '' Then         
            $avArray[$number_of_rows - 1][0] = UBound($aRowArray);Find array size
            For $c = 1 To $avArray[$number_of_rows - 1][0]
                $avArray[$number_of_rows - 1][$c] = $aRowArray[$c - 1]
            Next
            SetError(0)
            Return(0)
        EndIf
        
        For $r = $number_of_rows - 1 To $nRow Step -1           
            If $r <> $nRow Then
                $avArray[$r][0] = $avArray[$r - 1][0]
            Else
                $avArray[$r][0] = UBound($aRowArray)            
            EndIf
            For $c = 1 To $avArray[$r][0]
                If $r <> $nRow Then
                    $avArray[$r][$c] = $avArray[$r - 1][$c]
                    $avArray[$r - 1][$c] = ''
                Else
                    $avArray[$r][$c] = $aRowArray[$c - 1]
                EndIf
            Next
        Next
        SetError(0)
        Return(0)
                
    Case $nAction = 2;Delete entire row, if not row specifed it will delete the last row
        If $number_of_rows == 1 Or StringLower($aRowArray) == 'all' Then
            ReDim $avArray[1][1]
            $avArray[0][0] = '' 
            SetError(0)
            Return(0)
        EndIf
        If $nRow == '' Then
            $avArray[$number_of_rows - 1][0] = ''
            For $x = 0 To $number_of_rows - 1
                If $nSize < $avArray[$x][0] Then $nSize = $avArray[$x][0]               
            Next
            ReDim $avArray[$number_of_rows - 1][$nSize + 1]
            SetError(0)
            Return(0)
        EndIf
        
        $avArray[$nRow][0] = ''
        For $x = 0 To $number_of_rows - 1
            If $nSize < $avArray[$x][0] Then $nSize = $avArray[$x][0]               
        Next
        For $r = $nRow To $number_of_rows - 2
            For $c = 0 To $number_of_column -1
                $avArray[$r][$c] = ''
            Next
            $avArray[$r][0] = $avArray[$r + 1][0]
            For $c = 1 To $avArray[$r][0]
                $avArray[$r][$c] = $avArray[$r + 1][$c]
            Next
        Next
        ReDim $avArray[$number_of_rows - 1][$nSize + 1]
        SetError(0)
        Return(0)
    Case $nAction = 3;Insert a new value to the specific cell
        If $nColumn == 0 Then Return
        $avArray[$nRow][$nColumn] = $aRowArray
        If $avArray[$nRow][0] < $nColumn Then $avArray[$nRow][0] = $nColumn
        SetError(0)
        Return(0)       
    EndSelect
    SetError(1)
    Return(1)
EndFunc

Please checked out and post any bugs, before you can used you have to declare the 2 dimensional array.

Dim $multidim_array[1][1]

_ArrayCreateTwoDim($multidim_array, $array) ;This add the first array

_ArrayCreateTwoDim($multidim_array, $array, 1, 0) ;This will add the array at the beging of the 2 dimensional array

_ArrayCreateTwoDim($multidim_array, '', 2) ;This will delete the last row of the 2 dimensional array

_ArrayCreateTwoDim($multidim_array, 'X', 3, 0, 1) ;This will chaged the value at position [0][1]

Edit:

Add another choice to delete the entire two dimensional array by typing 'All' _ArrayCreateTwoDim($multidim_array, 'All', 2)

Fix Insert function now will update the index

Edited by Danny35d

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

Wow~ this is getting pretty Neat n Sweet

Great job guys

/me salute

I'm having abit of problem

Can anyone tell me what's wrong with this?

#include <Array.au3>
#include "_ArrayCreateTwoDim.au3"

Dim $varArray1
Dim $varArray2
Dim $varArray3

Dim $Alphabet[4][4]

$varArray1 = _ArrayCreate("X", "O", "Y")
$varArray2 = _ArrayCreate("Z", "Q", "T")
$varArray3 = _ArrayCreate("E", "K", "S")

$var = _ArrayCreateTwoDim($Alphabet, $varArray1)
If Not $var = 0 Then MsgBox(0, "1", $var)
$var = _ArrayCreateTwoDim($Alphabet, $varArray2)
If Not $var = 0 Then MsgBox(0, "2", $var)
$var = _ArrayCreateTwoDim($Alphabet, $varArray3)
If Not $var = 0 Then MsgBox(0, "3", $var)

MsgBox(0, "", $Alphabet[2][3])
Edited by Shibuya

The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

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