Jump to content

Array Question


ScottL
 Share

Recommended Posts

Hi-

I'm looking for some help with writing an array to a file.

I'm trying to take data in an edit control and write it out to a text file for use to be used later with a different script. What I'm trying to do is evaluate if the data in the edit control has a hanging carriage return at the bottom, and if it does, then cut it off. I'm trying to do this by writing the edit control data to a file, then reading that file into an array, evaluate for the extra return (using _ArrayMin and a value of ""), then writing the array out to the file again.

The data itself is arranged in a stacked way, and looks something like this:

D001

D002

D003

D004

I'm trying to see if there is an extra return just below the D004 and cut it off so that D004 is the last line.

Because the source file for this data varies user to user, I'm not able to use a static array index number (such as 5).

Here's the code I've been playing with:

CODE
Func WriteList()

; Recreates the user file

_FileCreate("M:\Functional\UserAccess\Ladders\" & $whoami & "AnchorLadders.txt")

$userfile = FileOpen("M:\Functional\UserAccess\Ladders\" & $whoami & "AnchorLadders.txt", 1)

; Check if file opened for writing OK

If $userfile = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

FileWrite($userfile, GUICtrlRead($Edit_1))

FileClose($userfile)

; Read the file into an Array

_FileReadToArray("M:\Functional\UserAccess\Ladders\" & $whoami & "AnchorLadders.txt",$SelectedList)

$numberlines = _FileCountLines("M:\Functional\UserAccess\Ladders\" & $whoami & "AnchorLadders.txt")

$emptyindex = _ArrayMinIndex($SelectedList,0,0)

$emptyvalue = _ArrayMin($SelectedList,0,0)

; Determines is there is an empty value at the end of the array and deletes it.

If $emptyvalue = "" Then

_ArrayDelete($SelectedList, $emptyindex)

$numberlines = $emptyindex - 1

EndIf

; Deletes the GUI to be recreated with the reset value

GUICtrlDelete($Edit_1)

;Clear the Edit control of data.

GUISetFont (10, 400, $font)

$Edit_1 = GUICtrlCreateEdit("", 10, 70, 180, 380)

For $x = 1 to $SelectedList[0]

If $x = $numberlines Then

GUICtrlSetData($Edit_1, $SelectedList[$x], 1)

Else

GUICtrlSetData($Edit_1, $SelectedList[$x] & @CRLF, 1)

EndIf

Next

This is as far as I've gotten.

What I don't understand is why this FOR...NEXT at the bottom of this code isn't working when it was working just fine as is in a section of the code above that populates the edit control intitially.

Can anyone give me some advice or guidance as to where to go from here or what to try next?

Thanks

Link to comment
Share on other sites

  • Moderators

See if this does what you are wanting.

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

Opt("GuiOnEventMode", True)

$GUI = GUICreate("Test")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$Edit = GUICtrlCreateEdit("", 10, 10)
$Button = GUICtrlCreateButton("Write", 10, 175)
GUICtrlSetOnEvent(-1, "_WriteFile")
GUISetState()

While 1
    Sleep(100)
WEnd

Func _WriteFile()
    $sText = GUICtrlRead($Edit)
    $aText = StringSplit($sText, @CRLF, 1)
    For $i = $aText[0] To 1 Step - 1
        If $aText[$i] = "" Then
            _ArrayDelete($aText, $i)
        Else
            ExitLoop
        EndIf
    Next
    _FileWriteFromArray(@ScriptDir & "\Test.txt", $aText, 1)
EndFunc   ;==>_WriteFile

Func _Exit()
    Exit
EndFunc   ;==>_Exit
Link to comment
Share on other sites

The task at hand and the code you have been playing with does not seem to corespond. I have not tested you code!

There seems to be a misunderstanding on how _ArrayMin works.

Maybee you can use something like this?

Local $arr[6] = [5, "data01", "data02", "data03", "data04", "data05"]
For $i = 1 to $arr[0]
    If $arr[$i] = "data04" Then 
        ;Trash the rest of the array
        ReDim $arr[$i]
        ExitLoop
    EndIf
Next
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...