Jump to content
edumanilha

Can't compare split results...

Recommended Posts

while 1
Global $destino = ControlGetText("GUI2", "", $Final)
    $IPdestino = StringSplit($destino,".")
    
        ;MsgBox($MB_SYSTEMMODAL, "CAMPO EM BRANCO!!!",$destino & $IPdestino[1] & $IPdestino[2] & $IPdestino[3] & $IPdestino[4],10)
        
    If $IPdestino[0] Or $IPdestino[1] Or $IPdestino[2] Or $IPdestino[3] = ""  then
                                                            MsgBox($MB_SYSTEMMODAL, "CAMPO EM BRANCO!!!","one two or three are empty",10)
    ElseIf $IPdestino[4] <> "" Then
                                                            MsgBox($MB_SYSTEMMODAL, "CAMPO EM BRANCO!!!","4 filled",10)
            If $IPdestino[2] + $IPdestino[3] <> $Filial Then
                            $Filial = $IPdestino[2]+$IPdestino[3]
                            GUICtrlSetData($labelfilialdest,"Filial: " & $Filial)
            EndIf
    EndIf
    
wend

Hello,

I have this code, the first part get text from a input, then split the result (IP adress), and then I tried to filter the actions, to do nothing on blank results and do something when the 4 octets are filled... But I cant entrer the IF's... I don't know why...Maybe something silly because I spent all day long playing with codes...

I put the msgbox for diagnosis, and I can see until [4] have values...I entered the [1],[2],[3] blank check part even with they filled and the [4] check I can't arrive....

Speaking about Ip adresses...Someone have a nice IP check filter? only numbers, ony full filled etc etc... ?

Thanks in  advance!

Edited by edumanilha

Share this post


Link to post
Share on other sites

@edumanilha
Each of the condition in your If must be compared to something, and now you are just comparing the third element of the $IPdestino array with a blank value.

If $IPdestino[0] = "" Or $IPdestino[1] = "" Or $IPdestino[2] = "" Or $IPdestino[3] = "" Then
    ; Statements
Else
    ; Other statements
EndIf

Or, using a Loop:

For $i = 0 To UBound($IPdestino) - 1 Step 1
    If $IPdestino[$i] == "" Then 
        ConsoleWrite("Field " & $i & " is blank!" & @CRLF)
        Exit For
    EndIf
Next
3 hours ago, edumanilha said:

Speaking about Ip adresses...Someone have a nice IP check filter? only numbers, ony full filled etc etc... ?

Regular Expressions is the way to do it (not the only one, but the most efficent) :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

A simple search will return various examples for an IP filter, some with RegEx and some without.

Here is an example without RegEx (Thanks goes to @BrewManNH ) :

https://www.autoitscript.com/wiki/Snippets_(_Internet_)#ValidIP

Spoiler
#include <Array.au3> ; This is only needed for the _ArrayDisplay function used in the example below, and is not needed for the _ValidIP function

; IPv4 validation script
Global Const $IPAddress = "192.168.2.1"

Global Const $Return = _ValidIP($IPAddress)

If $Return < 0 Then
    Switch @error
        Case 1
            MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 1 - IP address starts with an invalid number = 0, 127 , 169 or is > 239")
        Case 2
            MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 2 - one of the octets of the IP address is out of the range 0-255 or contains invalid characters")
        Case 3
            MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 3 - IP Address is not a valid dotted IP address (ex. valid address 190.40.100.20)")
        Case 4
            MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 4 - Last octet is either 0 or 255")
    EndSwitch
Else
    MsgBox(48, "", $IPAddress & " is a valid Class " & $Return[5] & " IP address")
    _ArrayDisplay($Return)
EndIf

