zsh-users
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: for loop question
Date: Mon, 3 Nov 2014 10:22:03 +0100	[thread overview]
Message-ID: <CAHYJk3T4=8fxBKFKJ_7ppLWrNPd4sV9kd9Jss3=f8WihwwgXXQ@mail.gmail.com> (raw)
In-Reply-To: <5456E6EA.2050806@eastlink.ca>

On Mon, Nov 3, 2014 at 3:22 AM, Ray Andrews <rayandrews@eastlink.ca> wrote:
> On 11/02/2014 05:53 PM, Mikael Magnusson wrote:
>>
>> We might want to avoid using obscure mixes of the discouraged alternate
>> syntax with syntax that depends on short_loops being set, when helping
>> people who are asking questions about basic syntax. :)
>
> It does get a bit overwhelming.  However, when I poke at the syntax by
> asking 'why can't we ...' sorts of questions, I expect to more or less have
> my fuses blown ;-)  Still, any sort of heads up about what is considered
> proper vs. what is considered discouraged will be most appropriate.

If you look up the section "Complex Commands" in the manpage, they're
all listed fairly well explained. The section after that is called
"Alternate Forms for Complex Commands" and lists some syntax that is
convenient interactively but not super encouraged. Using any syntax
that depends on SHORT_LOOPS being set will of course not work in any
other shell, and also has the downside that making a small mistake
will usually still parse but do something entirely unexpected. I'm a
bit more opposed to SHORT_LOOPS than others here, but when I disabled
the option, I immediately found like 5 bugs in the completion system
so I think I'm at least somewhat justified :). The whole point of it
is just to not have to write do; done, but it causes so many bugs and
confusion.

As to the for loop, it has two different forms. They're almost so
different you could say the only thing they have in common is the word
"for" :). One is
for some variables in list of values; do
  list; of; commands using $some $variables
done
Think of that as a foreach loop. The other form is
for (( i=0; i<10; i++ )); do
  list; of; commands using $i
done
This form is exclusively for math expressions, you can't just stick
arbitrary commands in there. If you want that, just use a while loop.
commands to initialize state
while my condition; do
  stuff
  here
  increment iterator or whatever
done
because as you saw, stuffing it all into the for line would just get
really ugly. And you want the foreach form for your initial example as
Stephane said, there is no need to iterate over the index unless you
have a veeery large array, and then it will already be very slow
anyway.

As a side note, if you ever want a do-while loop instead of a regular
while loop, the trick is
while just put the whole body;
      of the loop here instead;
      and the last command is;
      the terminating condition; do done

-- 
Mikael Magnusson


  reply	other threads:[~2014-11-03  9:22 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-02 20:47 Ray Andrews
2014-11-02 21:00 ` Bart Schaefer
     [not found] ` <CAH+w=7aWS0xyS4CXRJBphDjesfUFQOsyJRMaG3RZRxmuj7xkOg__20885.3257158355$1414962125$gmane$org@mail.gmail.com>
2014-11-02 21:37   ` Stephane Chazelas
2014-11-02 22:44     ` Ray Andrews
2014-11-02 22:57       ` Bart Schaefer
2014-11-02 23:24       ` Oliver Kiddle
2014-11-03  0:07         ` Ray Andrews
2014-11-03  1:53     ` Mikael Magnusson
2014-11-03  2:22       ` Ray Andrews
2014-11-03  9:22         ` Mikael Magnusson [this message]
2014-11-03 17:26           ` Ray Andrews
2014-11-04  1:56     ` Han Pingtian
2014-11-04  2:29       ` Han Pingtian
2014-11-04  2:43       ` Bart Schaefer
2014-11-04  4:51         ` Han Pingtian
2014-11-04  6:37         ` Ray Andrews
2014-11-04  7:13           ` Bart Schaefer
2014-11-04 18:00             ` Ray Andrews
     [not found]       ` <141103184338.ZM32221__48957.5251042426$1415069142$gmane$org@torch.brasslantern.com>
2014-11-04  7:08         ` Stephane Chazelas

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='CAHYJk3T4=8fxBKFKJ_7ppLWrNPd4sV9kd9Jss3=f8WihwwgXXQ@mail.gmail.com' \
    --to=mikachu@gmail.com \
    --cc=rayandrews@eastlink.ca \
    --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).