Jump to content

string split error catch


Ghost21
 Share

Recommended Posts

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

???

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 11 years later...

 

edit: more complete example showing behavior with various numbers of colons (including none) and made the msgbox title useful.

#include<array.au3>

$str = "computer:user:45ms"
;~ $str = "computer:user45ms"
;~ $str = "computeruser45ms"
local $arr[0]

for $i = 0 to _ArrayAdd($arr , $str , 0 , ":")
    msgbox(0, $i + 1 & ' of ' & ubound($arr), $arr[$i])
next

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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