Jump to content

StringSplit Example


Go to solution Solved by pixelsearch,

Recommended Posts

Good day,

As noted in the comments...

#cs
        $g_SetName holds the path to the Type_# data folder.
        This variable is very important...
        Thus, I require a means of determining the specific "type" of data [Type_1, Type_2, and so on] that was previously selected
        This selection will determine the master Type#.edl data file that is to be selected...and then deployed.
        An example path is: F:\Audio\Type_1\TestMe
        So, $_aGetType[3] = "Type_1" in this example.
        Requirements: 3 empty Type_# folders namely, Type_1, Type_2 and Type_3
#ce

Here is the code...

Func _BrowseForFolder()
    Local $g_SetName = FileSelectFolder("Please select the Set_Name folder...", "")
    ; -----------------------------------------------
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "No Set_Name folder was selected." & @CRLF & "Please select a Set_Name folder")
        _BrowseForFolder()
    Else
        MsgBox($MB_ICONNONE, "You chose the following Set_Name folder:", $g_SetName)

        #cs
        $g_SetName holds the path to the Type_# data folder.
        This variable is very important...
        Thus, I require a means of determining the specific "type" of data [Type_1, Type_2, and so on] that was previously selected
        This selection will determine the master Type#.edl data file that is to be selected...and then deployed.
        An example path is: F:\Audio\Type_1\TestMe
        So, $_aGetType[3] = "Type_1" in this example.
        Requirements: 3 empty Type_# folders namely, Type_1, Type_2 and Type_3
        #ce

        Local $_aGetType = StringSplit($g_SetName, "\")

        If $_aGetType[3] = "Type_1" Then
            MsgBox($MB_SYSTEMMODAL, "", "Carry on with Type_1...")
            ; _FunctionType_1
        ElseIf $_aGetType[3] = "Type_2" Then
            MsgBox($MB_SYSTEMMODAL, "", "Carry on with Type_2...")
            ; _FunctionType_2
        Else
            MsgBox($MB_ICONNONE, "You have chosen an inappropriate Type_# folder!:", "Prepare to die!")
        EndIf
    EndIf
EndFunc   ;==>_BrowseForFolder
; -----------------------------------------------

What I would very much appreciate, is:

1) Someone confirming that this script is indeed..."...doing what it should?"
2) Determining if there is a more preferable means of error checking than "If...ElseIf...Else...EndIf"?

As noted, the selection of a specific Type_# folder will determine the master Type#.edl data file that is to be selected...of which will then be later deployed.

Any comments and|or suggestion would be greatly appreciated.

Thank you for your time!

 

Link to comment
Share on other sites

@mr-es335 it's great that you experiment by yourself AutoIt functions to improve your knowledge with this language.

But when you do your tests (like you certainly did with this function), you should broaden the scope of testing, asking yourself : "what would happen if I the user selects a short path, for example C:\Temp which got only 1 backslash"

If you do this test, then you'll notice a fatal error, I let you check how to fix it :

If $_aGetType[3] = "Type_1" Then
If ^ ERROR

 

Link to comment
Share on other sites

pixelsearch,

I came across this very thing!!

The error is - and which occurs, as you note, when only the [Select Folder] button is selected:

"I:\Live_Rig\Show Development\Working\_BrowseForFolder.au3" (21) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If $_aGetType[3] = "Type_1" Then
If ^ ERROR

Though I somewhat understand  what the error message is referring to, I really have no idea how to fix this one?!?

Link to comment
Share on other sites

@mr-es335 i don't know about other users but when I started to learn AutoIt and a new function (like StringSplit) was needed in a script I was writing, then I put the script "on hold" and do many "little tests" I could with this new function (StringSplit), trying to understand exactly how it works, even if it will take me hours ! Gladly the help file is a big help and after these tests were done, only then I went back to the script to continue it.

Now you know that your issue concerns the array $_aGetType[] and this array was returned by... StringSplit, then you should take a little time to do some external little tests to understand SpringSplit a bit more, for example :

#include <Array.au3>

