Sign in to follow this  
Followers 0
Docfxit

How to separate an input string

12 posts in this topic

#1 ·  Posted (edited)

I am trying to read in a file and display/change in in a gui.

When I read in the file I'm reading it a line at a time.

I need to separate out the information on the line and put the correct information into the correct columns.

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

Global $hListView
Global $Input1 = GUICtrlCreateInput("", 72, 16, 209, 21)

_Main()
FileClose($file)

Func _Main()

Local $GUI, $hImage
$GUI = GUICreate("List of folders", 400, 300)

$hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 394, 268)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))

DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", $hListView, "wstr", "Explorer", "ptr", 0) ; Set "Vista" Style

GUISetState()

; Add columns
_GUICtrlListView_InsertColumn($hListView, 0, "FileID", 100)
_GUICtrlListView_InsertColumn($hListView, 1, "FileName", 100)
_GUICtrlListView_InsertColumn($hListView, 2, "FolderName", 100)


; Loop until user exits
Do
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
;    Case $Button1
Local $file = FileOpen("C:\Scratch\Scratch11\test.txt")
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
$row = 0
While 1
GUICtrlSetData($Input1, FileReadLine($file)) ; reads only the first Line of the file
If @error = -1 Then ExitLoop

; Add lines
$column = 0
_GUICtrlListView_AddItem($hListView, $Input1, $column) ;Row 1: Col 1
$column = $column + 1
_GUICtrlListView_AddSubItem($hListView, $row, "Row 1: Col 2", $column) ;Row 1: Col 2
$column = $column + 1
_GUICtrlListView_AddSubItem($hListView, 0, "Row 1: Col 3", 2) ;Row 1: Col 3
$row = $row + 1

WEnd
EndSwitch
WEnd
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc ;==>_Main

The file I'm working with test.txt has this in it:

2,1,"72496884:My mailbox","","My mailbox"
0,0,"1BD73CF2:5FDA:AS400LST","72496884:My mailbox","AS/400 Info"
0,0,"0D503CFA:5FDB:BPCSINFO","72496884:My mailbox","zBPCS info"
0,0,"1F0B3D0E:5FDD:FILEMAIL","72496884:My mailbox","zFiles by Mail"
0,0,"1CF43D15:5FDE:IN-MAIL","72496884:My mailbox","In-comming Saved Mail "

I only want the lines with 0,0 at the begging of the line.

For column one I need the four characters after the first Colin. So the first example would be 5FDA

For column two I need all characters after the second Colin up until the second ". So the first example would be AS400LST

For column three I need all characters in between the last set of quotes. So the first example would be AS/400 Info

I will be putting that just before ; Add lines

I don't know how to manipulate strings to separate these out.

Thank you,

Docfxit

Edited by Docfxit

Share this post


Link to post
Share on other sites

Docfxit,

Try this: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

_Main()

Func _Main()

    Local $aLines, $aSplit_1, $aSplit_2, $iIndex, $sString

    Local $GUI = GUICreate("List of folders", 400, 300)

    Local $hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 396, 250)
    _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    ; Add columns
    _GUICtrlListView_InsertColumn($hListView, 0, "FileID", 100)
    _GUICtrlListView_InsertColumn($hListView, 1, "FileName", 100)
    _GUICtrlListView_InsertColumn($hListView, 2, "FolderName", 100)

    Local $Button1 = GUICtrlCreateButton("Read", 10, 260, 80, 30)

    GUISetState()

    ; Loop until user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button1
                If Not FileExists("test.txt") Then
                    MsgBox(0, "Error", "File does not exist")
                    Exit
                EndIf
                ; Read the file into an array
                _FileReadToArray("test.txt", $aLines)
                ; Loop through the lines
                For $i = 1 To $aLines[0]
                    ; Check it begins with 0,0
                    If StringLeft($aLines[$i], 3) = "0,0" Then
                        ; Now split the line
                        $aSplit_1 = StringSplit($aLines[$i], ",")
                        ; For the first line - split on :
                        $aSplit_2 = StringSplit($aSplit_1[3], ":")
                        ; Add the central section to the ListView
                        $iIndex = _GUICtrlListView_AddItem($hListView, $aSplit_2[2])
                        ; Now the end section
                        $sString = StringReplace($aSplit_2[3], '"', '')
                        _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 1)
                        ; Finally the end string
                        $sString = StringReplace($aSplit_1[5], '"', '')
                        _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 2)
                    EndIf
                Next
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>_Main

