guinness Posted September 14, 2010 Posted September 14, 2010 _FileArray_Split() will read a file to an Array for those who write lines using a delimiter e.g. "|" or "," Example: 1stLine Data1|1stLine Data2|1stLine Data3 ... 2ndLine Data1|2ndLine Data2|2ndLine Data3 ... The function is working as intended, but I wanted to throw the question out of if anyone can create a similar function with different code? I simply want to learn! Code: expandcollapse popup#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include <Array.au3> #include <File.au3> Global $File = @ScriptDir & "\TEMP.txt" _Test_File($File, "|") ; Creates an Example using "|" ShellExecute($File) Global $Array1 = _FileArray_Split($File, "|") If Not @error Then _ArrayDisplay($Array1) _Test_File($File, ",") ; Creates an Example using "," ShellExecute($File) Global $Array2 = _FileArray_Split($File, ",") If Not @error Then _ArrayDisplay($Array2) Func _FileArray_Split($sFile, $sDelimiter = "|") Local $sFileOpen = FileOpen($sFile, 0) ; Code based around the function _FileReadToArray included in File.au3 If $sFileOpen = -1 Then Return SetError(1, 1, 0) Local $sFileRead = FileRead($sFileOpen, FileGetSize($sFile)) If StringRight($sFileRead, 1) = @LF Or StringRight($sFileRead, 1) = @CR Then $sFileRead = StringTrimRight($sFileRead, 1) FileClose($sFileOpen) If StringInStr($sFileRead, @LF) Then Local $TEMPsArray = StringSplit(StringStripCR($sFileRead), @LF) ElseIf StringInStr($sFileRead, @CR) Then $TEMPsArray = StringSplit($sFileRead, @CR) Else If StringLen($sFileRead) Then Dim $TEMPsArray[2] = [1, $sFileRead] Else Return SetError(1, 2, 0) EndIf EndIf Local $sStringSplit = StringSplit($TEMPsArray[1], $sDelimiter) ; Split the first line to find the number of columns for defining final Array Local $sArray[$TEMPsArray[0] + 1][$sStringSplit[0]] = [[0, $sStringSplit[0]]] ; Define final Array with the results found from reading the file and splitting line 1 For $A = 1 To $TEMPsArray[0] $sStringSplit = StringSplit($TEMPsArray[$A], $sDelimiter) ; Split the nth line to create a Temp Array If Not IsArray($sStringSplit) Then ContinueLoop ; If not Array then skip For $B = 1 To $sStringSplit[0] $sArray[$sArray[0][0] + 1][$B - 1] = $sStringSplit[$B] ; Add split data (by looping) to the final Array Next If UBound($sArray, 1) <= $sArray[0][0] + 1 Then ReDim $sArray[UBound($sArray, 1) * 2][UBound($sArray, 2)] ; Resize the number of rows if required $sArray[0][0] += 1 ; Add count to final Array Next ReDim $sArray[$sArray[0][0] + 1][UBound($sArray, 2)] ; Remove empty rows located at the end of the final Array Return $sArray ; Return the final Array EndFunc ;==>_FileArray_Split Func _Test_File($sFile, $sDelimiter = "|") Local $Temp_FileList[1000] For $A = 1 To 999 Local $Text = "" For $B = 1 To 10 $Text &= "Example_" & $B & $sDelimiter Next $Temp_FileList[$A] = StringTrimRight($Text, 1) $Temp_FileList[0] += 1 Next _FileWriteFromArray($sFile, $Temp_FileList, 1) Return 1 EndFunc ;==>_Test_File 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 parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018
seandisanti Posted September 20, 2010 Posted September 20, 2010 Sorry for the slow response on this one, new topics were being added quicker than i was going back to see older stuff... Seems like you could just... #include<file.au3>;so you can use _FileReadToArray()... Func DoIT($file,$delimiter);function should be called with a file handle (or filename, etc. check out file IO functions) _FileReadToArray($file,$fileArray);Why reinvent the wheel, job's half done If Not @error Then;bad file, etc could throw error on last call, so watch for it For $x = 0 to UBound($fileArray)-1;if no error then the array of contents is created so we will loop through the elements $fileArray[$x] = StringSplit($fileArray[$x],$delimiter);again, why reinvent the wheel we can already chop delimited strings Next; time for another iteration Return($fileArray);once it's done lets give the calling thread the data that it wants EndIf;and we're done EndFunc;that quick
guinness Posted September 20, 2010 Author Posted September 20, 2010 Hey thanks for the reply, always great to learn. But the example doesn't work as intended. It creates a blank 1D Array, when my intended output is a 2D Array with the Split content added to the columns. 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 parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018
omikron48 Posted September 20, 2010 Posted September 20, 2010 Just read the whole file content via FileRead then feed that into a StringSplit using the delimiter of your choice. DONE!
seandisanti Posted September 20, 2010 Posted September 20, 2010 Hey thanks for the reply, always great to learn. But the example doesn't work as intended. It creates a blank 1D Array, when my intended output is a 2D Array with the Split content added to the columns. it's actually not a blank 1d array, it's an array of arrays, each element contains an array made of splitting said line number at the delimiter, rather than doing _ArrayDisplay($filearray), you'd have to do a loop to show _ArrayDisplay($filearray[$iterator])
seandisanti Posted September 20, 2010 Posted September 20, 2010 it's actually not a blank 1d array, it's an array of arrays, each element contains an array made of splitting said line number at the delimiter, rather than doing _ArrayDisplay($filearray), you'd have to do a loop to show _ArrayDisplay($filearray[$iterator]) and even though displaying it may appear a little more cumbersome, it could be argued that it is more efficient because you do not have blank columns if there are lines with differing numbers of columns.
Tvern Posted September 20, 2010 Posted September 20, 2010 I doubt that it's more effecient:Especially the use of an Array inside another Array will severely affect the execution speed of your script.Still, that function could be easily changed to return a 2D array.
seandisanti Posted September 20, 2010 Posted September 20, 2010 I doubt that it's more effecient:Still, that function could be easily changed to return a 2D array.depends on what kind of efficiency you're talking about. i meant no wasted or empty columns since every array has only the number of elements it needs
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