Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: repeat count?



zeurkous@xxxxxxxxxxxxxxx wrote on Thu, 19 Jan 2023 14:57 +00:00:
> On Mon, 16 Jan 2023 20:20:13 +0100, Roman Perepelitsa 
> <roman.perepelitsa@xxxxxxxxx> wrote:
>> On Mon, Jan 16, 2023 at 7:06 PM Dominik Vogt <dominik.vogt@xxxxxx> wrote:
>>>
>>> Is it possible to get the pass number of a repeat loop from within
>>> the loop, or is it necessary to use a hand written counter?
>>
>> I just realized that none of the replies actually answered your
>> question. There is no special parameter set by the shell that contains
>> the iteration counter in repeat loops. I'd love to have that myself.
>
> Then the question is: how would that work w/ nested repeat loops...?

Let's see.  Draft requirements:

- Count from 1 upwards, incremented by 1 each iteration.

- Be read-only.¹

- Remain set after the loop ends, to facilitate testing it to determine
  which iteration, if any, ran «break».

- Be possible to have a «repeat» loop call a function and afterwards
  access its own iteration count, even if the function executes its own
  «repeat» loops.

So:

- The variable could have a well-known name, and loops that call
  functions before referencing the variable will need to save its value
  locally.

- The variable's name could be specified by the user, as in «repeat -v i 42»
  and then store the iteration number in $i.  This would be backwards
  incompatible in a corner case (the first word after «repeat» is a math
  expression, so syntaxes of the form «repeat -foo» or «repeat --bar»
  are valid today if SHORT_REPEAT hasn't been turned off).

- Or perhaps the arithmetic for() is good enough for now and we don't
  need this syntactic sugar at all.

Cheers,

Daniel

¹ Mainly because I don't want to think about «repeat -v i 10 ((++i))»
  right now.  If someone wants to spec a writable repeat count, go
  ahead.




Messages sorted by: Reverse Date, Date, Thread, Author