Jump to content

Remove Blank Lines in a file _FileReadtoArray


youtuber
 Share

Recommended Posts

Trying to delete empty lines is giving error

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>

$Form1 = GUICreate("Form1", 616, 457, 192, 124)
$Edit1 = GUICtrlCreateEdit("" & @CRLF, 24, 24, 569, 393, BitOR($ES_MULTILINE, $ES_AUTOVSCROLL, $WS_VSCROLL))
GUICtrlSetData(-1, "")
GUICtrlSendMsg($Edit1, $EM_LIMITTEXT, -1, 0)
$Button1 = GUICtrlCreateButton("Button1", 512, 424, 75, 25)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _allfilesetdata()
    EndSwitch
WEnd

Func _allfilesetdata()
    Local $aFiles = _FileListToArray(@ScriptDir, "*.txt", $FLTA_FILES, True)
    Local $aLines
    For $i = 1 To $aFiles[0]
        _FileReadToArray($aFiles[$i], $aLines)
        ;$aLines = StringRegExpReplace($aFiles[$i], '(?m:^\s*[\r\n])', '')
        ;If Not StringRegExp($alines[$i], '[^\s]', 0) Then _ArrayDelete($alines, $i)
        ;$aLines = StringRegExpReplace($aLines, "^\s*$", "")
        If IsArray($aLines) Then
            If $aLines[$i] = "" Then
                _ArrayDelete($aLines, $i)
            EndIf
            For $j = 1 To $aLines[0]
                GUICtrlSetData($Edit1, $aLines[$j] & @CRLF,1)
            Next
        EndIf
    Next
EndFunc   ;==>_allfilesetdata

 

Edited by youtuber
Link to comment
Share on other sites

  • Developers

... and the error is ....   ( or are we supposed to guess/test ourselves/ use the Crystal ball) ?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

When deleting in a array: Always from the end.

1 minute ago, Jos said:

.. and the error is .... 

The usual when deleting in a array from the begin.

@youtuber

When deleting in a array: Always from the end. Faster solution desc lieorder:

Func _allfilesetdata()
    Local $aFiles = _FileListToArray(@ScriptDir, "*.txt", $FLTA_FILES, True)
    Local $aLines
    For $i = 1 To $aFiles[0]
        _FileReadToArray($aFiles[$i], $aLines)
        If IsArray($aLines) Then
            For $j = $aLines[0] To 1 Step -1
                If $aLines[$j] = "" Then
                    _ArrayDelete($aLines, $j)
                Else
                    GUICtrlSetData($Edit1, $aLines[$j])
                EndIf
            Next
        EndIf
    Next
EndFunc   ;==>_allfilesetdata

if you want to have right lineorder must be:

Func _allfilesetdata()
    Local $aFiles = _FileListToArray(@ScriptDir, "*.txt", $FLTA_FILES, True)
    Local $aLines
    For $i = 1 To $aFiles[0]
        _FileReadToArray($aFiles[$i], $aLines)
        If IsArray($aLines) Then
            For $j = $aLines[0] To 1 Step -1
                If $aLines[$j] = "" Then _ArrayDelete($aLines, $j)
            Next
            For $j = 1 To $aLines[0]
                GUICtrlSetData($Edit1, $aLines[$j])
            Next
        EndIf
    Next
EndFunc   ;==>_allfilesetdata

 

Link to comment
Share on other sites

  • Developers

No pictures please ... just the text is fine ....  and I guess this error is something like:

Quote

Array variable has incorrect number of subscripts or subscript dimension range exceeded

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers
1 minute ago, AutoBert said:

The usual when deleting in a array from the begin.

I know and fully understand the issue but simply want to tell the OP he needs to get his shit together before posting! ( don't tell him I told you ...ok? We keep this our secret ;)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

7 minutes ago, Jos said:

I know and fully understand the issue but simply want to tell the OP he needs to get his shit together before posting! ( don't tell him I told you ...ok? We keep this our secret ;)

I think I got it right. :D

Func _allfilesetdata()
    Local $aFiles = _FileListToArray(@ScriptDir, "*.txt", $FLTA_FILES, True)
    Local $aLines
    For $i = 1 To $aFiles[0]
        _FileReadToArray($aFiles[$i], $aLines)
        If IsArray($aLines) Then
            For $j = $aLines[0] To 1 Step -1
                If $aLines[$j] = "" Then _ArrayDelete($aLines, $j)
            Next
            ;For $j = 1 To $aLines[0]
                For $j = 1 to UBound($aLines) -1
                    GUICtrlSetData($Edit1, $aLines[$j] & @CRLF,1)
            Next
        EndIf
    Next
EndFunc   ;==>_allfilesetdata

 

Link to comment
Share on other sites

You can get the array rows deleted faster using the range option of ArrayDelete.  Using this option, you can go forward through the array as well.  

Func _allfilesetdata()
    Local $aFiles = _FileListToArray(@ScriptDir, "*.txt", $FLTA_FILES, True)
    Local $aLines
    Local $sDeleteRows = ""
    For $i = 1 To $aFiles[0]
        _FileReadToArray($aFiles[$i], $aLines)
        If IsArray($aLines) Then
            For $j = 1 To $aLines[0]
                If $aLines[$j] = "" Then $sDeleteRows &= $j & ";"
            Next
            $sDeleteRows = StringTrimRight($sDeleteRows, 1) ;Remove last ";".
            _ArrayDelete($aLines, $sDeleteRows) ;Delete multiple rows.  
            $aLines[0] = UBound($aLines) - 1 ;Change $aLines[0] to current array size.
            
            For $j = 1 To $aLines[0]
                GUICtrlSetData($Edit1, $aLines[$j] & @CRLF,1)
            Next
        EndIf
    Next
EndFunc   ;==>_allfilesetdata

 

Adam

 

Edited by AdamUL
Bug in code
Link to comment
Share on other sites

My 2 cents
You might try something like this, shorter and much faster
BTW it also removes blank spaces (if exist) at the beginning of lines  :)

Func _allfilesetdata()
    Local $aFiles = _FileListToArray(@ScriptDir, "*.txt", $FLTA_FILES, True)
    For $i = 1 To $aFiles[0]
         $f = FileRead($aFiles[$i])
         $f = StringRegExpReplace($f, '(?m)^\s*\R?', "")
         GUICtrlSetData($Edit1, $f & @CRLF, 1)
    Next
EndFunc   ;==>_allfilesetdata

 

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...