Page 1 of 1
Expand use of SC gettokenindex
Posted: 29 Nov 2019 21:13
by klownboy
I was writing a script yesterday and encountered a situation where I wanted to determine if the selected files were all JPG files. If they were not I would provide an error message. I thought initially I could accomplish that strictly using SC gettokenindex, but I wasn't able to. These are 2 solutions I came up with, but I'm sure there are others:
Code: Select all
$images = "<selitems <crlf>>";
//one method to determine if all selected files were / were not JPG images
if(formatlist($images, "f", "<crlf>", "!*.jpg", "f") == "") {$AllFilesNotJPG = "0";};
else {$AllFilesNotJPG = "1";}
//another method
$FileCount = gettoken($images, "count", "<crlf>");
if(gettokenindex("*.jpg", $images, "<crlf>", 'iwc') != $FileCount) {$AllFilesNotJPG = "1";}
else {$AllFilesNotJPG = "0";}
I was wondering if it would make sense to have inverted or negative type switch for SC gettokenindex which you would be able to apply a inverted or not switch to determine if the token was possibly in the tokenlist, but not every entry in the tokenlist. So in my particular case
gettokenindex("*.jpg", $images, "<crlf>", '!iwc') where the file listing (tokenlist) contains a BMP file (or any other type file) other than JPG token are in the list, the return for SC gettokenindex would return a -1 or whatever to inform you that not not all items in the tokenlist are a token. Does that make sense?
Re: Expand use of SC gettokenindex
Posted: 29 Nov 2019 23:11
by highend
Just my 2 cents...
Be careful with formatlist(). It can choke on too much data to process.
regexmatches() would be an option as well. But what I'd suggest is a set of operators:
Each of them would operate on one variable and one list (of matches). Much more versatile
and not bound to only gettokenindex()...
Re: Expand use of SC gettokenindex
Posted: 30 Nov 2019 16:59
by klownboy
Thanks high-end. Now that you mention it, I do remember previous conversations about SC formatlist. I am currently using the other method though my file list would be relatively small anyway.
I'm not exactly sure how that would be implemented - adding a set of of operators that could be applied to other SCs as well (i.e., operating on a variable and one list of matches).
Re: Expand use of SC gettokenindex
Posted: 01 Dec 2019 09:15
by highend
To use these operators for your special case they'd need to be expanded by e.g.
"unique/short:U" or maybe isUnique
Code: Select all
$a = "two";
$b = "one|two|three";
if ($b contains $a) { ...this will be true... }
if ($b containsU $a { ...this will be false... }
Re: Expand use of SC gettokenindex
Posted: 01 Dec 2019 11:27
by bdeshi
I think something like this might be a good fit:
viewtopic.php?t=13822&p=143611#p143611
Re: Expand use of SC gettokenindex
Posted: 01 Dec 2019 16:15
by klownboy
highend wrote: ↑01 Dec 2019 09:15
$a = "two";
$b = "one|two|three";
if ($b contains $a) { ...this will be true... }
if ($b containsU $a { ...this will be false... }
Hey highend, so the first
if would be very similar to what is currently possible in the existing SC gettokenindex. The second
if statement would be what I would be after in my case where I'm determining if
all the selected files ("<selitems <crlf>>") are actually JPGs. The "U" could also stand for universally (or in every case), all selected file are of a certain type (like JPGs) and there are no other types like BMPs.
Hey Sammay, that's interesting. I've been trying to input my data into the function you referenced without success, which would be simply "<selitems <crlf>>" for the list of tokens along with file type I want to ensure all files in the list are (*.JPG). I'm most likely screwing up the syntax. I'm not sure how I should be inputting the 2nd parameter, 'exists("{@Item}")'. Though for my situation it appears to be much more work than the methods I'm currently using (in my first post).
Re: Expand use of SC gettokenindex
Posted: 01 Dec 2019 16:19
by highend
The second if statement would be what I would be after in my case where I'm determining if all the selected files ("<selitems <crlf>>") are actually JPGs
Not exactly. You would still need to gather all extensions to have the list. As I said, this is a more general approach which can be used for a broader range of scenarios.
Re: Expand use of SC gettokenindex
Posted: 01 Dec 2019 16:36
by klownboy
OK, it's intended to be general but I can see how it could be adapted. I'm looking for a "nothing but" application (i.e., the listing of different file types contains "nothing but" *.JPG files. I gave 2 methods in my first post and as you mentioned, you could use regex. You could loop through the listing, looking at file extensions and break out as soon as one is found that's not a JPG. That also works but seems to be more work (I did that originally). Thanks highend.
Re: Expand use of SC gettokenindex
Posted: 01 Dec 2019 17:16
by highend
ATM I'd use:
Code: Select all
$files = <selitems <crlf>>;
$exts = regexmatches($files, "\.[^.]+?(?=\r?\n|$)", <crlf>);
if (formatlist($exts, "sed", <crlf>) UnLikeI ".jpg") { text "NOT only .jpg files are selected!"; }
In this case formatlist() doesn't have to process so much data, so even 100k+ files could (probably) be selected...
Re: Expand use of SC gettokenindex
Posted: 01 Dec 2019 18:43
by klownboy
Thanks highend, that works too.

I think if I put in a formal wish I'd keep it more specific to SC gettokenindex since what I'm looking for (i.e., determining if the tokenlist contains tokens different than the token specified) is most in line with the purpose of SC gettokenindex finding a tokens in a tokenlist. More importantly, anything beyond that would be significantly more work for Don.