Jump to content

2D Array concatenation question


JAFN
 Share

Recommended Posts

Hi,

I have an existing two dimensional array that I want to add a single row of 2D data to.

I've looked but most of the array functions seem to be for single dimensional array.

Any help would be appreciated.

Thanks

[size="2"]The second mouse gets the cheese[/size]
Link to comment
Share on other sites

Here's a quick example of how it can be done:

#include <array.au3>
Global $array1[10][2]
For $I = 0 To 9
    For $J = 0 To 1
        $array1[$I][$J] = Chr(Random(65, 90, 1))
    Next
Next
_ArrayDisplay($array1, "$array1 before adding a new row")
;using string data
Global $newrow = "New row"
Global $newcol = "New column"
ReDim $array1[UBound($array1) + 1][UBound($array1, 2)]
$array1[UBound($array1) - 1][0] = $newrow
$array1[UBound($array1) - 1][1] = $newcol
_ArrayDisplay($array1, "$array1 after using string data row add")
; adding a one row 2D array. NOTE: When using this, the arrays both have to have the same
; number of columns or it will fail
Global $newrow1[1][2] = [["Newer row", "Newer column"]]
ReDim $array1[UBound($array1) + 1][UBound($array1, 2)]
For $I = 0 To UBound($array1, 2) - 1
    $array1[UBound($array1) - 1][$I] = $newrow1[0][$I]
Next
_ArrayDisplay($array1)

This creates a 2D, 2 column array and fills it with random characters. Then it adds a new row and fills it with strings, could be done using variables. The second example uses another 2D, 2 column array that contains one row and adds that to the end of the $array1 array.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Here's a quick example of how it can be done:

#include <array.au3>
Global $array1[10][2]
For $I = 0 To 9
    For $J = 0 To 1
        $array1[$I][$J] = Chr(Random(65, 90, 1))
    Next
Next
_ArrayDisplay($array1, "$array1 before adding a new row")
;using string data
Global $newrow = "New row"
Global $newcol = "New column"
ReDim $array1[UBound($array1) + 1][UBound($array1, 2)]
$array1[UBound($array1) - 1][0] = $newrow
$array1[UBound($array1) - 1][1] = $newcol
_ArrayDisplay($array1, "$array1 after using string data row add")
; adding a one row 2D array. NOTE: When using this, the arrays both have to have the same
; number of columns or it will fail
Global $newrow1[1][2] = [["Newer row", "Newer column"]]
ReDim $array1[UBound($array1) + 1][UBound($array1, 2)]
For $I = 0 To UBound($array1, 2) - 1
    $array1[UBound($array1) - 1][$I] = $newrow1[0][$I]
Next
_ArrayDisplay($array1)

This creates a 2D, 2 column array and fills it with random characters. Then it adds a new row and fills it with strings, could be done using variables. The second example uses another 2D, 2 column array that contains one row and adds that to the end of the $array1 array.

I am feeling so think, sun gone done and all.

I feel like I should be able to understand that but it is hurting me head.

But I really want to work on this in the morning so please take me from the generic to the specific given the this information.

I have an array of four pieces of information: title, quantity, genre, label each for however many number of items

I have a routine that asks for these four pieces of info. I want add this info into a new row in the array.

Please help my addled brain.

Thank you.

[size="2"]The second mouse gets the cheese[/size]
Link to comment
Share on other sites

How is the routine getting and storing the information?

If it's pulling them from, for example an input line, and storing that information into a string variable (i.e. $string = GUICtrlRead($Input1)) you use the first method I posted.

If you're pulling the information and placing it into an array (i.e. $array[0][0] = GUICtrlRead($input1), $array[0][1] = GUICtrlread($input2) etc.) you'd use the second method I posted.

In the array to array method, you loop through the columns adding them to the target array one at a time until you ran out of columns, or you could just add them all in 4 lines if, as in your case, you know how many columns the arrays have.

The ReDim command is being used to add an additional row to the array but keeping the column count the same. Then once you've added that to the array, you just have to figure out how you want to fill in the empty spaces. ReDim will preserve the contents of an array as long as you don't change the number of dimensions.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

How is the routine getting and storing the information?

If it's pulling them from, for example an input line, and storing that information into a string variable (i.e. $string = GUICtrlRead($Input1)) you use the first method I posted.

If you're pulling the information and placing it into an array (i.e. $array[0][0] = GUICtrlRead($input1), $array[0][1] = GUICtrlread($input2) etc.) you'd use the second method I posted.

In the array to array method, you loop through the columns adding them to the target array one at a time until you ran out of columns, or you could just add them all in 4 lines if, as in your case, you know how many columns the arrays have.

The ReDim command is being used to add an additional row to the array but keeping the column count the same. Then once you've added that to the array, you just have to figure out how you want to fill in the empty spaces. ReDim will preserve the contents of an array as long as you don't change the number of dimensions.

I'm drifting out of the time I can think:

If $Flag = "Add" Then
                    ReDim $data[UBound($array1) + 1][4]
                    $data[UBound][0] = GUICtrlRead($CreateTitle)
                    $data[UBound][1] = GUICtrlRead($CreatePrice)
                    $data[UBound][2] = GUICtrlRead($CreateLabel)
                    $data[UBound][3] = GUICtrlRead($CreateGenre)
                    RefreshListView()
                Else
                EndIf

Wgat am I doing wrong?

[size="2"]The second mouse gets the cheese[/size]
Link to comment
Share on other sites

I'm drifting out of the time I can think:

If $Flag = "Add" Then
                    ReDim $data[UBound($array1) + 1][4]
                    $data[UBound][0] = GUICtrlRead($CreateTitle)
                    $data[UBound][1] = GUICtrlRead($CreatePrice)
                    $data[UBound][2] = GUICtrlRead($CreateLabel)
                    $data[UBound][3] = GUICtrlRead($CreateGenre)
                    RefreshListView()
                Else
                EndIf

Even by my standards that was embarrassing.

I've got it now and it works.

Thank you for your patience.

[size="2"]The second mouse gets the cheese[/size]
Link to comment
Share on other sites

  • 2 weeks later...

Hello,

for my use, I create a useful function, see :

http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=7413

Regards

ricky03 - Very nice!

Any plans to auto detirmine the width of the columns in your _Array2DToHtml?

Or maybe set it as an parameter?

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