Jump to content

IP:PORT _ArraySort


tempman
 Share

Recommended Posts

Hi, is it possible to sort IP address by PORT?

This is my buggy script:

#include <Array.au3>

#cs

No sorted
---------
175.164.4.196:80
143.59.44.41:443
62.68.190.92:8000
77.119.25.12:80
101.167.48.241:8080
80.243.169.25:8443

Sorted
------
77.119.25.12:80
175.164.4.196:80
143.59.44.41:443
62.68.190.92:8000
101.167.48.241:8080
80.243.169.25:8443

#ce


Local $aArray[6] = ["175.164.4.196:80", _
        "143.59.44.41:443", _
        "62.68.190.92:8000", _
        "77.119.25.12:80", _
        "101.167.48.241:8080", _
        "80.243.169.25:8443"]

_ArrayDisplay($aArray, "No sorted")
_Sort($aArray)
_ArrayDisplay($aArray, "Sorted")


Func _Sort(ByRef $aArray)
     _ArrayColInsert($aArray, 1)
    For $i = 0 To UBound($aArray) - 1
       $aArray[$i][1] = StringRegExpReplace($aArray[$i][0], '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,4}', "$4$3$2$1")
    Next
    _ArraySort($aArray, 0, 0, 0, 1)
    _ArrayColDelete($aArray, 1)
EndFunc   ;==>_Sort

 

Thanks!

Link to comment
Share on other sites

tempman,

This creates a 2d array whereby the first element is the sort argument and the second element is the complete ip address.  The sort element is expanded to 4 digits for the sort to work properly...

#include <array.au3>

Local $aArray[6] = ["175.164.4.196:80", _
        "143.59.44.41:443", _
        "62.68.190.92:8000", _
        "77.119.25.12:80", _
        "101.167.48.241:8080", _
        "80.243.169.25:8443"]

Local $array_that_can_be_sorted[UBound($aArray)][2]

For $i = 0 To UBound($aArray) - 1
    ;ConsoleWrite($i & @CRLF)
    $array_that_can_be_sorted[$i][0] = StringFormat('%04i', StringRegExpReplace($aArray[$i], '[^:]+:(.*)', '$1'))
    $array_that_can_be_sorted[$i][1] = $aArray[$i]
Next

_ArrayDisplay($array_that_can_be_sorted)
_ArraySort($array_that_can_be_sorted)
_ArrayDisplay($array_that_can_be_sorted)

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

But how to save only Col 1 data to txt file?

#include <Array.au3>
#include <File.au3>

Local $aArray[6] = ["175.164.4.196:80", _
        "143.59.44.41:443", _
        "62.68.190.92:8000", _
        "77.119.25.12:80", _
        "101.167.48.241:8080", _
        "80.243.169.25:8443"]

Local $array_that_can_be_sorted[UBound($aArray)][2]

For $i = 0 To UBound($aArray) - 1
    ;ConsoleWrite($i & @CRLF)
    $array_that_can_be_sorted[$i][0] = StringFormat('%04i', StringRegExpReplace($aArray[$i], '[^:]+:(.*)', '$1'))
    $array_that_can_be_sorted[$i][1] = $aArray[$i]
Next

_ArrayDisplay($array_that_can_be_sorted)
_ArraySort($array_that_can_be_sorted)
_ArrayDisplay($array_that_can_be_sorted)

_ArrayDisplay($array_that_can_be_sorted, "","|1:1")

Local $sFilePath = FileOpen(@ScriptDir & "\zzz.txt", 1)
_FileWriteFromArray($sFilePath, $array_that_can_be_sorted)

 

Link to comment
Share on other sites

tempman,

As jdelaney suggests...

#include <Array.au3>
#include <File.au3>

Local $aArray[6] = ["175.164.4.196:80", _
        "143.59.44.41:443", _
        "62.68.190.92:8000", _
        "77.119.25.12:80", _
        "101.167.48.241:8080", _
        "80.243.169.25:8443"]

Local $array_that_can_be_sorted[UBound($aArray)][2]

For $i = 0 To UBound($aArray) - 1
    ;ConsoleWrite($i & @CRLF)
    $array_that_can_be_sorted[$i][0] = StringFormat('%04i', StringRegExpReplace($aArray[$i], '[^:]+:(.*)', '$1'))
    $array_that_can_be_sorted[$i][1] = $aArray[$i]
Next

_ArrayDisplay($array_that_can_be_sorted)
_ArraySort($array_that_can_be_sorted)
_ArrayDisplay($array_that_can_be_sorted)

_ArrayDisplay($array_that_can_be_sorted, "", "|1:1")

Local $sFilePath = FileOpen(@ScriptDir & "\zzz.txt", 1)

; iterate thru the array outputing whatever column you want in whatever format you want

Local $hfl = FileOpen(@ScriptDir & '\zzz.txt', 2), $str = ''
For $i = 0 To UBound($array_that_can_be_sorted) - 1
    $str &= $array_that_can_be_sorted[$i][1] & ',' ; format col 1 of array for camma delimited write
Next
FileWrite($hfl, $str)
FileClose($hfl)

;_FileWriteFromArray($sFilePath, $array_that_can_be_sorted)

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Hi, and again, thank you all!
I was aware of this kind of solution, but I thought if there is this:

_ArrayDisplay($array_that_can_be_sorted, "", "|1:1")

Maybe there is something like this:

_FileWriteFromArray($sFilePath, $array_that_can_be_sorted, "|1:1")

Or I just became lazy? :D

Link to comment
Share on other sites

close,  and I believe @kylomas adds a bonus comma

#include <File.au3>

Local $aArray[6] = ["175.164.4.196:80", _
        "143.59.44.41:443", _
        "62.68.190.92:8000", _
        "77.119.25.12:80", _
        "101.167.48.241:8080", _
        "80.243.169.25:8443"]

Local $array_that_can_be_sorted[UBound($aArray)][2]

For $i = 0 To UBound($aArray) - 1
    ;ConsoleWrite($i & @CRLF)
    $array_that_can_be_sorted[$i][0] = StringFormat('%04i', StringRegExpReplace($aArray[$i], '[^:]+:(.*)', '$1'))
    $array_that_can_be_sorted[$i][1] = $aArray[$i]
Next

$column = 1

consolewrite(_ArrayToString($array_that_can_be_sorted , "," , -1 , -1 , "," , $column , $column))

 

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