zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: counting trouble
Date: Wed, 4 Apr 2018 18:15:40 -0700	[thread overview]
Message-ID: <CAH+w=7bJHzWRq9W8TCog1hR=8gi48X8S64sK6tNw8NQTVzDiCQ@mail.gmail.com> (raw)
In-Reply-To: <469d8a16-69c9-79f2-bdce-467886913158@eastlink.ca>

On Wed, Apr 4, 2018 at 11:21 AM, Ray Andrews <rayandrews@eastlink.ca> wrote:
>
>     tmp=( $1*(N) )
>
>     if [[ "$#tmp" > 1 ]]; then
>
> Alas, if there is only one match then the count becomes a count not of lines
> but of characters which is a nasty gotcha.

That can't be all there is to it.  $#array only becomes a count of
characters if the context forces the array into string form.  Which
admittedly can happen in some non-obvious ways, but what you've shown
above is not one of those ways.  What are you really doing?

> Hacking away I find this works:
>
>     [ -e "$1" ] && tmp=( "${(f)${1}}" ) || tmp=( ${1}*(N) )

Umm ...  [ -e "$1" ] will only succeed for a single literal file name,
not a pattern, and the (f) flag means to split on newlines, so unless
you have a file with newlines embedded in the name (in which case this
is entirely broken a different way) there's no reason for ${(f)...}
there at all.  And none of this has anything to do with how [[ $#tmp >
1 ]] would work after the assignment.

> expected this to work: " ${(f)${1}*} " but it doesn't.

Expected it to do what?  Filename generation like ${1}* out in the
open (so to speak)?  What led you to expect that?  In any case
filename generation would never produce a newline-separated string
(again barring files with newlines in the name), so (f) would not do
anything to the result.

Please don't think about globbing as returning "lines".  It doesn't.


  reply	other threads:[~2018-04-05  1:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-04 18:21 Ray Andrews
2018-04-05  1:15 ` Bart Schaefer [this message]
2018-04-05  2:07   ` Ray Andrews
2018-04-05  4:48     ` Bart Schaefer
2018-04-05 13:45       ` Ray Andrews

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='CAH+w=7bJHzWRq9W8TCog1hR=8gi48X8S64sK6tNw8NQTVzDiCQ@mail.gmail.com' \
    --to=schaefer@brasslantern.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).