Jump to content

Quick isnumber and isint problem


Glyph
 Share

Recommended Posts

For some reason "isint" and "isnumber" will not work for me! (I may be using them incorrectly)

#include <File.au3>
Global $impfile = "Proxy1.txt", $expfile = "PFilter" & @YEAR & @MON & @MDAY & "-" & @HOUR & @MIN & @SEC & ".txt"
ConsoleWrite("Pfilter(" & $impfile & ", " & $expfile & ")" & @CRLF)
_pfilter($impfile, $expfile)
Func _pfilter($pf_impfile = "", $pf_expfile = "")
    Local $pf_buffer = "" ;clear buffer
    ConsoleWrite("Welcome to Pfilter()" & @CRLF)
    If $pf_impfile = "" Then
        ConsoleWrite("!Missing import file." & @CRLF)
    Else
        $pf_file = FileOpen($pf_impfile, 0)
        If $pf_file = -1 Then
            ConsoleWrite("!Unable to open file." & @CRLF)
            Exit
        EndIf

        $pf_totallines = _FileCountLines($pf_impfile)
        ConsoleWrite("@Total lines: " & $pf_totallines & @CRLF)
        For $pf_ln = 1 To $pf_totallines
            Local $validproxy = False, $validport = False, $octets = 0, $prelim = 0;reset variables for next line
            $pf_line = FileReadLine($pf_file, $pf_ln)

            $pf_port = StringRegExp($pf_line, ":") ;check for ports
            If $pf_port = 0 Then
                $prelim = $prelim + 0
                ConsoleWrite("!No port. (" & $pf_ln & ")" & @CRLF)
                ;no port!
            Else
                $prelim = $prelim + 1
            EndIf

            $pf_octet = StringRegExp($pf_line, ".") ;check for octets
            If $pf_octet = 0 Then
                $prelim = $prelim + 0
                ConsoleWrite("!Invalid octet length. (" & $pf_ln & ")" & @CRLF)
                ;not enough octets!
            Else
                $prelim = $prelim + 1
            EndIf

            If $prelim = 2 Then
                ConsoleWrite("@Preliminary check passed. (" & $pf_ln & ")" & @CRLF)
            Else
                ConsoleWrite("!Preliminary check failed. (" & $pf_ln & ")" & @CRLF)
            EndIf

            ;so far the checks passed, let's continue filtering.
            $pf_splitport = StringSplit($pf_line, ":")
            If $pf_splitport[0] = 2 Then; so far so good.
                $pf_splitoctet = StringSplit($pf_splitport[1], ".")
                If $pf_splitoctet[0] = 4 Then ;check for 4 octets
                    ConsoleWrite("@Found 4 octets. (" & $pf_ln & ")" & @CRLF);4 octets were found!


                    If isnumber($pf_splitoctet[1]) = 1 Then
                        $octets = $octets + 1
                    Else
                        ConsoleWrite("!Found invalid octet(1): "&$pf_splitoctet[1]&"(" & $pf_ln & ")" & @CRLF)
                    EndIf

                    If isnumber($pf_splitoctet[2]) = 1 Then
                        $octets = $octets + 1
                    Else
                        ConsoleWrite("!Found invalid octet(2): "&$pf_splitoctet[2]&"(" & $pf_ln & ")" & @CRLF)
                    EndIf

                    If isnumber($pf_splitoctet[3]) = 1 Then
                        $octets = $octets + 1
                    Else
                        ConsoleWrite("!Found invalid octet(3): "&$pf_splitoctet[3]&"(" & $pf_ln & ")" & @CRLF)
                    EndIf

                    If isnumber($pf_splitoctet[4]) = 1 Then
                        $octets = $octets + 1
                    Else
                        ConsoleWrite("!Found invalid octet(4): "&$pf_splitoctet[4]&"(" & $pf_ln & ")" & @CRLF)
                    EndIf

                    If isnumber($pf_splitport[2]) = 1 Then
                        $validport = True
                    Else
                        ConsoleWrite("!Found invalid port: "&$pf_splitport[2]&"(" & $pf_ln & ")" & @CRLF)
                    EndIf


                    If $octets = 4 And $validport = True Then
                        $validproxy = True
                        ConsoleWrite("@Validated Proxy: " & $pf_line & @CRLF)
                        $pf_buffer = $pf_buffer & $pf_line & @CRLF
                    Else
                        $validproxy = False
                        ConsoleWrite("!Invalid Proxy: " & $pf_line & @CRLF)
                    EndIf
                EndIf
            Else
                ConsoleWrite("!Invalid proxy. (" & $pf_ln & ")" & @CRLF)
            EndIf
        Next
        FileWrite($pf_expfile, $pf_buffer) ;write the validated proxies
        ConsoleWrite("@Wrote valid proxies to: " & $pf_expfile & @CRLF)
    EndIf
EndFunc   ;==>_pfilter

