Decrementing in a while loop

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Decrementing in a while loop

Post by klownboy »

I have a script situation where it make more sense to work backward from a higher number to a lower number in a SC while loop since the tab number I'm looking for will probably always be a the high end of the list. The script works fine incrementing from the low end (ie., tab #1 'til it found it's tab name match which was usually tab #18 - the last tab added). The script section looked like this...incrementing...

Code: Select all

   $t = tab("get", "count");
   $tabToCloseCaption = "$drivename";
    while ($i++ <= $t) {
        $tabCaption = tab("get", "caption", $i);
        if ($tabCaption == $tabToCloseCaption) {
            tab("close", 0, $i); end(1==1);
        }
    }
To work from the last tab downward, I know I can do this here and it also works...

Code: Select all

   $i = tab("get", "count");
   $tabToCloseCaption = "$drivename";
    while ($i > 0) {
        $tabCaption = tab("get", "caption", $i);
        if ($tabCaption == $tabToCloseCaption) {
            tab("close", 0, $i); end(1==1);
        }
	  $i--;
    }
But is there any way to use "$i--" or similar directly in the while loop as in the first example without using the separate line at the bottom of the while loop to decrement? In other words, when incrementing in the first example, it starts with value "1" and works up the the last tab. When decrementing using "$i--", it will immediately subtract 1 so the first tab number is 1 minus the total of 18 which is 17. I read the help but I'm not sure if there's a way around not having the separate line to do the decrementing for this situation. The help under increment states, "Note that before v14.30.0100 the value was incremented/decremented before it was passed to the function!" But, isn't that what is happening here, at least for decrementing?
Thanks,
Ken

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Decrementing in a while loop

Post by PeterH »

Maybe increment $i by one before looping, then $i-- in header of the loop?

By the way: in first code you increment $i before having it initialized. You must hope it defaults to "" or 0. I would be afraid...

And: isn't end(1==1); the same as end(1); ?

klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Re: Decrementing in a while loop

Post by klownboy »

PeterH wrote:Maybe increment $i by one before looping, then $i-- in header of the loop
Duh yes :oops: , that was simple enough. Just + 1 to the total before the while loop though I was initially thinking there might be some other work around.
PeterH wrote:By the way: in first code you increment $i before having it initialized. You must hope it defaults to "" or 0. I would be afraid...
It must be defaulting to "0" since it does properly cycle through all the tabs, but I see what you mean as far as initializing or assigning "$i" to "0" beforehand.
PeterH wrote:isn't end(1==1); the same as end(1);
Sure is. It's from some older code, but still true, no pun intended (i.e., end true;) :)
Thanks PeterH

Post Reply