PlinioPlinio Posted March 22, 2013 Share Posted March 22, 2013 (edited) Hello everyone, I've searched a little (I should have put more effort to it) and couldn't find a clear answer to my question.I'm trying to read my file get a line, split it when there's a space and put it into an array. Here's what I've done:Func loadFile() Local $file = FileOpen('auxiliar.txt', 0) Global $array[1] If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf Local $x = 0 While 1 Local $line = FileReadLine($file) If @error = -1 Then ExitLoop $array[$x] = StringSplit($line, ' ', 2) $x = $x + 1 ReDim $array[UBound($array)+1] WEnd EndFuncI've read StringSplit returns an array, so I thought I would access my splited strings with $array[0][0], but it says my array have not enough dimensions. If I just use $array[0] it returns me a empty string. What should I do?Sorry for any errors, english is not my native language Edited March 22, 2013 by PlinioPlinio Link to comment Share on other sites More sharing options...
BrewManNH Posted March 22, 2013 Share Posted March 22, 2013 Because you're putting the array into another array, and you can't address it like that. Doing this is just going to cause you headaches in the future, so I'd avoid putting arrays in arrays unless you feel you absolutely have to, and know what you're doing when you need to access them. 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 GudeHow 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 More sharing options...
PlinioPlinio Posted March 22, 2013 Author Share Posted March 22, 2013 When I first though about doing this, I thought get array into array would automatically get me a multidimensional array, but it looks like things don't work this way. I've made a little change so I can get it working the way I want, but I have to make every StringSplit return an array with a limited lenght. Func loadFile() Local $file = FileOpen('auxiliar.txt', 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf Local $x = 0 While 1 If IsDeclared('array') = 0 Then Global $array[1][3] Else ReDim $array[UBound($array)+1][3] EndIf Local $line = FileReadLine($file) If @error = -1 Then ExitLoop Local $aux = StringSplit($line, ' ', 2) For $i = 0 to 2 $array[$x][$i] = $aux[$i] Next $x = $x + 1 WEnd EndFunc But this way I need every StringSplit to return me an array with a max lenght of 3. (Or another number) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 22, 2013 Moderators Share Posted March 22, 2013 (edited) PlinioPlinio, You appear to be trying to put arrays inside another array - this does not result in a 2D array. I think what you want is more like this: expandcollapse popup#include <File.au3> #include <Array.au3> ; Just for display <<<<<<<<<<<<<<<<<<<<< Global $aArray[1][1] loadFile() Func loadFile() ; Load file into an array Local $aLines _FileReadToArray("auxiliar.txt", $aLines) _ArrayDisplay($aLines) ; Just for display <<<<<<<<<<<<<<<<<<<<< ; Redim the main array to the correct size ReDim $aArray[$aLines[0]][1] ; Set the current size of the second dimension Local $iCurr2Dim = 1 ; Now loop through the lines For $i = 1 To $aLines[0] ; Split the line on spaces $aTemp = StringSplit($aLines[$i], " ") ; See if we need to ReDim the array If $aTemp[0] > $iCurr2Dim Then $iCurr2Dim = $aTemp[0] ReDim $aArray[$aLines[0]][$aTemp[0]] EndIf ; And copy in the elements For $j = 1 To $aTemp[0] $aArray[$i - 1][$j - 1] = $aTemp[$j] Next _ArrayDisplay($aArray, "After line " & $i) ; Just for display <<<<<<<<<<<<<<<<<<<<< Next _ArrayDisplay($aArray, "Complete") ; Just for display <<<<<<<<<<<<<<<<<<<<< EndFunc ;==>loadFile I used this file to test: one two three one two three four one two one two three four five one Did I guess right? M23 Edit: Welcome to the AutoIt forum, by the way. Edited March 22, 2013 by Melba23 czardas 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
PlinioPlinio Posted March 22, 2013 Author Share Posted March 22, 2013 Yeah, you got what I wanted. This way some of my items will have empty space, since the length of my second dimension is determined by the "largest" StringSplit, how do I know if one position is empty? What is the if condition? Link to comment Share on other sites More sharing options...
czardas Posted March 22, 2013 Share Posted March 22, 2013 This is almost the same as Melba's code. The only drawback is that it uses the full delimiter string @CRLF to split the lines. #include <Array.au3> Global $sString = FileRead("auxiliar.txt") Global $aArray = StringTo2dArray($sString, @CRLF, " ") _ArrayDisplay($aArray) Func StringTo2dArray($sString, $sDelim4Rows, $sDelim4Cols) Local $aArray = StringSplit($sString, $sDelim4Rows, 3) ; Split to get rows Local $iBound = UBound($aArray) Local $aRet[$iBound][2], $aTemp, $iOverride = 0 For $i = 0 To $iBound -1 $aTemp = StringSplit($aArray[$i], $sDelim4Cols, 1) ; Split to get row items If Not @error Then If $aTemp[0] > $iOverride Then $iOverride = $aTemp[0] ReDim $aRet[$iBound][$iOverride] ; Add columns to accomodate more items EndIf EndIf For $j = 1 To $aTemp[0] $aRet[$i][$j -1] = $aTemp[$j] ; Populate each row Next Next If $iOverride <= 1 Then $aRet = $aArray ; Array contains single row or column Return $aRet EndFunc ;==> StringTo2dArray operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 22, 2013 Moderators Share Posted March 22, 2013 PlinioPlinio,Glad to see the crystal ball in still in good nick! how do I know if one position is empty? What is the if condition?I do not understand your question. As the 2D array is always at least as big as the number of words on the longest line so far read there is no need to worry about filling the "empty elements" if the current line has fewer - you use the count from the StringSplit to transfer those elements that do exist and just leave as undefined the unused elements deeper into the 2D array. Clearer? M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
czardas Posted March 22, 2013 Share Posted March 22, 2013 (edited) how do I know if one position is empty? What is the if condition? This condition tests if element $aArray[0][2] is not an empty string. If $aArray[0][2] <> "" Then MsgBox(0, "", $aArray[0][2]) Edited March 22, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
PlinioPlinio Posted March 22, 2013 Author Share Posted March 22, 2013 Thank you guys! Problem solved! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 22, 2013 Moderators Share Posted March 22, 2013 Oh well, I see the crystal ball has gone cloudy again. M23 czardas 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now