From: "Daniel Shahaf" <d.s@daniel.shahaf.name>
To: zsh-users@zsh.org
Subject: Re: repeat count?
Date: Thu, 19 Jan 2023 16:37:08 +0000 [thread overview]
Message-ID: <cb0de352-3ce7-403c-8318-cb8ea1c83842@app.fastmail.com> (raw)
In-Reply-To: <thinkcrap!zeurkous!1674140226.24007@uucp>
zeurkous@blaatscaahp.org wrote on Thu, 19 Jan 2023 14:57 +00:00:
> On Mon, 16 Jan 2023 20:20:13 +0100, Roman Perepelitsa
> <roman.perepelitsa@gmail.com> wrote:
>> On Mon, Jan 16, 2023 at 7:06 PM Dominik Vogt <dominik.vogt@gmx.de> 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.
next prev parent reply other threads:[~2023-01-19 17:03 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-16 18:05 Dominik Vogt
2023-01-16 18:26 ` zeurkous
2023-01-16 18:49 ` Roman Perepelitsa
2023-01-16 19:17 ` Marc Chantreux
2023-01-19 14:50 ` zeurkous
2023-01-16 19:04 ` Stephane Chazelas
2023-01-16 19:05 ` Stephane Chazelas
2023-01-19 14:52 ` zeurkous
2023-01-16 19:20 ` Roman Perepelitsa
2023-01-19 14:57 ` zeurkous
2023-01-19 16:37 ` Daniel Shahaf [this message]
2023-01-19 17:49 ` Ray Andrews
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cb0de352-3ce7-403c-8318-cb8ea1c83842@app.fastmail.com \
--to=d.s@daniel.shahaf.name \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).