Jump to content
Sign in to follow this  
Ghost21

string split error catch

Recommended Posts

Ghost21

computer:user:45ms

$splitdata = StringSplit($pcdata, ":")

$PC = $splitdata[1]

$USER = $splitdata[2]

$PING = $splitdata[3]

but if it doesn't find a : it errors out.. I was thinking something like if @error <> 0 then exitloop .

doesn't seem to be working

???

Share this post


Link to post
Share on other sites
stampy

Assuming $pcdata = 'computer:user:45ms' it worked for me. Are you sure your variable is a string and has a colon in it?

Share this post


Link to post
Share on other sites
Ghost21

Assuming $pcdata = 'computer:user:45ms' it worked for me. Are you sure your variable is a string and has a colon in it?

well it works great if that syntax is like that but if I put like dsfhlsdhdlsfh it crashes the program

Share this post


Link to post
Share on other sites
BrettF

well it works great if that syntax is like that but if I put like dsfhlsdhdlsfh it crashes the program

If IsArray ($splitdata) Then... Edited by Bert

Share this post


Link to post
Share on other sites
picaxe

The help file says

Return Value

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

So I don't think "IsArray($splitdata)" will work.

"@error <> 0 exit loop" should work.

Are you checking @error immediately after StringSplit or saving it in a var for later checking. Or else try checking for $splitdata[0]=1 And $splitdata[1]==$pcdata

Share this post


Link to post
Share on other sites
MHz

$splitdata = StringSplit($pcdata, ":")

$PC = $splitdata[1]

$USER = $splitdata[2]

$PING = $splitdata[3]

You expect 3 elements and $splitdata[0] will contain the count of all elements in the array, so 4 elements is what you need to check for by using UBound() or by checking the value of $splitdata[0]. You can also check @error to whether the StringSplit failed and if it did, then indexing the array above $splitdata[1] would cause error. Failure returns the complete string in $splitdata[1].

$pcdata = '1:2:3'
$splitdata = StringSplit($pcdata, ":")
If UBound($splitdata) = 4 Then
    $PC = $splitdata[1]
    $USER = $splitdata[2]
    $PING = $splitdata[3]
    MsgBox(0, '', $PC & @CRLF & $USER & @CRLF & $PING)
EndIf

How you decide to check depends on your requirement.

:D

Share this post


Link to post
Share on other sites
Ghost21

$pcdata = '1:2:3'
If StringInStr($pcdata, ":") Then
$splitdata = StringSplit($pcdata, ":")
If UBound($splitdata) = 4 Then
    $PC = $splitdata[1]
    $USER = $splitdata[2]
    $PING = $splitdata[3]
    MsgBox(0, '', $PC & @CRLF & $USER & @CRLF & $PING)
EndIf
Else
;Error
EndIf

Thanks for the great help everyone... I will have to look into Ubound somemore..

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.