zsh-users
 help / color / mirror / code / Atom feed
* counting in "for" loops
@ 1998-12-02 16:00 Sven Guckes
  1998-12-02 16:16 ` Thomas Koehler
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Sven Guckes @ 1998-12-02 16:00 UTC (permalink / raw)
  To: ZShell Users List

Again, I look at the manual and  I cannot find
how to do a simple count within a for loop.

	$ for count in ???; do
	> echo -n $count
	> done
	1 2 3 4 5 6 7 8 9 10

What do I need for "???"?
Why isn't this in the manual as an example?  *sigh*

Sven


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

* Re: counting in "for" loops
  1998-12-02 16:00 counting in "for" loops Sven Guckes
@ 1998-12-02 16:16 ` Thomas Koehler
  1998-12-02 16:21   ` Peter Stephenson
  1998-12-02 16:50 ` counting in "for" loops Bruce Stephens
  1998-12-03  4:22 ` Geoff Wing
  2 siblings, 1 reply; 9+ messages in thread
From: Thomas Koehler @ 1998-12-02 16:16 UTC (permalink / raw)
  To: ZShell Users List

On Wed, Dec 02, 1998 at 05:00:32PM +0100, Sven Guckes wrote:
> Again, I look at the manual and  I cannot find
> how to do a simple count within a for loop.
> 
> 	$ for count in ???; do
> 	> echo -n $count
> 	> done
> 	1 2 3 4 5 6 7 8 9 10
> 
> What do I need for "???"?
> Why isn't this in the manual as an example?  *sigh*

for i in {1..10} ; do echo -n "$i " ; done

> Sven

HTH,
Thomas

P.S.: Documentation could be better organized ;-)

-- 
    Thomas Köhler    Email:     jean-luc@picard.franken.de
        <><           WWW:    http://home.pages.de/~jeanluc/
                      IRC:               jeanluc
      LCARS --- Linux for Computers on All Real Starships


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

* Re: counting in "for" loops
  1998-12-02 16:16 ` Thomas Koehler
@ 1998-12-02 16:21   ` Peter Stephenson
  1998-12-03  3:15     ` Updating documentation (Re: counting in "for" loops ) Timothy J Luoma
  0 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 1998-12-02 16:21 UTC (permalink / raw)
  To: ZShell Users List

Thomas Koehler wrote:
> P.S.: Documentation could be better organized ;-)

