Expand use of SC gettokenindex

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Expand use of SC gettokenindex

Post 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 casegettokenindex("*.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?
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

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

Re: Expand use of SC gettokenindex

Post 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:

Code: Select all

(not) in
and/or
(not) contains
Each of them would operate on one variable and one list (of matches). Much more versatile
and not bound to only gettokenindex()...
One of my scripts helped you out? Please donate via Paypal

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

Re: Expand use of SC gettokenindex

Post 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).
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

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

Re: Expand use of SC gettokenindex

Post 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... }
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Expand use of SC gettokenindex

Post by bdeshi »

I think something like this might be a good fit: viewtopic.php?t=13822&p=143611#p143611
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

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

Re: Expand use of SC gettokenindex

Post 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).
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

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

Re: Expand use of SC gettokenindex

Post 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.
One of my scripts helped you out? Please donate via Paypal

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

Re: Expand use of SC gettokenindex

Post 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.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

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

Re: Expand use of SC gettokenindex

Post 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...
One of my scripts helped you out? Please donate via Paypal

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

Re: Expand use of SC gettokenindex

Post by klownboy »

Thanks highend, that works too. :tup: 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.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

Post Reply