zsh-workers
 help / color / mirror / code / Atom feed
* 3.1.9-dev-6: bug or confusion?
@ 2000-09-13 18:44 E. Jay Berkenbilt
  2000-09-14  4:57 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: E. Jay Berkenbilt @ 2000-09-13 18:44 UTC (permalink / raw)
  To: zsh-workers


Define these two functions:

function okay
{
  emulate -L zsh
  echo ${#${@:#-}}
  setopt localoptions xtrace
  (( ${#${@:#-}} ))
}

function broken
{
  emulate -L zsh
  echo ${#${@#-}}
  setopt localoptions xtrace
  (( ${#${@#-}} ))
}

Notice that the only difference between these two functions is that
one uses :# (match whole word) and the other uses # (match beginning
of word) as applied to each element of $@.

Then run

zsh% okay -

You get the expected output:

0
+okay:5> ((  0  ))

Now run

zsh% broken -

You get the unexpected (by me anyway) output:

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?  Is it a
bug?  If not, I'd be grateful if someone could explain this behavior.

--
E. Jay Berkenbilt (ejb@ql.org)  |  http://www.ql.org/q/


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

* Re: 3.1.9-dev-6: bug or confusion?
  2000-09-13 18:44 3.1.9-dev-6: bug or confusion? E. Jay Berkenbilt
@ 2000-09-14  4:57 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2000-09-14  4:57 UTC (permalink / raw)
  To: E. Jay Berkenbilt, zsh-workers

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   


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

end of thread, other threads:[~2000-09-14  4:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-09-13 18:44 3.1.9-dev-6: bug or confusion? E. Jay Berkenbilt
2000-09-14  4:57 ` 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).