zsh-users
 help / color / mirror / code / Atom feed
* repeat count?
@ 2023-01-16 18:05 Dominik Vogt
  2023-01-16 18:26 ` zeurkous
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Dominik Vogt @ 2023-01-16 18:05 UTC (permalink / raw)
  To: Zsh Users

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?

  repeat 10; do
    echo "pass number ???"
  done

Ciao

Dominik ^_^  ^_^

--

Dominik Vogt


^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: repeat count?
  2023-01-16 18:05 repeat count? Dominik Vogt
@ 2023-01-16 18:26 ` zeurkous
  2023-01-16 18:49   ` Roman Perepelitsa
  2023-01-16 19:04 ` Stephane Chazelas
  2023-01-16 19:20 ` Roman Perepelitsa
  2 siblings, 1 reply; 12+ messages in thread
From: zeurkous @ 2023-01-16 18:26 UTC (permalink / raw)
  To: dominik.vogt, Zsh Users

On Mon, 16 Jan 2023 19:05:21 +0100, 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?
>
>   repeat 10; do
>     echo "pass number ???"
>   done
>

Dunno, but one alternative is--

 for a in {0..9}; do
  echo "pass number ${a}";
 done;

> Ciao
>
> Dominik ^_^  ^_^

HTH,
         --zeurkous.

>
> =2D-
>
> Dominik Vogt
>

-- 
Friggin' Machines!


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: repeat count?
  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
  0 siblings, 2 replies; 12+ messages in thread
From: Roman Perepelitsa @ 2023-01-16 18:49 UTC (permalink / raw)
  To: zeurkous; +Cc: dominik.vogt, Zsh Users

On Mon, Jan 16, 2023 at 7:31 PM <zeurkous@blaatscaahp.org> wrote:
>
> On Mon, 16 Jan 2023 19:05:21 +0100, 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?
> >
> >   repeat 10; do
> >     echo "pass number ???"
> >   done
> >
>
> Dunno, but one alternative is--
>
>  for a in {0..9}; do
>   echo "pass number ${a}";
>  done;

Be aware that this is a lot slower if you are iterating many times and
care about the performance of the loop.

Roman.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: repeat count?
  2023-01-16 18:05 repeat count? Dominik Vogt
  2023-01-16 18:26 ` 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
  2 siblings, 2 replies; 12+ messages in thread
From: Stephane Chazelas @ 2023-01-16 19:04 UTC (permalink / raw)
  To: Zsh Users

2023-01-16 19:05:21 +0100, Dominik Vogt:
> 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?
> 
>   repeat 10; do
>     echo "pass number ???"
>   done
[...]

You can always do:

n=0; repeat 10 echo pass number $((++n))

Or a la ksh93

for ((n=1;n<=10;n++)) echo pass number $((++n))

-- 
Stephane


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: repeat count?
  2023-01-16 19:04 ` Stephane Chazelas
@ 2023-01-16 19:05   ` Stephane Chazelas
  2023-01-19 14:52   ` zeurkous
  1 sibling, 0 replies; 12+ messages in thread
From: Stephane Chazelas @ 2023-01-16 19:05 UTC (permalink / raw)
  To: Zsh Users

2023-01-16 19:04:07 +0000, Stephane Chazelas:
[...]
> Or a la ksh93
> 
> for ((n=1;n<=10;n++)) echo pass number $((++n))

Sorry, should have been:

for ((n=1;n<=10;n++)) echo pass number $n


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: repeat count?
  2023-01-16 18:49   ` Roman Perepelitsa
@ 2023-01-16 19:17     ` Marc Chantreux
  2023-01-19 14:50     ` zeurkous
  1 sibling, 0 replies; 12+ messages in thread
From: Marc Chantreux @ 2023-01-16 19:17 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: zeurkous, dominik.vogt, Zsh Users

On Mon, Jan 16, 2023 at 07:49:39PM +0100, Roman Perepelitsa wrote:
> >  for a in {0..9}; do
> >   echo "pass number ${a}";
> >  done;

print -f 'pass number %d\n' {0..9}

# is there a way to online this ?
set -- {0..9}
print -l 'pass number '$^@

n=0; repeat 10 echo pass number $[++n]

> Be aware that this is a lot slower if you are iterating many times and
> care about the performance of the loop.

my prefered would be

seq -f 'pass number %g' 10

regards,
marc


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: repeat count?
  2023-01-16 18:05 repeat count? Dominik Vogt
  2023-01-16 18:26 ` zeurkous
  2023-01-16 19:04 ` Stephane Chazelas