No argument, but the best person to do it is someone who can see
what's wrong with it.  Those of us zsh-workers who spend more time
looking at the source than the documentation are not necessarily in
that position.  Anyone is very welcome to make suggestions as to what
to do.  Even if there are people who would like to do it but don't
feel their English is up to it (and I know the feeling :-/), any
detailed enough suggestions might eventually get acted upon (and
that's a promise :-)).

-- 
Peter Stephenson <pws@ibmth.df.unipi.it>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy


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

* Re: counting in "for" loops
  1998-12-02 16:00 counting in "for" loops Sven Guckes
  1998-12-02 16:16 ` Thomas Koehler
@ 1998-12-02 16:50 ` Bruce Stephens
  1998-12-03  4:22 ` Geoff Wing
  2 siblings, 0 replies; 9+ messages in thread
From: Bruce Stephens @ 1998-12-02 16:50 UTC (permalink / raw)
  To: Sven Guckes; +Cc: ZShell Users List

Sven Guckes <guckes@math.fu-berlin.de> writes:

> Again, I look at the manual and  I cannot find
> how to do a simple count within a for loop.
> 
> 	$ for count in ???; do
> 	> echo -n $count
> 	> done
> 	1 2 3 4 5 6 7 8 9 10

This works for me (with zsh-3.1.4):

for ((count=1; count<=10; count++)); do
echo $count
done

> Why isn't this in the manual as an example?  *sigh*

The manuals don't have many examples, currently.  By all means
contribute things that you'd like to see in future versions, and this
example seems useful.  Although something like:

for ((count=0; count<10; count++))

would be a more common sort of requirement.

Alternatively {1..10} will expand to a list containing the numbers, so

for count in {1..10}; do
...

might be sufficient for what you want.  Doing the same for
{1..1000000} and things will take more memory than is optimal, though.


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

* Updating documentation (Re: counting in "for" loops )
  1998-12-02 16:21   ` Peter Stephenson
@ 1998-12-03  3:15     ` Timothy J Luoma
  0 siblings, 0 replies; 9+ messages in thread
From: Timothy J Luoma @ 1998-12-03  3:15 UTC (permalink / raw)
  To: ZShell Users List

	Author:	Peter Stephenson <pws@ibmth.df.unipi.it>
	Date:	Wed, 02 Dec 1998 17:21:28 +0100
	ID:	<9812021621.AA54418@ibmth.df.unipi.it>

> Even if there are people who would like to do it but don't
> feel their English is up to it (and I know the feeling :-/)

FWIW

I don't feel like I know ZSH well enough to help document it, but I would be  
very happy to help those who would be willing but whose English they feel is  
not adequate.

After taking Hebrew, I have sympathy for anyone trying to learn a different  
language, especially something as nonsensical as English.

TjL



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

* Re: counting in "for" loops
  1998-12-02 16:00 counting in "for" loops Sven Guckes
  1998-12-02 16:16 ` Thomas Koehler
  1998-12-02 16:50 ` counting in "for" loops Bruce Stephens
@ 1998-12-03  4:22 ` Geoff Wing
  1998-12-03 21:26   ` Phil Pennock
  2 siblings, 1 reply; 9+ messages in thread
From: Geoff Wing @ 1998-12-03  4:22 UTC (permalink / raw)
  To: zsh-users

Sven Guckes <guckes@math.fu-berlin.de> typed:
:Again, I look at the manual and  I cannot find
:how to do a simple count within a for loop.
:	$ for count in ???; do
:	> echo -n $count
:	> done
:	1 2 3 4 5 6 7 8 9 10
:What do I need for "???"?
:Why isn't this in the manual as an example?  *sigh*

The manual does list quite a few methods if you read it above and below
the section on for loops.  Most people would have found a method they
like and use it.  It's not as if there aren't a few choices.  For starters:

% count=1; while ( [[ $count -le 10 ]] ); do echo -n $count; let count++; done
% count=1; while ( [[ $count -le 10 ]] ); do echo -n $count; (( count++ )); done
% count=1; while ( [[ count -le 10 ]] ); do echo -n $count; let count++; done
% count=1; while ( [[ count -le 10 ]] ); do echo -n $count; (( count++ )); done
% count=1; while ( [[ $count -le 10 ]] ) { echo -n $count; let count++ }
% count=1; while ( [[ $count -le 10 ]] ) { echo -n $count; (( count++ )) }
% count=1; while ( [[ count -le 10 ]] ) { echo -n $count; let count++ }
% count=1; until ( [[ $count -gt 10 ]] ) { echo -n $count; (( count++ )) }
% count=1; until ( [[ count -gt 10 ]] ); do echo -n $count; let count++; done
% count=1; until ( [[ $count -gt 10 ]] ); do echo -n $count; (( count++ )); done
% count=1; until ( [[ count -gt 10 ]] ) { echo -n $count; let count++ }
% count=1; until ( [[ count -gt 10 ]] ) { echo -n $count; (( count++ )) }
% count=1; repeat 10; do echo -n $count; let count++; done
% count=1; repeat 10; do echo -n $count; (( count++ )); done
% count=1; repeat 10 { echo -n $count; let count++ }
% count=1; repeat 10 { echo -n $count; (( count++ )) }
% for count in {1..10}; do echo -n $count; done
% for count in {1..10}; echo -n $count
zsh-3.1.5% for (( count=1; count <= 10; count++ )); do echo -n $count; done
zsh-3.1.5% for ((count=1; count<=10; count++)) echo -n $count

-- 
Geoff Wing   <gcw@pobox.com>            Mobile : 0412 162 441
Work URL: http://www.primenet.com.au/   Ego URL: http://pobox.com/~gcw/


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

* Re: counting in "for" loops
  1998-12-03  4:22 ` Geoff Wing
@ 1998-12-03 21:26   ` Phil Pennock
  1998-12-03 21:27     ` Geoff Wing
  1998-12-04 15:53     ` Phil Pennock
  0 siblings, 2 replies; 9+ messages in thread
From: Phil Pennock @ 1998-12-03 21:26 UTC (permalink / raw)
  To: zsh-users

Typing away merrily, Geoff Wing produced the immortal words:
> like and use it.  It's not as if there aren't a few choices.  For starters:

Mostly just variants on a theme.  With the problem that they all look to
produce:
12345678910 (with no newline)

% ( local foo; set -A foo {1..10} ; print $foo )
% ( local foo; print ${(A)foo::={1..10}} )
Handle it, without the trailing space of another suggestion.

I wanted:
% print ${(A)_::={1..10}}
which would nicely leave $_ with the values, but _ is a readonly
variable.  *sigh*
-- 
--> Phil Pennock ; GAT d- s+:+ a22 C++(++++) UL++++/I+++/S+++/H+ P++@ L+++
E-@ W(+) N>++ o !K w--- O>+ M V !PS PE Y+ PGP+ t-- 5++ X+ R !tv b++>+++ DI+ D+
G+ e+ h* r y?


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

* Re: counting in "for" loops
  1998-12-03 21:26   ` Phil Pennock
@ 1998-12-03 21:27     ` Geoff Wing
  1998-12-04 15:53     ` Phil Pennock
  1 sibling, 0 replies; 9+ messages in thread
From: Geoff Wing @ 1998-12-03 21:27 UTC (permalink / raw)
  To: zsh-users

Phil Pennock <phil@athenaeum.demon.co.uk> typed:
:Typing away merrily, Geoff Wing produced the immortal words:
:> like and use it.  It's not as if there aren't a few choices.  For starters:
:Mostly just variants on a theme.  With the problem that they all look to
:produce:
:12345678910 (with no newline)

Yes, my mistake.  I blindly whacked -n on the echo.

:% ( local foo; set -A foo {1..10} ; print $foo )
:% ( local foo; print ${(A)foo::={1..10}} )
:Handle it, without the trailing space of another suggestion.
:I wanted:
:% print ${(A)_::={1..10}}
:which would nicely leave $_ with the values, but _ is a readonly
:variable.  *sigh*

Either way, you're not left with only one option.
-- 
Geoff Wing   <gcw@pobox.com>            Mobile : 0412 162 441
Work URL: http://www.primenet.com.au/   Ego URL: http://pobox.com/~gcw/


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

* Re: counting in "for" loops
  1998-12-03 21:26   ` Phil Pennock
  1998-12-03 21:27     ` Geoff Wing
@ 1998-12-04 15:53     ` Phil Pennock
  1 sibling, 0 replies; 9+ messages in thread
From: Phil Pennock @ 1998-12-04 15:53 UTC (permalink / raw)
  To: zsh-users

Typing away merrily, I produced the daft words:
> I wanted:
> % print ${(A)_::={1..10}}
> which would nicely leave $_ with the values, but _ is a readonly
> variable.  *sigh*

The docs for [zshexpn(1)/PARAMETER EXPANSION/Parameter Expansion Flags]
state:
   A   Create an array parameter with ${...:=...} or ${...::=...}.
       Assignment is made before sorting or padding.

However, this also works:
% print ${(A):-{1..10}}
since the null variable is unset.  Undocumented feature?

Be careful not to use:
% print ${(A)-{1..10}}
though as this uses $- (shell flags) and seems to silently drop the (A)
and the {1..10} ... almost as if it parses - twice, once as a variable
and once as 'if not set' which is untrue.

Curious.

I'm also tempted to rewrite some of my initialisation files as:
: ${(A)path::=/bin /usr/bin /wherever/bin}
just to cheer myself up.  *grin*
-- 
--> Phil Pennock ; GAT d- s+:+ a22 C++(++++) UL++++/I+++/S+++/H+ P++@ L+++
E-@ W(+) N>++ o !K w--- O>+ M V !PS PE Y+ PGP+ t-- 5++ X+ R !tv b++>+++ DI+ D+
G+ e+ h* r y?


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

end of thread, other threads:[~1998-12-04 18:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-12-02 16:00 counting in "for" loops Sven Guckes
1998-12-02 16:16 ` Thomas Koehler
1998-12-02 16:21   ` Peter Stephenson
1998-12-03  3:15     ` Updating documentation (Re: counting in "for" loops ) Timothy J Luoma
1998-12-02 16:50 ` counting in "for" loops Bruce Stephens
1998-12-03  4:22 ` Geoff Wing
1998-12-03 21:26   ` Phil Pennock
1998-12-03 21:27     ` Geoff Wing
1998-12-04 15:53     ` Phil Pennock

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