; FUNCTION# ===========================================================================================================
; Name...........: _ValidIP
; Description ...: Verifies whether an IP address is a valid IPv4 address or not
; Syntax.........: _ValidIP($sIP)
; Parameters ....: $sIP - IP address to validate
;
; Return values .: Success - Array containing split IP Address, IP address in Hex, and the Class of the IP address
;                            array[0] - [3] = the IP address split into octets
;                            array[4]       = IP address in Hex
;                            array[5]       = Class of the IP address [A through D]
;                  Failure - -1, sets @error
;                  |1 - IP address starts with an invalid number = 0, 127 , 169 or is > 239
;                  |2 - one of the octets of the IP address is out of the range 0-255 or contains invalid characters
;                  |3 - IP Address is not a valid dotted IP address (ex. valid address 190.40.100.20)
;                  |4 - Last octet ends in 0 or 255 which are invalid for an IP address
; Author ........: BrewManNH
; Modified.......:
; Remarks .......: This will accept an IP address that is 4 octets long, and contains only numbers and falls within
;                  valid IP address values. Class A networks can't start with 0 or 127. 169.xx.xx.xx is reserved and is
;                  invalid and any address that starts above 239, ex. 240.xx.xx.xx is reserved. The address range
;                  224-239 1s reserved as well for Multicast groups but can be a valid IP address range if you're using
;                  it as such. Any IP address ending in 0 or 255 is also invalid for an IP
; Related .......:
; Link ..........:
; Example .......: Yes
; =====================================================================================================================
Func _ValidIP($sIP)
    Local $adIPAddressInfo[6]

    Local $aArray = StringSplit($sIP, ".", 2)

    If Not IsArray($aArray) Or UBound($aArray) <> 4 Then Return SetError(3, 0, -1)

    Local $dString = "0x"

    If $aArray[0] <= 0 Or $aArray[0] > 239 Or $aArray[0] = 127 Or $aArray[0] = 169 Then
        Return SetError(1, 0, -1)
    EndIf

    For $I = 0 To 3
        If $I < 3 Then
            If $aArray[$I] < 0 Or $aArray[$I] > 255 Or Not StringIsDigit($aArray[$I]) Then
                Return SetError(2, 0, -1)
            EndIf
        Else
            If Not StringIsDigit($aArray[$I]) Then
                Return SetError(2, 0, -1)
            EndIf

            If $aArray[$I] < 1 Or $aArray[$I] > 254 Then
                Return SetError(4, 0, -1)
            EndIf
        EndIf

        $dString &= StringRight(Hex($aArray[$I]), 2)

        $adIPAddressInfo[$I] = $aArray[$I]
    Next

    $adIPAddressInfo[4] = $dString

    Switch $aArray[0]
        Case 1 To 126
            $adIPAddressInfo[5] = "A"
            Return $adIPAddressInfo
        Case 128 To 191
            $adIPAddressInfo[5] = "B"
            Return $adIPAddressInfo
        Case 192 To 223
            $adIPAddressInfo[5] = "C"
            Return $adIPAddressInfo
        Case 224 To 239
            $adIPAddressInfo[5] = "D"
            Return $adIPAddressInfo
    EndSwitch
EndFunc   ;==>_ValidIP

 

The code is a bit longer, but easier to follow (especially for beginners).

You may e.g. also take a look at the thread : regular-expression-to-confirm-an-ipv4-address

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share this post


Link to post
Share on other sites
5 hours ago, FrancescoDiMuro said:

@edumanilha
Each of the condition in your If must be compared to something, and now you are just comparing the third element of the $IPdestino array with a blank value.

If $IPdestino[0] = "" Or $IPdestino[1] = "" Or $IPdestino[2] = "" Or $IPdestino[3] = "" Then
    ; Statements
Else
    ; Other statements
EndIf

Or, using a Loop:

For $i = 0 To UBound($IPdestino) - 1 Step 1
    If $IPdestino[$i] == "" Then 
        ConsoleWrite("Field " & $i & " is blank!" & @CRLF)
        Exit For
    EndIf
Next

Regular Expressions is the way to do it (not the only one, but the most efficent) :)

I knew it something silly...You know when you spend all day long programming and at some point you don't know what you,re doing anymore? I looked many times and forgot about each Or comparator... Thanks!

Share this post


Link to post
Share on other sites
8 hours ago, Musashi said:

A simple search will return various examples for an IP filter, some with RegEx and some without.

Here is an example without RegEx (Thanks goes to @BrewManNH ) :

https://www.autoitscript.com/wiki/Snippets_(_Internet_)#ValidIP

  Reveal hidden contents
#include <Array.au3> ; This is only needed for the _ArrayDisplay function used in the example below, and is not needed for the _ValidIP function

; IPv4 validation script
Global Const $IPAddress = "192.168.2.1"

Global Const $Return = _ValidIP($IPAddress)