@ 2023-01-16 19:20 ` Roman Perepelitsa
  2023-01-19 14:57   ` zeurkous
  2 siblings, 1 reply; 12+ messages in thread
From: Roman Perepelitsa @ 2023-01-16 19:20 UTC (permalink / raw)
  To: dominik.vogt, Zsh Users

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.

Roman.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: Re: repeat count?
  2023-01-16 18:49   ` Roman Perepelitsa
  2023-01-16 19:17     ` Marc Chantreux
@ 2023-01-19 14:50     ` zeurkous
  1 sibling, 0 replies; 12+ messages in thread
From: zeurkous @ 2023-01-19 14:50 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: dominik.vogt, Zsh Users

On Mon, 16 Jan 2023 19:49:39 +0100, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> On Mon, Jan 16, 2023 at 7:31 PM <zeurkous@blaatscaahp.org> wrote:
>>
>> On Mon, 16 Jan 2023 19:05:21 +0100, 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?
>> >
>> >   repeat 10; do
>> >     echo "pass number ???"
>> >   done
>> >
>>
>> Dunno, but one alternative is--
>>
>>  for a in {0..9}; do
>>   echo "pass number ${a}";
>>  done;
>
> Be aware that this is a lot slower if you are iterating many times and
> care about the performance of the loop.

Yes, due to the text processing involved. But in a crunch, it can do.
         --zeurkous.

> Roman.

-- 
Friggin' Machines!


^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: Re: repeat count?
  2023-01-16 19:04 ` Stephane Chazelas
  2023-01-16 19:05   ` Stephane Chazelas
@ 2023-01-19 14:52   ` zeurkous
  1 sibling, 0 replies; 12+ messages in thread
From: zeurkous @ 2023-01-19 14:52 UTC (permalink / raw)
  To: Stephane Chazelas, zsh-users

On Mon, 16 Jan 2023 19:04:07 +0000, Stephane Chazelas <stephane@chazelas.org> wrote:
> 2023-01-16 19:05:21 +0100, Dominik Vogt:
>> 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?
>> 
>>   repeat 10; do
>>     echo "pass number ???"
>>   done
> [...]
>
> You can always do:
>
> n=0; repeat 10 echo pass number $((++n))
>
> Or a la ksh93
>
> for ((n=1;n<=10;n++)) echo pass number $((++n))

Me didn't realize that {--,++} were supported. That appears to be
useful -- thanks.

         --zeurkous.

-- 
Friggin' Machines!


^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: Re: repeat count?
  2023-01-16 19:20 ` Roman Perepelitsa
@ 2023-01-19 14:57   ` zeurkous
  2023-01-19 16:37     ` Daniel Shahaf
  0 siblings, 1 reply; 12+ messages in thread
From: zeurkous @ 2023-01-19 14:57 UTC (permalink / raw)
  To: Roman Perepelitsa, dominik.vogt, Zsh Users

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...?
        --zeurkous.

> Roman.

-- 
Friggin' Machines!


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: repeat count?
  2023-01-19 14:57   ` zeurkous
@ 2023-01-19 16:37     ` Daniel Shahaf
  2023-01-19 17:49       ` Ray Andrews
  0 siblings, 1 reply; 12+ messages in thread
From: Daniel Shahaf @ 2023-01-19 16:37 UTC (permalink / raw)
  To: zsh-users

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.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: repeat count?
  2023-01-19 16:37     ` Daniel Shahaf
@ 2023-01-19 17:49       ` Ray Andrews
  0 siblings, 0 replies; 12+ messages in thread
From: Ray Andrews @ 2023-01-19 17:49 UTC (permalink / raw)
  To: zsh-users


On 2023-01-19 08:37, Daniel Shahaf wrote:

> - Or perhaps the arithmetic for() is good enough for now and we don't
>    need this syntactic sugar at all.
>
Just a comment from the peanut gallery: I was thinking about that issue 
and the question arose in my head whether or not something that is so 
easy to do manually should be hard coded even in the best of all 
possible worlds.  Bigger code, new hardcoded variable, minute but still 
possible bug of some sort, an extra microsecond to compile and run, 
slightly bigger doc needed.  Small issues, but small issues add up over 
time, and since there is no *real* problem, why bother?  Contrast ... 
dunno ... say the '-U' option with arrays.  When I first discovered that 
I laughed like a loonie, it was so damned useful -- worth whatever 
weight it added to the shell.  Featuritis can add entropy rather than 
reduce it. Little things can clutter more than they help.

My 2 cents.




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-01-19 17:50 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-16 18:05 repeat count? 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
2023-01-19 17:49       ` Ray Andrews

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).