_Split("F:")
_Split("F:\")
_Split("F:\Audio")
_Split("F:\Audio\Type_1")
_Split("F:\Audio\Type_1\TestMe")
_Split("F:\Audio\Type_1\TestMe\")

Func _Split($sString)
    Local $aArray = StringSplit($sString, "\")
    If @error Then ConsoleWrite($sString & "  error = " & @error & " (no delimiter found)" & @crlf)
    _ArrayDisplay($aArray, $sString)
EndFunc

Now you should focus on the value of the 1st element of the array, i.e. $aArray[0] which always exists in this example because we didn't use the flag $STR_NOCOUNT . As you can see, its value goes from 1 to 5 in this example, and the help file is clear about that :

StringSplit returns an array, by default the first element ($aArray[0]) contains the number of strings returned, the remaining elements ($aArray[1], $aArray[2], etc.) contain the delimited strings.

So by testing the value of $aArray[0] you should achieve your goal (i.e. if the value is < 3 then you can be sure that the user didn't select the 'Type_1' subfolder, because when 'Type_1' is found in the selected path, then $aArray[0] >= 3

3 if "F:\Audio\Type_1"
4 if "F:\Audio\Type_1\TestMe"
5 if "F:\Audio\Type_1\TestMe\"
etc...

Hope it helps

Link to comment
Share on other sites

pixelsearch,

Two some degree "Yes!"

However, and this AN IMPORTANT however, my brain is wired for "methodology"...NOT FOR LOGIC!

This is why...though I love "programming"....I have always struggled with the "logic part".

I do hope that this makes sense?

Observations
1. Though I can handle the response to the user selecting"Cancel"...AND...
2. Though I now what to do with the string "Type_1...AND...
3. And  I now what to do with the string "Type_2"...
4. I have NO  IDEA what do with $aArray[0]<> 3!!

Edited by mr-es335
Link to comment
Share on other sites

  • Solution

@mr-es335 I would go on with something like this :

#include <MsgBoxConstants.au3>

_BrowseForFolder()

Func _BrowseForFolder()
    While 1
        Local $g_SetName = FileSelectFolder("Please select the Set_Name folder...", "")
        ; -----------------------------------------------
        If @error Then
            MsgBox($MB_TOPMOST, "", "No Set_Name folder was selected." & @CRLF & "Please select a Set_Name folder")
            ContinueLoop
        EndIf

        MsgBox($MB_TOPMOST, "You chose the following Set_Name folder:", $g_SetName)

        #cs
        $g_SetName holds the path to the Type_# data folder.
        This variable is very important...
        Thus, I require a means of determining the specific "type" of data [Type_1, Type_2, and so on] that was previously selected
        This selection will determine the master Type#.edl data file that is to be selected...and then deployed.
        An example path is: F:\Audio\Type_1\TestMe
        So, $_aGetType[3] = "Type_1" in this example.
        Requirements: 3 empty Type_# folders namely, Type_1, Type_2 and Type_3
        #ce

        Local $_aGetType = StringSplit($g_SetName, "\")

        Select
            Case $_aGetType[0] < 3
                MsgBox($MB_TOPMOST, "Retry", "Path selected is too short") ; user didn't select folders "Type_1" or "Type_2"
                ContinueLoop

            Case $_aGetType[3] = "Type_1"
                MsgBox($MB_TOPMOST, "", "Carry on with Type_1...")
                ; _FunctionType_1
                ExitLoop

            Case $_aGetType[3] = "Type_2"
                MsgBox($MB_TOPMOST, "", "Carry on with Type_2...")
                ; _FunctionType_2
                ExitLoop

            Case Else
                MsgBox($MB_TOPMOST, "You have chosen an inappropriate Type_# folder!:", "Prepare to die!")
                ExitLoop
        EndSelect
    Wend
EndFunc   ;==>_BrowseForFolder
; -----------------------------------------------

Edit: I'm not really satisfied that the user can't exit the function when he cancels the FileSelectFolder dialog, but it's your decision, as you scripted it in a recursive way in your script, calling _BrowseForFolder() when inside _BrowseForFolder() if the user cancels the dialog window.

I changed the dangerous recursion way to a safer While...Wend loop, but kept the functionality you scripted (e.g. the user can't quit the loop until he chooses a path containing at least 2 backslash)

Edited by pixelsearch
Link to comment
Share on other sites

pixelsearch,

Having what I would refer to as "a working knowledge" of the available syntax is indeed significant...as can be clearly demonstrated here with the deployment of "ContinueLoop"!

You have resolved two my previous issues here:

1) Determining if there is a more preferable means of error checking than "If...ElseIf...Else...EndIf"?
• It would appear, that Select|Case, is. if I may used the term "cleaner", than If...Then's...

2) That the employment of Select|Case appears to highlight, what I would refer to as, "the order of precedence" and the deployment of "exit codes".

Lastly, that, for whatever reason, the employment of "FileSelectFolder", seemed to imply the employment of "StringSplit" [...at least from the many examples that I have come across...] ...where the employment of "StringExtraction" may have been more appropriate....especially here....my present situation?

What do you think?

As always, pixelsearch, I do so very, very, very much appreciate your efforts pm my behalf. I have learnt a great deal today!
• Just curious, did you happen to appreciate my hopefully, "sense of humor" with regards to "MsgBox($MB_ICONNONE, "You have chosen an inappropriate Type_# folder!:", "Prepare to die!")?

Edited by mr-es335
Link to comment
Share on other sites

44 minutes ago, mr-es335 said:

...where the employment of "StringExtraction" may have been more appropriate....especially here....my present situation?

You're right, the string extraction should be preferred to StringSplit in your case. I had it in mind constantly but as you named the topic "StringSplit Example" then I went on with StringSplit

46 minutes ago, mr-es335 said:

Just curious, did you happen to appreciate my hopefully, "sense of humor" with regards to "MsgBox($MB_ICONNONE, "You have chosen an inappropriate Type_# folder!:", "Prepare to die!")?

I felt immediately that "the dye-dying message" was humor, with a user who can't even select a folder correctly, what a pity :D

Concerning $MB_ICONNONE (in your script) which in fact is equal to 0, I guess you used it twice in place of $MB_SYSTEMMODAL because you wanted the title of the messagebox to be fully displayed and not truncated, as $MB_SYSTEMMODAL truncates the title when it's too long.

That's one of the reason why I always use $MB_TOPMOST (which doesn't truncate the title) instead of $MB_SYSTEMMODAL

Link to comment
Share on other sites

pixelsearch,

The line, Prepare to die!" is for a classic entitled, "The Princess Bride".

You stated, "I felt immediately that "the dye-dying message" was humor, with a user who can't even select a folder correctly, what a pity."
• Was the latter part of this comment meant to be "specific"? If so, what do you mean by this? Just curious.

 

Link to comment
Share on other sites

pixelsearch,

Following is my updated script:

#include <MsgBoxConstants.au3>

_BrowseForFolder()

Func _BrowseForFolder()
    While 1
        Local $g_SetName = FileSelectFolder("Please select the Set_Name folder...", "")
        ; -----------------------------------------------
        If @error Then
            MsgBox($MB_TOPMOST, "", "No Set_Name folder was selected." & @CRLF & "Please select a Set_Name folder")
            ContinueLoop
        EndIf

        ;MsgBox($MB_TOPMOST, "You chose the following Set_Name folder:", $g_SetName)

        Local $_aGetType = StringSplit($g_SetName, "\")

        Select
            Case $_aGetType[0] < 3
                ;MsgBox($MB_TOPMOST, "Retry", "Path selected is too short") ; user didn't select folders "Type_1" or "Type_2"
                MsgBox($MB_TOPMOST, "NOTICE!", "You need to select a Type_# folder...")
                ContinueLoop

            Case $_aGetType[3] = "Type_1"
                MsgBox($MB_TOPMOST, "", "Carry on with Type_1...")
                ; _FunctionType_1
                ExitLoop

            Case $_aGetType[3] = "Type_2"
                MsgBox($MB_TOPMOST, "", "Carry on with Type_2...")
                ; _FunctionType_2
                ExitLoop

            Case Else
                ;MsgBox($MB_TOPMOST, "You have chosen an inappropriate Type_# folder!:", "Prepare to die!")
                ;ExitLoop
                MsgBox($MB_TOPMOST, "NOTICE!", "You need to select the correct Type_# folder...")
                ContinueLoop
        EndSelect
    WEnd
EndFunc   ;==>_BrowseForFolder
; -----------------------------------------------

Comments?

PS: I will be looking further into "StringExtraction"...

Link to comment
Share on other sites

37 minutes ago, mr-es335 said:

Was the latter part of this comment meant to be "specific"? If so, what do you mean by this? Just curious.

Not specific at all and I meant nothing :)

12 minutes ago, mr-es335 said:

Comments?

Your revised script is ok for your own use because you got the "Type_1" and "Type_2" folders on your computer. But now anybody on the Forum launching your example will be stuck inside the loop without a way to end the script gracefully, no matter they choose a path containing numerous backslash.

Maybe a warning before your script should indicate this, you'll decide.

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