Modify SC substr to obtain substrings divided by a separator

Features wanted...
Post Reply
klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Modify SC substr to obtain substrings divided by a separator

Post by klownboy »

Would it make sense to have a XYplorer command which would pull a range of items from a string. For example, let's say we have a file listing, a variable called $recents from get("list_recentlyopenedfiles") and we would like only the first 15 files in the list. I know there are current ways to do that using a while loop with a count or foreach loop, but it would seem to be much more direct if we could accomplish that using a scripting command.

What I was thinking is a change to SC substr which would recognize if/when a separator parameter is provided.

- existing syntax for SC: substr(string, [start], [length])
- proposed syntax SC: substr(string, [start], [length], [separator=CRLF]) such that if the separator is provided, XY knows that the resultant substring is broken down by the separator not by the characters as is the current default (and would still remain the default). Most importantly this separator parameter would not affect existing use of substr w/o sep.

So using the above example, where $recents = get("list_recentlyopenedfiles");
$recent15 = substr($recent, 0, 15, <crlf>); would provide a listing of the first 15 files listed via get("list_recentlyopenedfiles").

Another possibility would be a new "format" along with a parameter for SC formatlist such that if a format of "m" is given a paramenter of 2-8 would list items 2-8 after any other formatting. Though I think SC substr would be better suited. Thanks.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

highend
Posts: 13277
Joined: 06 Feb 2011 00:33

Re: Modify SC substr to obtain substrings divided by a separator

Post by highend »

No while or foreach loop necessary...

You're missing the flags field of gettoken()

$recent15 = gettoken($recent, 15, <crlf>, , 1);

or if you really need a range (with a higher value than 1 for $start)

Code: Select all

function GetRange($string, $start, $end, $sep=<crlf>) {
    if ($start > 1) {
        $string = gettoken($string, $start, $sep, , 2);
        return gettoken($string, $end - $start + 1, $sep, , 1);
    }
    return gettoken($string, $end, $sep, , 1);
}
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Re: Modify SC substr to obtain substrings divided by a sep [resolved - not needed]

Post by klownboy »

Oh darn (in a good way), I had looked SC gettoken and have probably used it to do exactly what I asked in the past. So double darn. Thanks highend for reminding me or waking me up. You can either delete this or I'll just indicate resolved.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Re: Modify SC substr to obtain substrings divided by a separator

Post by klownboy »

Thanks highend for the function. My current use is with a simple start of "1", but it may come in handy for future scripts. :tup:
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

Post Reply