SC formatlist: sorting multi-dot numbers

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Filehero
Posts: 2734
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

SC formatlist: sorting multi-dot numbers

Post by Filehero »

FYI

I share this "Did you know" only because it took me a couple of time to find the obvious (*) format configuration).
To me formatlist is one of the most helpful SCs, actually part of the provided functions is a powerful sort.

Anyway, here's a simple example for easy sorting of multi-dot numbers like IPs.

Code: Select all

  $in = <<<MDOTNUM
192.168.2.103
255.255.255.0
193.168.2.103
192.2.2.103
192.169.2.103
192.168.3.103
192.168.3.3
192.168.2.104
MDOTNUM;
//<<<
  
  text(formatlist($in, "sn", <crlf>));
Also handy when dealing with folders/files representing software build versions (patch management).

:appl:


FH

*: for the sometimes blinds like me :lol:

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: SC formatlist: sorting multi-dot numbers

Post by highend »

I'm also using it to keep / remove unwanted entries in a (normally <crlf>-sorted) list.
Much faster than any foreach / while loop and it has no problems with unicode chars.

There is just one thing to be aware of: Two of it's wildcards need to be escaped when
the data to be sorted contains them (the rest don't exist in path / file names)

Code: Select all

    // Escape wildcards ("#" and "[") for formatlist()
    $pattern = regexreplace(<things we need to filter>, "(#|\[)", "[$1]");
    text formatlist(<source list>, "ef", <crlf>, "!$pattern");
Btw, "text ..." is a command not a function call:)

A filter scenario:
You want to see all ip addresses that aren't in the 192er subnet:

Code: Select all

    $in = <<<MDOTNUM
192.168.2.103
255.255.255.0
193.168.2.103
192.2.2.103
192.169.2.103
192.168.3.103
192.168.3.3
192.168.2.104
MDOTNUM;

    text formatlist($in, "snf", <crlf>, "!192.*");
One of my scripts helped you out? Please donate via Paypal

Post Reply