If you post scripts for debugging, do at least make sure they can at least be run and have all the controls necessary. Expecting people to add code just to get the script to run is not really a good idea of you want help. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

M23

Thank you very much. I had no idea what to start with to split the input. I really appreciate your generosity in showing me how to split the lines and how to clean it up.

Thank you,

Docfxit

Share this post


Link to post
Share on other sites

I'm trying to figure out why my MsgBox's aren't displaying the strings.

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

_Main()

Func _Main()

    Local $aLines, $aSplit_1, $aSplit_2, $iIndex, $sString

    Local $GUI = GUICreate("List of folders", 400, 300)

    Local $hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 396, 250)
    _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    ; Add columns
    _GUICtrlListView_InsertColumn($hListView, 0, "FileID", 100)
    _GUICtrlListView_InsertColumn($hListView, 1, "FileName", 100)
    _GUICtrlListView_InsertColumn($hListView, 2, "FolderName", 100)

    Local $Button1 = GUICtrlCreateButton("Read", 10, 260, 80, 30)

    GUISetState()

    ; Loop until user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button1
                If Not FileExists("C:\Scratch\Scratch11\HIERARCH.PM") Then
                    MsgBox(0, "Error", "File does not exist")
                    Exit
                EndIf
                ; Read the file into an array
                _FileReadToArray("C:\Scratch\Scratch11\HIERARCH.PM", $aLines)
                ;                _ArrayDisplay($aLines)
                ; Loop through the lines
                For $i = 1 To $aLines[0]
                    ; Check it begins with 0,0

                    If StringLeft($aLines[$i], 3) = "0,0" Then
                        ; Now split the line
                        $aSplit_1 = StringSplit($aLines[$i], ",")
                        MsgBox(4096, "", "aSplit_1 -" & $aSplit_1 & "- " & "$i -" & $i & "-") ; this MsgBox isn't displaying anything for $aSplit_1
                        ; For the first line - split on :
                        $aSplit_2 = StringSplit($aSplit_1[3], ":")
                        MsgBox(4096, "", "aSplit_2 -" & $aSplit_2 & "- " & "$i -" & $i & "-") ; this MsgBox isn't displaying anything for $aSplit_2
                        ; Add the central section to the ListView
                        $iIndex = _GUICtrlListView_AddItem($hListView, $aSplit_2[2])
                        ; Now the end section
                        $sString = StringReplace($aSplit_2[3], '"', '')
                        MsgBox(4096, "", "$sString -" & $sString & "- " & "$i -" & $i & "-")
                        _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 1)
                        ; Finally the end string
                        $sString = StringReplace($aSplit_1[5], '"', '')
                        MsgBox(4096, "", "$sString -" & $sString & "- " & "$i -" & $i & "-")
                        _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 2)
                    EndIf
                Next
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>_Main

I'm getting an error when it gets to the last line:

2,1,"72496884:My mailbox","","My mailbox"
0,0,"1BD73CF2:5FDA:AS400LST","72496884:My mailbox","AS/400 Info"
0,0,"0D503CFA:5FDB:BPCSINFO","72496884:My mailbox","zBPCS info"
0,0,"1F0B3D0E:5FDD:FILEMAIL","72496884:My mailbox","zFiles by Mail"
0,0,"1CF43D15:5FDE:IN-MAIL","72496884:My mailbox","In-comming Saved Mail "
0,0,"","72496884:My mailbox",Name_Unavailable

I actually have a lot more lines in the file. I wasn't thinking the last line would cause an error. That line can be skipped. How can I skip it?

Thank you very much,

Docfxit

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

