Page 1 of 2

ForEach Conditions

Posted: 13 Aug 2014 16:09
by Stef123
My Intention: Running a ForEach structure on selected files, provided there is indeed something selected.
HelpFile example under ForEach Loops wrote: // shows "No files selected!" if no files are selected, and skips the loop
foreach($item, <get selecteditemspathnames>, <crlf>, , "No files selected!") {
echo $item;
}
Cannot get this to work. Or maybe I am not getting it? I expected to see "No files selected" in some kind of dialog that allows me to exit the script gracefully. Instead I get to see "No files selected" in the XY script environment - that dialog stuff that belongs under the hood and not to the interface. :?: :?:

Also, could someone please me string me along how to skip (within that loop) file types that are not jpg or jpeg or gif or png?
I tried tons of variations that look more or less like this:

Code: Select all

if (get($item, "ext") LikeI ("jpg" or "gif") {......}
The above in various combinations with and w/o quotes and parentheses and nested get-desperations... :(

Re: ForEach Conditions

Posted: 13 Aug 2014 16:21
by highend
Where did you get the syntax "get($item, "ext")" from?
Afaik, this doesn't even exist :)

1. You still have to use a different check like:

Code: Select all

if (get("SelectedItemsPathNames", "<crlf>") == "") { status self("base") . ": No item(s) selected...", "FF0000", "stop"; end(true); }
2. There are many ways to do a check for an extension and skip the loop
One variant:

Code: Select all

    foreach($item, "<get selecteditemspathnames>", "<crlf>") {
        if (regexmatches(getpathcomponent($item, "ext"), "jpe?g|gif|png")) { continue; }
        echo $item;
    }
regexmatches is not the fastest way but it's easy to write, understand and maintain (at least if there is a basic knowledge of regexes).

Regarding the "jpe?g":
The ? makes the preceding char optional so it'll match jpeg AND jpg.

Re: ForEach Conditions

Posted: 13 Aug 2014 16:46
by Stef123
Thank you, highend
#2 - the regex makes sense to me, except for the "continue"-statement. Does that mean the ForEach loop will not continue after "continue" if the if-condition is not met? Or did you just use "continue" as a placeholder for my code, not referring to the "continue" SC command?

#1 - way over my head, need time to digest and look up all these things in the help file. The help file example quoted above seems to indicate a built-in smartness that takes care of "No files selected!" Had hoped for an easy-to-spot typo, a missing comma... :whistle:

Thanks for now. Guess I'll have to do much more reading and analyzing existing scripts before I tackle these advanced topics.

Re: ForEach Conditions

Posted: 13 Aug 2014 17:00
by highend
Does that mean the ForEach loop will not continue after "continue" if the if-condition is not met?
If the extension of the current file in the loop matches one of the entries (jpe?g|png|etc.) it will skip the rest of the loop (by using the continue statement). If it doesn't match, everything else after it will be executed (the echo... line).
Is that more clear?
way over my head
It looks more difficult than it is.

Code: Select all

if (get("SelectedItemsPathNames", "<crlf>") == "") { echo "no files selected..."; end(true); }
Is a bit easier (to read)...

The "MessageOnEmpty" is more a debugging option, I've never had any use for it...

Re: ForEach Conditions

Posted: 13 Aug 2014 17:10
by highend
Btw, a faster comparison would be:

Code: Select all

    foreach($item, "<get selecteditemspathnames>", "<crlf>") {
        $ext = getpathcomponent($item, "ext");
        if ("|jpg|jpeg|gif|mp4|" LikeI "*|$ext|*") { continue; }
        echo $item;
    }
On 300 items it takes 78 msecs.

The regexmatches method needs 140 on the same items.

Re: ForEach Conditions

Posted: 13 Aug 2014 17:24
by TheQwerty
I tend to prefer filtering the list first if possible, so an alternative:

Code: Select all

 $items = FormatList(Get('SelectedItemsPathNames', '|'), 'fdents', '|', '*.jpg|*.jpeg|*.gif');
 End $items == '', 'No images selected.';
 foreach ($item, $items, '|') {
   echo $item;
 }

Re: ForEach Conditions

Posted: 13 Aug 2014 20:11
by Stef123
Phew - my initial scripting enthusiasm is fading away considerably. Quite a gap between reading the manual examples and getting it to work in real scenarios.
highend wrote:If the extension of the current file in the loop matches one of the entries (jpe?g|png|etc.) it will skip the rest of the loop (by using the continue statement). If it doesn't match, everything else after it will be executed (the echo... line).
Is that more clear?
Much better. What got me confused here: I need it exactly the other way round, I do want those jpg and png files processed by the loop, not skipped. So I guess I'd have to negate the if-condition.

[quote="highend"
It looks more difficult than it is.

Code: Select all

if (get("SelectedItemsPathNames", "<crlf>") == "") { echo "no files selected..."; end(true); }
Is a bit easier (to read)...[/quote]
Again, much better. Still struggling with "end(true)" - it ends the script, that much seems clear, but I cannot decipher it. Been trying to match it to some help file example, but you don't seem to be using an optional parameter, but rather evaluating a result (true) itself *headscratch" :shock: :eh:

@TheQwerty,
thanks, took me a while to figure it out (halfway, enough to get the general drift of what's going on) - very creative and smart approach. Formatlist - would have never thought it possible to pull it off, that kind of thing.
Will use your solution and (hopefully) manage to insert the run-calls myself at the right places.

Thanks to both of you.
Stef

Re: ForEach Conditions

Posted: 13 Aug 2014 20:39
by bdeshi
Stef123 wrote:Still struggling with "end(true)" - it ends the script, that much seems clear, but I cannot decipher it. Been trying to match it to some help file example, but you don't seem to be using an optional parameter, but rather evaluating a result (true) itself
Because apparently end can also be used as a function, so both of theae are the same:

Code: Select all

 end true, "ending";
 //^ end as a command: end param1, param2 [, param3]
 end(true);
 //^ end as a function: end(param1 [,param2, param3])
 // [] parts were skipped

Re: ForEach Conditions

Posted: 13 Aug 2014 20:47
by SkyFrontier
Hi, Stef.

As someone who had several issues concerning both understanding scripting and finding support back then, I'd like to encourage you to persist.
Sometimes it's hard, I know, but you'll find the earnings in so numerous ways that you'll be glad for the benefits.
As you can see, you had immediate answers from two of the big guys - with more or less difficulty, help always come.
Unfortunately I have no time anymore to closely follow the forums as I did but try to pm me when you have a persistent unanswered question, I'll be glad to help if I can. I'm sure other experienced users will follow.

Documentation *cannot* cover all possible conbinations or may even be just outdated, misleading or anything - it HELPS pointing those issues here, perhaps even opening a thread to report them.

Code: Select all

   foreach(problem, scripting, "your mind") {
                                           report to forums;
                                           $test = solved?;
                                           if ($test == true) { nice!, another experience point earned; keep going; }
                                           elseif ($test == false) { ask again; }
                                           else { pm SkyFrontier - or other more talented user; }
                                            }
   echo "satisfaction!";
My script has a little race condition, lacks a good logic and requires debuging. No time as of now. But you've got the idea!

Re: ForEach Conditions

Posted: 13 Aug 2014 22:41
by PeterH
SammaySarkar wrote:
Stef123 wrote:Still struggling with "end(true)" - it ends the script, that much seems clear, but I cannot decipher it. Been trying to match it to some help file example, but you don't seem to be using an optional parameter, but rather evaluating a result (true) itself
Because apparently end can also be used as a function, so both of theae are the same:

Code: Select all

 end true, "ending";
 //^ end as a command: end param1, param2 [, param3]
 end(true);
 //^ end as a function: end(param1 [,param2, param3])
 // [] parts were skipped
Oh - I'm rather sceptical :roll:

Did you find the function end() documented somewhere? If not you may have a problem in some future release of XY!

Remember that some time ago for duplicates of commands and function, I think substr was one of them, all command types were restricted to be called as function - i.e. substr $s, "String", ...; had to be changed to $s=substr("String", ...);
All scripts using the command versions had to be changed to continue working. I wouldn't take the risk for something like that to happen here :whistle:

Re: ForEach Conditions

Posted: 13 Aug 2014 22:52
by SkyFrontier
Take PeterH's word of caution seriously, guys.
I'm still having problems because of that change and this won't stop anytime soon.
:(

Re: ForEach Conditions

Posted: 14 Aug 2014 00:32
by highend
If not you may have a problem in some future release of XY!
If that ever happens I fire up my beloved PowerGrep and 10 seconds later everything's fixed :)

Re: ForEach Conditions

Posted: 14 Aug 2014 01:00
by PeterH
highend wrote:
If not you may have a problem in some future release of XY!
If that ever happens I fire up my beloved PowerGrep and 10 seconds later everything's fixed :)
But you should do this also for all those people having copied code from your examples. :shock: Will you? :mrgreen:

OK: I would understand it, if it would give any profit. But it only puzzles people, and brings risks in sense of compatibility. So, for my eyes, it's not good :roll:

Re: ForEach Conditions

Posted: 14 Aug 2014 03:34
by highend
But you should do this also for all those people having copied code from your examples
Should I? If they ask (nicely) why something isn't working as expected, I'll fix it.
But it only puzzles people
This is the first thread where someone was puzzled (at least as far as I'm aware of) about my bad coding habbits :)

I need access to the forum's database and I'll fix it for all the code I've posted asap. Don? *eg*

Re: ForEach Conditions

Posted: 14 Aug 2014 07:34
by Stef123
@ SkyFrontier - thanks for the encouragement.

@Sammay - I like the pragmatic undertone of your explanation. Maybe I fret too much about the logic involved, some kind of mental gymnastics that require a thinking muscle I have not developed (yet).

Let me ask you guys a more generic question: Does it take a certain mindset that I need to foster, to actually understand the grammar behind all this?
Or does it suffice to collect a repertoire of "stock phrases" that I apply in appropriate circumstances, not worrying whether vocabulary like "end(true)" is termed command or function?

In other words, is it more like calculus or more like learning French? Will I be crunching numbers that are always different or can I become fluent simply by parroting idioms?