Search the Community
Showing results for tags '2D Array'.
-
Hello, I'm trying to make a program that can look at a folder directory, find all the CSV files, and then add the data from CSV's to an array. The problem I seem to be running into is on the 2nd iteration (2nd file) when the script will not create an array. Could someone please help? Thanks in advance #include <Array.au3> #include <File.au3> #include <MsgBoxConstants.au3> #include <Excel.au3> #include <MsgBoxConstants.au3> Global $MasterArray RefineData() Func RefineData() Local $i, $filenum, $file, $csvArray, $sFilePath = @ScriptDir $fileList = _FileListToArrayRec($sFilePath, "*.csv", 1) ;Create and array of all .csv files within folder ;=====Loop through the .csv files within the folder====== For $filenum = 1 To UBound($fileList) - 1 Step 1 $file = $fileList[$filenum] $sFilePath = $sFilePath & "\" & $file ;=====Create array based on csv file===== _FileReadToArray($sFilePath, $csvArray, $FRTA_NOCOUNT, ",") _ArrayDisplay($csvArray,"File: " & $filenum) If $filenum = 1 Then $MasterArray = $csvArray _ArrayDisplay($MasterArray, "Master") Else $MasterArray = _ArrayColInsert($MasterArray, UBound($MasterArray)) ;want column added at end For $i = 0 To UBound($MasterArray)-1 Step 1 $MasterArray[$i][UBound($MasterArray) - 1] = $csvArray[$i][4] Next _ArrayDisplay($MasterArray, "Master") EndIf Next EndFunc ;==>RefineData
-
Hello, I wonder if there is a better way than this!: #include <Array.au3> Local $aArray[1][3] $aArray[0][0] = 1 $aArray[0][1] = 2 $aArray[0][2] = 3 ;$aArray[0] = [1, 2, 3] _ArrayDisplay($aArray)IIRC line no. 9 should work, but its not Thanks in Advance, TD
-
I always use _FileWriteFromArray2D written by Universalist, but the script is slow and almost useless if the file contains more then 5000 rows. Thats why I've made _FileFromArray2D and co-function _FileToArray2D. If you have a file containing 30000 rows, _FileReadToArray2D takes about 1 hour to create the 2D Array _FileToArray2D can do it in a few seconds. There are some extra options in _FileToArray2D, a positive endrow number stops importing at fileline number in file, a negative value will import startrow + nr off rows. The script needs some error handling, so feel free to post it here. #include-Once #include <File.au3> #include <Array.au3> ; #INDEX# ======================================================================================================================= ; Title .........: FileArray2D ; AutoIt Version : 3.2.10++ ; Language ......: English ; Description ...: Functions for creating en reading 2D arrays. ; Author(s) .....: Frans (SnArF) Ordelman ; =============================================================================================================================== ; #NO_DOC_FUNCTION# ============================================================================================================= ; Not working/documented/implemented at this time ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ;_FileToArray2D ;_FileFromArray2D ; =============================================================================================================================== ; #INTERNAL_USE_ONLY# =========================================================================================================== ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileToArray2D ; Description ...: Creates a 2D array from a file. ; Syntax.........: _FileToArray2D($aFile, ByRef $aArray, $aDelim = ";", $aStartRow = 1, $aEndRow = 0) ; Parameters ....: $aFile - File to read ; $aArray - 2D Array to create ; $aDelim - [Optional] String delimiter, default is ";" ; $aStartRow - [Optional] Row to start, default is 1 ; $aEndRow = [Optional] Row to end, default is 0 (all) ; Return values .: Success - Index of last added item ; Failure - -1, sets @error ; Author ........: Frans (SnArF) Ordelman ; Modified.......: ; Remarks .......: $aEndRow positive stops importing at filenr in file, ; a negative value will import startrow + nr off rows ; Related .......: __FileFromArray2D ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _FileToArray2D($aFile, ByRef $aArray, $aDelim = ";", $aStartRow = 1, $aEndRow = 0) Local $aRead, $aRows, $aColums, $aString $aRead = FileOpen($aFile) If $aStartRow < 1 Then $aStartRow = 1 $aRows = _FileCountLines($aFile) + 1 - $aStartRow If $aEndRow >0 Then $aRows = $aRows - ($aRows - $aEndRow + $aStartRow -1) If $aEndRow <0 Then $aRows = $aRows - ($aRows + $aEndRow) $aColums = UBound(StringSplit(FileReadLine($aRead, $aStartRow), $aDelim))-1 FileSetPos($aRead, 0, $FILE_BEGIN) FileReadLine($aRead,-1 + $aStartRow) Local $aArrayTemp[$aRows + 1][$aColums] $aArrayTemp[0][0] = $aRows For $i = 1 to $aRows $aString = StringSplit(FileReadLine($aRead), ";") For $k = 0 to $aColums -1 $aArrayTemp[$i][$k] = $aString[$k+1] Next Next FileClose($aRead) $aArray = $aArrayTemp EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileFromArray2D ; Description ...: Creates a file from a 2D array. ; Syntax.........: _FileFromArray2d($aFile, $aArray, $aDelim = ";", $aWriteMode = 2, $aStartRow = 0, $aEndRow = -1, $aStartCol = 0, $aEndCol = -1) ; Parameters ....: $aFile - File to create ; $aArray - 2D Array to read ; $aDelim - [Optional] String delimiter, default is ";" ; $aWriteMode - [Optional] Mode to open the file in, default is 2 ; $aStartRow - [Optional] Row to start, default is 0 ; $aEndRow = [Optional] Row to end, default is -1 (all) ; $aStartCol = [Optional] Colum to start, default is 0 ; $aEndCol = [Optional] Column to end, default is -1 (all) ; Return values .: Success - Index of last added item ; Failure - -1, sets @error ; Author ........: Frans (SnArF) Ordelman ; Modified.......: ; Remarks .......: ; Related .......: __FileToArray2D ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _FileFromArray2d($aFile, $aArray, $aDelim = ";", $aWriteMode = 2, $aStartRow = 0, $aEndRow = -1, $aStartCol = 0, $aEndCol = -1) Local $aWrite, $aUboundRow, $aUboundCol, $aLine, $aDelimNr If Not IsArray($aArray) Then SetError(1) Return 0 EndIf $aWrite = FileOpen($aFile, $aWriteMode) If $aWrite = -1 Then SetError(2) Return 0 EndIf $aUboundRow = UBound($aArray, 1)-1 $aUboundCol = UBound($aArray, 2)-1 $aDelimNr = StringLen($aDelim) If $aEndRow > $aUboundRow Or $aEndRow = -1 Then $aEndRow = $aUboundRow If $aEndCol > $aUboundCol Or $aEndCol = -1 Then $aEndCol = $aUboundCol For $i = $aStartRow To $aEndRow $aLine = "" For $k = $aStartCol To $aEndCol $aLine &= $aArray[$i][$k] & $aDelim Next $aLine = StringTrimRight($aLine, $aDelimNr ) FileWriteLine($aWrite, $aLine) Next FileClose($aWrite) EndFunc_filearray2d.au3 Example 2D Array.zip
-
Hi everyone. New guy here. I'm still learning this awesome language and I'm unable to figure this one out from google searches. Heres my problem: I'm working on a small application to help users in my environment connect to wireless projectors. To keep this easily updated with new projectors I'm reading the room names and IP addresses from a csv file and putting them into a 2D array. (MeetingRoom1,xxx.xxx.xxx.xxx) So far I'm able to read the 0 column and display the room names in a combo box. Where I am stuck is how to take the user's room selection from the gui and associate it with an IP address in the array. After that point I've got things prepared to pass the address to the connection application. Any help you all could provide would be greatly appreciated.
-
I've been working on a program for a while now to help me with my job in data entry, I'm facing one last issue and I think using an Array might just do the job. Most of the data I work with contains 1 medical code "example: E11", I made 2 input boxes in my program one for the letter and the 2nd for the number, sometimes i get cases with +2 medical codes, I want a way to put as many medical codes as needed before I run the program. A little more explanation: I get a case with 1 medical code, i enter it in my program then run it, the program then enters the needed information plus that code, if i get 2 medical codes, I have to wait for the program to finish it work then enter the 2nd code manually, I added a little button that when clicked will do the part of the program where it enters the medical codes so that way i only have to enter the 2nd code and click the button (without using my program i have to wait for 4 loading screens). Here is what I came up with so far: Local $finalMC1 = GUICtrlRead($Medical) Local $finalMC2 = GUICtrlRead($Medical2) Global $MC[1][2] = [[$finalMC1, $finalMC2]] If _GUICtrlEdit_LineLength($Medical2) = 2 And _GUICtrlEdit_GetModify($Medical2) > 0 Then ReDim $MC[UBound($MC) + 1][2] $MC[UBound($MC) - 1][0] = $finalMC1 $MC[UBound($MC) - 1][1] = $finalMC2 GUICtrlSetState($Medical, $GUI_FOCUS) _GUICtrlEdit_SetModify($Medical2, False) EndIf So basically if $Medical2 = 2 characters the script will resize the array and add the values of $Medical and $Medical1 to it, so far so good. Next thing I'll be needing is to find out how many items are in the $MC array (found out that Ubound will do the job on this one), I tried using "For...To...Step...Next", then the script threw the following error: "ReDim" used without an array variable.", To be completely honest here I have no idea whats causing that error, this is my 1st time working with Arrays, UBound and ReDim, I got the way for resizing 2D Arrays from a post here on the forums, I tried googling the error and came back with nothing, It's really getting irritating...
-
This script is fairly straightforward. If you ever worked with large files before then this may be of help. By large I mean files of 2 MB or so. Granted this doesn't sound so big but going through the file and parsing it to a 2D array all at once took an astronomical amount of time so I wrote my own function to handle this. I discovered that chunking a large array can boost the performance of iterating through the elements and theoretically this should maintain the performance no matter how large the array size is. I know there is room for improvement so please feel free to contribute! Note: I wasn't able to fully test this on larger files such as 200 MB in size due to AutoIt complaining about an error allocating memory while executing _FileReadToArray(). Any help is appreciated. Features: Chunking (Performance will never degrade over time; I.E. Capable of parsing 200 lines or 20,000 and no performance hit will occur) Automatically re-sizes to dynamic columns Preserves Columns while parsing FAST!!!!!! (I can parse a file that contains 24,000 lines with variable columns up to 8 columns and it will finish under a second.) Script: _ArrayTo2DArray.au3 Example usage: Local $aExport ;Initialize array _FileReadToArray("LARGE TEXT.txt", $aExport) ;Returns 1D array of file Local $aSheet = _ArrayTo2DArray($aExport) ;Converts it to 2D Example Text File: LARGE TEXT.txt This script was inspired by >this post. *Updated attachment: Minor bug fixes* *UPDATE June 6, 2013: I apologize! I just realized I made a complete mess of the algorithm. I'm working on a fix now.* *UPDATE June 6, 2013: Bug fixed! It's attached in the post now.
- 2 replies
-
- 2D Array
- Large Files
-
(and 1 more)
Tagged with:
-
NOTICE: The most current update contains script-breaking changes! The function now works exactly like _ArrayConcatenate. It does not return a value, but sets the parent array as the concatenated array. I know there's examples of this, but I thought I share this one with you. It's fully commented to aid those learning the language. Supports vertical and horizontal merging. There's minimal explicit variables by design. Not very elegant, but it works. One advantage this method has is zero dependencies. The dependencies in the download are for the example. EDIT: Updated script to UDF standards. EDIT2: Converted to ByRef and Const ByRef parameters (script breaking change!) Array2DConcat.au3 Previous versions: Examples in next post. Script above contains examples. -cb
-
Hey people, anyone helpme please? wath's wrong in this? #include <Array.au3> Dim $sArray[1][5] = [[1,2,3,4,5]] _ArrayDisplay($sArray) ReDim $sArray[2][5] = [2,3,4,5,6] _ArrayDisplay($sArray) how to i redim one array, and add another array in this (to use it as columns) #include <Array.au3> Dim $sArray[1][5] = [[1,2,3,4,5]] _ArrayDisplay($sArray) ;ReDim $sArray[2][5] = [2,3,4,5,6] ;_ArrayDisplay($sArray) this works fine, but i can't redim and add another itens as columns =S i know i use a loop to add items such as columns, but much more work would ... i think it can be done that way! Thanks a lot!