That's because StringSplit is giving an array as a result, and your var $aSplit_1 is an array. So in order to use this, you will have to type $aSplit_1[1], or $aSplit_1[2], $aSplit_1[3], etc... all the way up to $aSplit_1[$aSplit_1[0]]. In your case, for function: $aSplit_1 = StringSplit($aLines[$i], ",") you will get:

$aSplit_1[0] = 5

$aSplit_1[1] = 0

$aSplit_1[2] = 0

$aSplit_1[3] = "1BD73CF2:5FDA:AS400LST"

$aSplit_1[4] = "72496884:My mailbox"

$aSplit_1[5] = "AS/400 Info"

edit: typo error for the var $aSplit_1

Edited by dragan

Share this post


Link to post
Share on other sites

dragan

Thank you for catching that. I didn't see it was an array.

Thanks,

Docfxit

Share this post


Link to post
Share on other sites

M23

Now I understand how you broke everything out. That is brilliant. I never would have figured that out on my own. You should get a medal for that.

Thank you for all you do. That's really great.

Docfxit

Share this post


Link to post
Share on other sites

Docfxit,

Did you solve the problem with the last line in that example? :huh:

If not, then you just need to add some errorchecking to the script to check that there is something to split when you ask AutoIt to separate the different parts of the line. Otherwise when you try to split the section to extract the various elements and place them into the ListView there is nothing there to work with! :o

Let me know if you need some more pointers and I will show you what I mean. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Could you please help me with that. I don't know how to do the error checking.

Thanks,

Docfxit

Share this post


Link to post
Share on other sites

Docfxit,

The problem arises becase when you try to split the "" element of this line:

0,0,"","72496884:My mailbox",Name_Unavailable

there is nothing in it to split. So we need to check that we did actually split something and that the various elements exist before we try to use them. We do it by checking the resulting array as you can see in the <<<<<<<<< lines below:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

_Main()

Func _Main()

    Local $aLines, $aSplit_1, $aSplit_2, $iIndex, $sString

    Local $GUI = GUICreate("List of folders", 400, 300)

    Local $hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 396, 250)
    _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    ; Add columns
    _GUICtrlListView_InsertColumn($hListView, 0, "FileID", 100)
    _GUICtrlListView_InsertColumn($hListView, 1, "FileName", 100)
    _GUICtrlListView_InsertColumn($hListView, 2, "FolderName", 100)

    Local $Button1 = GUICtrlCreateButton("Read", 10, 260, 80, 30)

    GUISetState()

    ; Loop until user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button1
                If Not FileExists("test.txt") Then
                    MsgBox(0, "Error", "File does not exist")
                    Exit
                EndIf
                ; Read the file into an array
                _FileReadToArray("test.txt", $aLines)
                ; Loop through the lines
                For $i = 1 To $aLines[0]
                    ; Check it begins with 0,0
                    If StringLeft($aLines[$i], 3) = "0,0" Then
                        ; Now split the line
                        $aSplit_1 = StringSplit($aLines[$i], ",")
                        ; For the first line - split on :
                        $aSplit_2 = StringSplit($aSplit_1[3], ":")
                        ; Now check there was a split <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                        If $aSplit_2[0] >= 3 Then
                            ; Add the central section to the ListView
                            $iIndex = _GUICtrlListView_AddItem($hListView, $aSplit_2[2])
                            ; Now the end section
                            $sString = StringReplace($aSplit_2[3], '"', '')
                            _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 1)
                            ; Finally the end string - if there is something to add <<<<<<<<<<<<<<<<<<<<<<<<
                            If $aSplit_1[0] >= 5 Then
                                $sString = StringReplace($aSplit_1[5], '"', '')
                                _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 2)
                            EndIf
                        EndIf
                    EndIf
                Next
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>_Main

Al clear now? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

You just have to make sure the syntax remains the same for all lines, in the last line if you have:

0,0,"::","72496884:My mailbox","Name_Unavailable"

It wont crash, problem is the script doesn't find the : :

EDIT: arrived late. lol

Edited by careca

Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites

M23,

That is super. It's really great what you can do.

Thank you very much. It works perfect now.

Docfxit

Share this post


Link to post
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
Sign in to follow this  
Followers 0