zsh-workers
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: "E. Jay Berkenbilt" <ejb@ql.org>, zsh-workers@sunsite.auc.dk
Subject: Re: 3.1.9-dev-6: bug or confusion?
Date: Thu, 14 Sep 2000 04:57:42 +0000	[thread overview]
Message-ID: <1000914045742.ZM7044@candle.brasslantern.com> (raw)
In-Reply-To: <200009131844.OAA01710@soup.ads.apexinc.com>

On Sep 13,  2:44pm, E. Jay Berkenbilt wrote:
} Subject: 3.1.9-dev-6: bug or confusion?
}
} Define these two functions:
} 
} function okay
} {
}   emulate -L zsh
}   echo ${#${@:#-}}
}   setopt localoptions xtrace
}   (( ${#${@:#-}} ))
} }
} 
} function broken
} {
}   emulate -L zsh
}   echo ${#${@#-}}
}   setopt localoptions xtrace
}   (( ${#${@#-}} ))
} }
} 
} zsh% broken -
} 0
} +broken:5> ((  1  ))
} 
} Why would the value be 1 inside (( ... )) and 0 outside in the second
} case but not in the first?  Does it have something to do with an empty
} string being counted as a word in one case but not the other?

Yes, it has something to do with that.

    (( stuff ))
    
is equivalent to

    let "stuff"

(note the double quotes).  So the two expansions in `broken' should be
akin to the difference between $@ (without quotes) and "$@".

Where it gets confusing is that, in the nested expansion ${#${@}}, you
first have the expansion of the inner $@, which (when not in quotes)
implies that the empty elements disappear, and then the count is done
on the remaining array.  This is not the same as ${#@}, which counts
the array *before* expanding it (see below).

Aside to EJR:  I should have remembered this.

Finally, in ${@#-}, the pattern match/delete on each word is performed
*before* the expansion, so again the resulting empty elements (if any)
disappear only when the whole expression is not in quotes.

Aside:  This particular item is missing from the 11 "Rules" of parameter
expansion in the Expansion chapter of the zsh manual.  It happens after
step 7, at approximately the same time as step 8 (because empty elements
resulting from an (s) modifier are completely removed, even in quotes,
but separators inserted by (j) are always added between the empties).
Also missing is an exact indication of when ${^...} ${#...} and ${~...}
are applied; ${#...} actually happens between steps 6 and 7, whereas the
other two don't happen until after step 8.  (Care to fix this up in a
more accurate way in the docs, Peter?)

The definition of ${...:#...} says explicitly that matched elements are
completely removed from the array, so that's why there's no difference
in the `okay' case.  (And that happens at step 6, before (j).)

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


      reply	other threads:[~2000-09-14  4:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-09-13 18:44 E. Jay Berkenbilt
2000-09-14  4:57 ` Bart Schaefer [this message]

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=1000914045742.ZM7044@candle.brasslantern.com \
    --to=schaefer@candle.brasslantern.com \
    --cc=ejb@ql.org \
    --cc=zsh-workers@sunsite.auc.dk \
    /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).