Jump to content

Correct way to populate this 2-dimensional array?


Recommended Posts

So... this doesn't work.

#include <Array.au3>

Local $aTest1[3] = ["Thing1 Thing2 Thing3","Thing4 Thing5 Thing6","Thing7 Thing8 Thing9"]

Local $aTest2[3][3]
For $i = 0 to UBound($aTest1)
$aTest2[$i] = StringSplit($aTest1[$i]," ")
Next

_ArrayDisplay($aTest2)

It seems that a 2-dimensional array is completely different from an array in which every element is an array.

The result I'm hoping for is:

$aTest2[0][0] = "Thing1"
$aTest2[0][1] = "Thing2"
$aTest2[0][2] = "Thing3"
$aTest2[1][0] = "Thing4"
$aTest2[1][1] = "Thing5"
$aTest2[1][2] = "Thing6"
$aTest2[2][0] = "Thing7"
$aTest2[2][1] = "Thing8"
$aTest2[2][2] = "Thing9"
Link to comment
Share on other sites

#include <Array.au3>

Local $aArray_1[3] = ['Thing1 Thing2 Thing3', 'Thing4 Thing5 Thing6', 'Thing7 Thing8 Thing9']

Local Const $iUBound = UBound($aArray_1)
Local $aArray_2[3][3], $aSplit = 0
For $i = 0 To $iUBound - 1
    $aSplit = StringSplit($aArray_1[$i], ' ', 2)
    For $j = 0 To UBound($aSplit) - 1
        $aArray_2[$i][$j] = $aSplit[$j]
    Next
Next
_ArrayDisplay($aArray_2)

Edited by guinness

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

Link to comment
Share on other sites

  • Moderators

lavascript,

You need to extract the elements before loading them into the array: ;)

#include <Array.au3>

Local $aTest1[3] = ["Thing1 Thing2 Thing3", "Thing4 Thing5 Thing6", "Thing7 Thing8 Thing9"]

Local $aTest2[3][3]
For $i = 0 To UBound($aTest1) - 1
    $aTemp = StringSplit($aTest1[$i], " ", 2)
    For $j = 0 To UBound($aTemp) - 1
        $aTest2[$i][$j] = $aTemp[$j]
    Next
Next

_ArrayDisplay($aTest2)

All clear? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Try this:

#include <Array.au3>

Local $aTest1[3] = ["Thing1 Thing2 Thing3","Thing4 Thing5 Thing6","Thing7 Thing8 Thing9"]

Local $aTest2[3][3]
For $i = 0 to UBound($aTest1) - 1
$split = StringSplit($aTest1[$i]," ", 2)
For $x = 0 To UBound($split) - 1
$aTest2[$i][$x] = $split[$x]
Next
Next

_ArrayDisplay($aTest2)

Hi!

Edited by Nessie

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

Local $aTest1[3][3] = [["Thing1", "Thing2", "Thing3"], ["Thing4", "Thing5", "Thing6"], ["Thing7", "Thing8", "Thing9"]]

...or to make it more readable:

Local $aTest1[3][3] = [ _
     ["Thing1", "Thing2", "Thing3"], _
     ["Thing4", "Thing5", "Thing6"], _
     ["Thing7", "Thing8", "Thing9"] _
     ]
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

×
×
  • Create New...