Page 1 of 1

SC formatlist: sorting multi-dot numbers

Posted: 15 Nov 2016 21:29
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:

Re: SC formatlist: sorting multi-dot numbers

Posted: 15 Nov 2016 21:56
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.*");