!Found invalid octet(1): 10(1)

I'm not sure why it's saying "10" is not a number... I would greatly appreciate any help. This seems like a simple problem, I just can't figure it out right now...

I've included the list that I am using.

Proxy1.txt

Edited by Glyph

tolle indicium

Link to comment
Share on other sites

In your situation, I'd say that AutoIt leads 15-0.

No "10" is not a number, it's a string. Not surprising as it comes from StringSplit. Use Number().

Also couldn't you do the same validation with one regexp and one if ?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Wrong function for the wrong intended job ... IsNumber() check the variable base Type not its apparent contents.

To test if a string contains a number ... use StringIsAlNum() and its other related AutoIt functions.

Edit:Getting rid of double negative, before ...

Ah, very good. Thanks!

Now I can work on making the code better. (1 regex instead of 2, etc.. :mellow:)

I'll make sure to share it after i'm done. :]

tolle indicium

Link to comment
Share on other sites

Beware that $pf_octet = StringRegExp($pf_line, ".") doesn't give you the result you want: dot in a pattern matches almost anything!

:ahem: I was thinking of replacing the whole function by something more efficient!

Do you intent to match decimal IP and port only?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Beware that $pf_octet = StringRegExp($pf_line, ".") doesn't give you the result you want: dot in a pattern matches almost anything!

:ahem: I was thinking of replacing the whole function by something more efficient!

Do you intent to match decimal IP and port only?

Yes.

I don't really understand the stringregexp command at the moment, it's been a while since i've dove into autoit. (i'm tired, it doesn't help. :mellow: )

I'll check it out tomorrow. But, in the mean time i've found this:

$pf_line = "6.21.14.35"
$pf_check = StringRegExp($pf_line, "(((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9]?[0-9]))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9]?[0-9]))")

Now, I don't completely understand what this does, but what I do notice is that if you add any character at the last octet it will return as true reguardless of what it is. (e.g. "1.2.3.4x") So, it's not what i'm looking for in this particular piece of code.

Edited by Glyph

tolle indicium

Link to comment
Share on other sites

Try to add any check you like (e.g. obviously wrong ports, local host address, a.s.o.

Local $a[17] = [ _
    "10.89.185.2:1080", _
    "10.96.0.194:8888", _
    " 100.100   . 250.123   :   5555", _
    "140.red-80-34-73.pooles.rima-tde.net:80", _
    "100.526.210.23:23:23", _
    "108.9.10.226:8080", _
    "141-52.adsl.cust.tie.cl:80", _
    "141.24.249.129:3128", _
    "141.24.249.129:8000000000", _
    "141.24.249.130:3127.", _
    "141.24.249.*130:3128", _
    "141.24.33.161:3124", _
    "141.24:.33.161:3127", _
    "141.155.238.14380A1R1C1V1X0", _
    "141.24.0000000000033.162:000000000000003128", _
    "141.24.33.192:3124", _
    "141.16.94.2178080A1R1C1V0X0" _
]

For $i = 0 To UBound($a) - 1
    $s = StringStripWS($a[$i], 8)   ; remove all whitespaces, no merci!
    $res = StringRegExp($s, "\A(\d+)\.(\d+)\.(\d+)\.(\d+):(\d+)\z", 1)  ; match the structure "number dot number dot number dot number column number"
    If @error Then
        ConsoleWrite('"' & $s & '" is not in the required IP:port format.' & @LF)
        ContinueLoop
    EndIf
    If $res[0] < 0 Or $res[0] > 255 Or _        ; check allowable values
 $res[1] < 0 Or $res[1] > 255 Or _
 $res[2] < 0 Or $res[2] > 255 Or _
 $res[3] < 0 Or $res[3] > 255 Or _
 $res[4] < 1 Or $res[4] > 65535 Then
        ConsoleWrite($s & ' has invalid values in IP or port.' & @LF)
        ContinueLoop
    EndIf
    ; add extra checks here
    $a[$i] = Number($res[0]) & '.' & Number($res[1]) & '.' & Number($res[2]) & '.' & Number($res[3]) & ':' & Number($res[4])
    ConsoleWrite($a[$i] & ' is a valid entry.' & @LF)
Next

Don't try to check both the structure and the values within a single regexp. It would become unduly complex and unreadable. Regexp are best at matching / classifying sequences of chars but not as good at checking for the range those chars build. Checking the range of a number is better left for simple conditions in a subsequent if, once you know the parts are indeed numbers.

Notice that we use implicit String --> Number conversion in the tests. As the regexp has told us that each part was made of digits only, that's safe. We rewrite the valid entries in the array doing two conversions: Number then the implicit Number --> String in order to get rid of leading zeroes, if any. We do more conversions than strictly necessary, but I assumed you don't run his code on billion addresses every minute, so being marginally inefficient doesn't harm much.

I may still overlook something, but it shouldn't be hard to correct.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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