Jump to content
Sign in to follow this  
victor

adding element to 2d array.

Recommended Posts

victor

Hi Everyone , I have a question about array and i can't find the answer anywhere online.

I know you can add an element to an array like below but what about 2D array ?

Local $arr[3] ; Make room for three elements

;Assign some data

$arr[0]="Element 1"

$arr[1]="Element 2"

$arr[2]="Element 3"

how do i add data for avArray ?

Local $avArray[3][3]

$avArray[3][3]=[2,5,6]

[2,5,6]

[5,6,3]

[5,5,2]

p/s I don 't want to do it all in one go. , I want it to be added one by one

Local $avArray[5][3] = [ _

[5, 20, 8], _

[4, 32, 7], _

[3, 16, 9], _

[2, 35, 0], _

[1, 19, 6]]

Edited by victor

Share this post


Link to post
Share on other sites
guinness

Look at _ReDim in my signature too.

#include <Array.au3>

Global $aArray[6][3] = [[5, 3], _ ; ]Number of rows excluding that this one, number of columns]
        [5, 20, 8], _
        [4, 32, 7], _
        [3, 16, 9], _
        [2, 35, 0], _
        [1, 19, 6]]

$aArray[0][0] += 1 ; Add 1 to the total count as we want to add another item.
ReDim $aArray[$aArray[0][0] + 1][$aArray[0][1]] ; Re-size using the total count plus the index we use to store the information.

For $i = 0 To $aArray[0][1] - 1 ; Loop through the number of columns from index 0 to 2, hence the 3 - 1.
    $aArray[$aArray[0][0]][$i] = Random(1, 100, 1)
Next
_ArrayDisplay($aArray)

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
Spiff59

I'm thinking you're asking more about populating an existing array, rather than adding to (expanding) one?

The syntax you see when defining an array: Local $avArray[3][3]= [[2,5,6],[5,6,3],[5,5,2]]

is unique to those declaration statements (Dim/Local/Global).

At no other time or place can you reference more than either the entire array : $avCopy = $avArray

or, a single element within the array: $avArray[2][2] = 2.

So to individually populate elements of a predefined 2D array you would just have a series of:

$avArray[0][0] = 2

$avArray[0][1] = 5

$avArray[0][2] = 6

$avArray[1][0] = 5

$avArray[1][1] = 6

...

Share this post


Link to post
Share on other sites
victor

Hi Thanks, for the tips.

one thing that I don't quite understand is why my code below doent's loop ? like it suppose to ?

$vertical = 1

$test = 3

local $bArray[1]

$vertical = 1

$test = 3

for $i = 0 to 10 step 1

ReDim $bArray[$bArray[0] + 1]

$bArray[ubound($bArray)-1]=$vertical & " " & $test

Next

_ArrayDisplay($bArray)

Share this post


Link to post
Share on other sites
Spiff59

I'm quite sure if you stuck a "Beep(800,100)" inside your loop that your PC would make some noise (11 times).

I don't think your problem is that you're not looping, it's that you're not accomplishing what you hope to within the loop.

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  

×