Jump to content

_FileReadToArray not work with empty last lines


 Share

Recommended Posts

when I try make delimiter in _FileReadToArray, I found a problem, it'll not work if last lines have more than a empty lines

#include <Array.au3>
#include <File.au3>
Local $aFileName
_FileReadToArray(@ScriptDir & "\test.txt", $aFileName, 0, ",")
_ArrayDisplay($aFileName)

work:

aa,bb

11,22

work:

aa,bb

11,22

[empty]

not work:

aa,bb

11,22

[empty]

[empty]

 

I'm not sure this's bug or by I'm not fully understand about _FileReadToArray

For temp, I must rewrite that file with delete all empty last lines, then I can use _FileReadToArray. feeling stupid. Could someone help me a smarter way?

Link to comment
Share on other sites

  • Moderators

Nubie,

You are trying to read a delimited line file with lines that do not contain a delimiter - so the function fails and will return @error set to 3 as explained in the Help file. You either need to delete the empty lines or add a delimiter to them - then each line of the file will "match" and the function will work.

And before you ask, the function was designed that way to warn of files which had incorrect delimiter formatting.

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

Thanks sir for explain

But I'm still stucking for find a smarter way without rewrite file. Tried this but get new problem. How to split array when rows and column is unknow amounts

#include <Array.au3>

Local $aFileName
$aFileName = FileReadToArray(@ScriptDir & "\test.txt")
_ArrayDelete($aFileName , _ArrayToString(_ArrayFindAll($aFileName , "")))
_ArrayDisplay($aFileName)

 

Link to comment
Share on other sites

  • Moderators

Nubie,

Try this:

#include <Array.au3>
#include <File.au3>

Global $aArray

; Reads file into array of arrays
_FileReadToArray("Test.txt", $aArray, $FRTA_INTARRAYS, ",")

; Find maximum nunber of delimiter per line
$iMax_Delim = 0
For $i = 0 To UBound($aArray) - 1
    $iElements = UBound($aArray[$i])
    If $iElements > $iMax_Delim Then
        $iMax_Delim = $iElements
    EndIf
Next

; Create a suitably sized array to hold the expanded elements
Global $aExpanded[UBound($aArray)][$iMax_Delim]
; And fill it
For $i = 0 To UBound($aArray) - 1
    For $j = 0 To UBound($aArray[$i]) - 1
        $aExpanded[$i][$j] = ($aArray[$i])[$j]
    Next
Next
; And here it is
_ArrayDisplay($aExpanded, "", Default, 8)

Any use?

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

Another option IF the number of items per row are equal :

#include <Array.au3>

Local $sString = StringRegExpReplace (FileRead ('Test.txt'),"(?m)^\s*$\R?", '')
StringReplace (StringMid ($sString,1,StringInStr ($sString,@CRLF)),",", "")
Local $aArray[0][@extended+1]
_ArrayAdd ($aArray,$sString,0,",")
_ArrayDisplay ($aArray)

Not sure about the speed though.  You may want to compare with M23 solution.

Edited by Nine
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...