Jump to content

Recommended Posts

Posted (edited)

I'm confused about the return value of stringsplit  :blink:

From the help:

If no delimiters were found then the @error flag is set to 1, count is equal to 1 ($aArray[0]) and the full string is returned ($aArray[1]).

For checking if there is an error for any function i'm always do:

$aTest = StringSplit($Var, "|")

If @error Then etc.

But if i have string like this:

$Var = "TEST" & "|"

Well with that $Var give me @error = 1 and the retuned array is:

[0] = 1

[1] = Test

The help say "If no delimiters were found", but i have the delimiter in the string "TEST|". Is normal this type of return or the help-function miss of something?

So for checking the error of StringSplit, example like nothing to Split instead of:

$aTest = StringSplit($Var, "|")

If @error Then etc.

I'm doing:

$aTest = StringSplit($Var, "|")

If $aTest[1] = "" Then

I'm correct to replace the @error with $aTest[1] for error checking or can create some problem? Sorry for nocode tag but i'm on a phone :D

Edited by MyEarth
Posted

What version of AutoIt are you using please? This works...

#include <Array.au3>

Local $sString = 'Example|'
Local $aSplit = StringSplit($sString, '|')
ConsoleWrite('@error = ' & @error & ', UBound() = ' & UBound($aSplit) & @CRLF)
_ArrayDisplay($aSplit)

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

#include <Array.au3>

Local $sString = 'TEST|'
Local $aSplit = StringSplit($sString, '|') ; give me and empty value [2] = ""
ConsoleWrite('@error = ' & @error & ', UBound() = ' & UBound($aSplit) & @CRLF)
_ArrayDisplay($aSplit)

Local $sString = 'TEST|'
Local $aSplit = StringSplit(StringTrimRight($sString, 1), '|')
ConsoleWrite('@error = ' & @error & ', UBound() = ' & UBound($aSplit) & @CRLF)
_ArrayDisplay($aSplit)

Local $sString = 'TEST|TOOL|'
Local $aSplit = StringSplit(StringTrimRight($sString, 1), '|')
ConsoleWrite('@error = ' & @error & ', UBound() = ' & UBound($aSplit) & @CRLF)
_ArrayDisplay($aSplit)

Yes you have right. I have read better my code and i have found what is the error:

The first version give me no error but i have an empty value in [2], that create me some problem because there is only one "real value" and not two

The second give me error because i remove the delimeter but don't give me any empty array value

The third with two or more element work fine without error, i'm always append a last "|"

So or i'm redim the array, or i'm deleting the empty array value, or i'm using If $aSplit[1] = "" Then instead of @error or if you have another solution...

Edited by MyEarth
Posted

It's not a bug, you need to strip the last delimiter.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

I don't have say its a bug. But if i'm strip the last delimiter with a string like:

"TEST|"

I have the @error = 1 because no delimeter was found, but strip the last char in a string like:

"TEST|TOOL|"

Work fine without any @error

So how i can i know if the @error is real or not? :D

Of is unecessary to use @error for this function because in any way i have always an Array?

Edited by MyEarth
Posted

The @error isn't really an error in the normal sense of the word. Anyway this is what you want I think...

#include <Array.au3>
#include <StringConstants.au3>

Local $sString = 'Example|'
Local $aSplit = MyStringSplit($sString, '|-')
ConsoleWrite('@error = ' & @error & ', UBound() = ' & UBound($aSplit) & @CRLF)
_ArrayDisplay($aSplit)

Func MyStringSplit($sString, $sDelim, $iFlag = Default)
    If $iFlag = Default Then $iFlag = 0
    $sString = StringRegExpReplace($sString, '[' & StringRegExpReplace($sDelim, '([]^\-\\[])', '\\\1') & ']*$', '')
    Local $aSplit = StringSplit($sString, $sDelim, $iFlag)
    Return $aSplit
EndFunc   ;==>MyStringSplit

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

#include <Array.au3>

; Or if you understand regular expressions then knock yourself out!
Local $aSRE = StringRegExp('Example|Example|Example', '([^|]+)', 3)
_ArrayDisplay($aSRE)

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Another approach.

#include <Array.au3>
#include <StringConstants.au3>

Local $sString = 'Example\E'
Local $aSplit = MyStringSplit($sString, '\E', $STR_ENTIRESPLIT)
ConsoleWrite('@error = ' & @error & ', UBound() = ' & UBound($aSplit) & @CRLF)
_ArrayDisplay($aSplit)

Func MyStringSplit($sString, $sDelim, $iFlag = Default)
    If $iFlag = Default Then $iFlag = 0
    $sString = StringRegExpReplace($sString, '[\Q' & StringRegExpReplace($sDelim, '\\([eEqQ])', '\1\\') & '\E]*$', '')
    Local $aSplit = StringSplit($sString, $sDelim, $iFlag)
    Return $aSplit
EndFunc   ;==>MyStringSplit

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
  • Recently Browsing   0 members

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