Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: PATCH: 4.1: multi-parameter for loop



"Andrej Borsenkow" wrote:
> Which opens up a question of odd number of list elements. It is invalid in
> assignment to hash - shuld it be invalid here as well?

This is covered in the manual page.

  for p1 p2 in foo bar something else rubbish; do
    print $p1 $p2
  done

prints (I cut and pasted this from actually trying it out):

  foo bar
  something else
  rubbish

and you get an empty string for the final $p2.  I didn't thing there was
any point in doing too many sums --- particularly since you can use more
than two parameters; here's a way of naming a set of function arguments:

  fn() {
    local nam cmd val dsc
    for nam cmd val dsc; do; break; done;

    print "$nam, $cmd, $val, $dsc"
  }

Then `fn Me nothing none dunno' prints `Me, nothing, none, dunno'.

With regard to `in', the standard interpretation is the one taken in all
the suggestions I've seen so far.  I should make this clear in the manual,
though.

> bor@itsrm2% for i
> for> in a b c
> bor@itsrm2%
> ^^^^^^^^^^^^^^^ Oops!

That shouldn't be too hard to fix, and wouldn't affect the new syntax:  we
just terminate the argument list at the end of the first line.  Currently
it thinks the newline means you're not going to give an `in' and are using
positional parameters.  I don't know why there's no syntax error, though,
but you get one if you set NO_SHORT_LOOPS.

> I guess, Bart suggestion (use foreach) is better. And more Perlish :-)

I'm not so convinced, since `for' and `foreach' currently do the same
thing, and I don't really like dividing the syntax to make people use the
non-standard variant just for one use.  foreach is automatically handled at
the moment, as well as for, by the way.

> foreach key val hash
> 
> or even
> 
> foreach val array
> 
> i.e. do *not* expand $hash/%array value? For large arrays it may result in
> significant speedup.

It's possible, but actually not that effective for hashes, since to iterate
through them they have to be flattened, and that only happens once in the
current system anyway.

By the way, I discovered a problem:  alias expansion is turned on for
parameter names after the first;

  alias h='history -f'
  for g h in foo bar; do; done

turns `h' into `history -f' and the -f causes an unexpected parse error.
This is because of the csh-style syntax --- the shell is looking for a `('
which it needs to parse as a single token, so it uses the parsing rules for
command position.  But now I know about it I can turn alias expansion off
explicitly for that bit.

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************



Messages sorted by: Reverse Date, Date, Thread, Author