If $Return < 0 Then
    Switch @error
        Case 1
            MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 1 - IP address starts with an invalid number = 0, 127 , 169 or is > 239")
        Case 2
            MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 2 - one of the octets of the IP address is out of the range 0-255 or contains invalid characters")
        Case 3
            MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 3 - IP Address is not a valid dotted IP address (ex. valid address 190.40.100.20)")
        Case 4
            MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 4 - Last octet is either 0 or 255")
    EndSwitch
Else
    MsgBox(48, "", $IPAddress & " is a valid Class " & $Return[5] & " IP address")
    _ArrayDisplay($Return)
EndIf

; FUNCTION# ===========================================================================================================
; Name...........: _ValidIP
; Description ...: Verifies whether an IP address is a valid IPv4 address or not
; Syntax.........: _ValidIP($sIP)
; Parameters ....: $sIP - IP address to validate
;
; Return values .: Success - Array containing split IP Address, IP address in Hex, and the Class of the IP address
;                            array[0] - [3] = the IP address split into octets
;                            array[4]       = IP address in Hex
;                            array[5]       = Class of the IP address [A through D]
;                  Failure - -1, sets @error
;                  |1 - IP address starts with an invalid number = 0, 127 , 169 or is > 239
;                  |2 - one of the octets of the IP address is out of the range 0-255 or contains invalid characters
;                  |3 - IP Address is not a valid dotted IP address (ex. valid address 190.40.100.20)
;                  |4 - Last octet ends in 0 or 255 which are invalid for an IP address
; Author ........: BrewManNH
; Modified.......:
; Remarks .......: This will accept an IP address that is 4 octets long, and contains only numbers and falls within
;                  valid IP address values. Class A networks can't start with 0 or 127. 169.xx.xx.xx is reserved and is
;                  invalid and any address that starts above 239, ex. 240.xx.xx.xx is reserved. The address range
;                  224-239 1s reserved as well for Multicast groups but can be a valid IP address range if you're using
;                  it as such. Any IP address ending in 0 or 255 is also invalid for an IP
; Related .......:
; Link ..........:
; Example .......: Yes
; =====================================================================================================================
Func _ValidIP($sIP)
    Local $adIPAddressInfo[6]

    Local $aArray = StringSplit($sIP, ".", 2)

    If Not IsArray($aArray) Or UBound($aArray) <> 4 Then Return SetError(3, 0, -1)

    Local $dString = "0x"

    If $aArray[0] <= 0 Or $aArray[0] > 239 Or $aArray[0] = 127 Or $aArray[0] = 169 Then
        Return SetError(1, 0, -1)
    EndIf

    For $I = 0 To 3
        If $I < 3 Then
            If $aArray[$I] < 0 Or $aArray[$I] > 255 Or Not StringIsDigit($aArray[$I]) Then
                Return SetError(2, 0, -1)
            EndIf
        Else
            If Not StringIsDigit($aArray[$I]) Then
                Return SetError(2, 0, -1)
            EndIf

            If $aArray[$I] < 1 Or $aArray[$I] > 254 Then
                Return SetError(4, 0, -1)
            EndIf
        EndIf

        $dString &= StringRight(Hex($aArray[$I]), 2)

        $adIPAddressInfo[$I] = $aArray[$I]
    Next

    $adIPAddressInfo[4] = $dString

    Switch $aArray[0]
        Case 1 To 126
            $adIPAddressInfo[5] = "A"
            Return $adIPAddressInfo
        Case 128 To 191
            $adIPAddressInfo[5] = "B"
            Return $adIPAddressInfo
        Case 192 To 223
            $adIPAddressInfo[5] = "C"
            Return $adIPAddressInfo
        Case 224 To 239
            $adIPAddressInfo[5] = "D"
            Return $adIPAddressInfo
    EndSwitch
EndFunc   ;==>_ValidIP

 

The code is a bit longer, but easier to follow (especially for beginners).

You may e.g. also take a look at the thread : regular-expression-to-confirm-an-ipv4-address

I just thought I could seize the oportunity , since I'm talking abou IP's...But thanks for your examples! I'll try!

Share this post


Link to post
Share on other sites
12 hours ago, edumanilha said:

silly...You know when you spend all day long programming and at some point you don't know what you,re doing anymore?

Definitely yes, and it is very annoying! :D

Sometimes it is better to stop working when it's late, and begin the next day with a bunch of hours of sleep.

By the way, happy to have helped :)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...