Jump to content

[array] Adding row to 2D array?


Go to solution Solved by littlebigman,

Recommended Posts

Posted

Hello,

This is my first try at using 2D arrays with the Array.au3 UDF, and Googling + wiki didn't help.

How do I add a new row? _ArrayDisplay() is always empty.

#include <Array.au3>

Local $arr[0][0]
;_ArrayAdd($arr, [["My URL", "My title"]])
;_ArrayAdd($arr, ["My URL", "My title"])
;_ArrayAdd($arr, "My URL" & "|" & "My title")
Local $new = StringFormat("%s#%s", "My URL", "My title")
_ArrayAdd($arr, $new,0,"#")

_ArrayDisplay($arr, "Collected Chrome URLs")

Thank you.

Posted (edited)

so, ["My URL", "My title"]] is what you want to have 🤔

#include <Array.au3>

Local $arr[1][2] ; <<<<< change that
;_ArrayAdd($arr, [["My URL", "My title"]])
;_ArrayAdd($arr, ["My URL", "My title"])
;_ArrayAdd($arr, "My URL" & "|" & "My title")
Local $new = StringFormat("%s#%s", "My URL", "My title")
_ArrayAdd($arr, $new,0,"#")

_ArrayDisplay($arr, "Collected Chrome URLs")

Edit: use #AutoIt3Wrapper_AU3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 as the 1st line of every script will help you to code better code.

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting  image.gif.922e3a93535f431de08b31ee669cc446.gif
autoit_scripter_blue_userbar.png

Posted (edited)

Thanks. I was about to reply.

I naïvely assumed _ArrayAdd() auto-redimensioned an array to fit a new row, but it looks like it must be done manually:

Local $arr[0][0]

ReDim $arr[0][2]
;BAD ;_ArrayAdd($arr, [["My URL" , "My title"]])
;BAD _ArrayAdd($arr, ["My URL" , "My title"])
;_ArrayAdd($arr, "My URL" & "|" & "My title")

_ArrayDisplay($arr, "Collected Chrome URLs")

Edit:

While True
    …
    ;add one row
    ReDim $arr[UBound($arr, $UBOUND_ROWS)][2]

    Local $new = StringFormat("%s#%s", $sCurrentURL, $sTitle)
    _ArrayAdd($arr, $new,0,"#")
WEnd

_ArrayDisplay($arr, "Collected Chrome URLs")
Edited by littlebigman
Posted

It is always a good idea to check the error code after calling a function:

#include <Array.au3>

; Working code
Local $arr1[0][2]
_ArrayAdd($arr1, "My URL" & "|" & "My title")
MsgBox(0, "Example 1 - Error code", @error)
_ArrayDisplay($arr1, "Collected Chrome URLs")

; Code returns an error
Local $arr2[0][1]
_ArrayAdd($arr2, "My URL" & "|" & "My title")
MsgBox(0, "Example 2 - Error code", @error)
_ArrayDisplay($arr2, "Collected Chrome URLs")

My understanding is that _ArrayAdd only adds rows to an existing array but the number of columns has to be >= the number of elements of the string to be added.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted (edited)

Indeed:

#cs
1 - $aArray is not an array
2 - $aArray is not a 1 or 2 dimensional array
3 - $vValue has too many columns to fit into $aArray
4 - $iStart outside array bounds (2D only)
5 - Number of dimensions for $avArray and $vValue arrays do not match
#CE

I'll read up on _ArrayAdd() to understand what this means:

Quote

2D arrays:
    $sDelim_Item delimited string - adds 1 row, columns filled if enough items
    $sDelim_Row delimited string - adds as many rows as items, only 1 column filled
    $sDelim_Item & Row delimited string - adds rows and items, depending on split points
    0-based 2D array - adds as many rows/columns as in array

 

Edited by littlebigman
Posted

Run the example scripts which you can find in the helpfile for _ArrayAdd

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

  • Solution
Posted (edited)

Yup

Turns out _ArrayAdd() takes care of adding rows, but the number of columns has to be correct or I get error 3 "$vValue has too many columns to fit into $aArray":

Local $arr[0][2]
_ArrayAdd($arr, "item1|item2")
_ArrayAdd($arr, "item1|item2")
_ArrayAdd($arr, "item1|item2")

 

Edited by littlebigman
Posted

Do all rows have the same number of items? Or is there a need to redim the array before adding new rows?
How many rows do you need to add to the array?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

Still learning…

In the help file, I went through the "GUICtrl*()" and "_GuiCtrlListView_*()" sections, but didn't find how to get the headers in a listview to actually look like headers, ie. grey background.

Is it normal, or do I need to user other parameters?

$Form1 = GUICreate("", 615, 430, 267, 124)
$ListView1 = GUICtrlCreateListView("URL|Title", 8, 152, 594, 230, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))

Local $aFill[][] = [["New Item 2 - 1", "New Item 2 - 2"], ["New Item 3 - 1", "https://www.acme.com/******************************************************************************************"]]
_GUICtrlListView_AddArray($ListView1, $aFill)

_GUICtrlListView_SetColumnWidth($ListView1, 1, $LVSCW_AUTOSIZE)

GUISetState(@SW_SHOW)

While True
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

 

image.png.f8e1f7295e8313a6fa3cb743c9c882b7.png

Posted (edited)

I have never spent much time to color the ListView headers, but ...
The following thread describes a solution: 

Change the 0x values to your liking or even better use the color variables you can find in ColorConstants.au3

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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
×
×
  • Create New...