zsh-users
 help / color / mirror / code / Atom feed
* leading zeros in for/foreach loops
@ 2016-02-15 18:07 Andy Spiegl
  2016-02-15 18:59 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Andy Spiegl @ 2016-02-15 18:07 UTC (permalink / raw)
  To: Zsh Users

% for i in {01..03}; echo $i
01
02
03

% foreach i ( {01..03} ); echo $i; end
01
02
03

But these two commands don't give me leading zeros if I FIRST run this:
% for ((i=1;i<=3;i++)) echo $i
1
2
3

Can someone please explain this to me?  Does this for loop do
something strange with the variable "i"?  How can I avoid that?

Thanks so much!
 Andy


-- 
 Our scientific power has outrun our spiritual power.
 We have guided missiles and misguided men.
   (Martin Luther King Jr.)


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

* Re: leading zeros in for/foreach loops
  2016-02-15 18:07 leading zeros in for/foreach loops Andy Spiegl
@ 2016-02-15 18:59 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2016-02-15 18:59 UTC (permalink / raw)
  To: Zsh Users

On Feb 15,  7:07pm, Andy Spiegl wrote:
}
} Can someone please explain this to me?  Does this for loop do
} something strange with the variable "i"?  How can I avoid that?

If the first assignment to an undeclared variable occurs in an
arithmetic context, the variable is given numeric type (integer or
float, depending on what has been assigned to it).  "for (( ... ))"
syntax is such a context.

You can avoid this implicit declaration by explicitly declaring the
variable before using it in any of your loops, e.g., "typeset i" or
"declare i" or "local i" or the appropriate synonym.  Or "unset i"
after the loop is finished.


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

end of thread, other threads:[~2016-02-15 18:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-15 18:07 leading zeros in for/foreach loops Andy Spiegl
2016-02-15 18:59 ` Bart Schaefer

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