zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: linuxtechguy@gmail.com, zsh-users@zsh.org
Subject: Re: A bug or improperly formatted script
Date: Sat, 25 Feb 2017 14:12:11 -0800	[thread overview]
Message-ID: <170225141211.ZM20788@torch.brasslantern.com> (raw)
In-Reply-To: <CA+rB6GJoCDbdEVYR-zYEh6P0Oau7VAxQeW6UcnksdeB3hvL=HQ@mail.gmail.com>

On Feb 25, 12:34pm, Jim wrote:
}
} While doing a search, unrelated, I found the following on the
} zsh-lovers(1) man page:
} 
} # Count the number of directories on the stack
}   $ print $((${${(z)${(f)"$(dirs -v)"}[-1]}[1]} + 1)) #...
} 
} I was surprised the first time I tried it I got "-2" as the output.
} 
} It would appear as if the original script is returning
} a scalar when the stack only has one entry and an array when the
} stack has 2 or more entries.

Yes, this is a known / traditional behavior, and it somewhat regularly
comes up in examples where a program has been tested only in the "more
than one element" cases.

The problem is that nesting ${${...}} "wants" to treat the inner ${...}
as a scalar, and will do so unless something else forces it to be an
array.  (f) doesn't accomplish that because $(dirs -v) produces only
one line of output with the final newline stripped, so there is nothing
for (f) to split on.  (@) won't accomplish it because it only means
that things that are already arrays should remain so.

So what can be done to force interpretation as an array here?  Unless
someone else has a better solution, the answer is to forcibly assign
the result to an array variable:

print $((${${(z)${(Af)reply::="$(dirs -v)"}[-1]}[1]} + 1))

Once we are expanding ${reply} rather than ${"$(dirs -v")} the array
property is preserved and [-1] remains an array subscript instead of
becoming a scalar string slice.

A follow-up to this is going to zsh-workers.


  reply	other threads:[~2017-02-25 22:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-25 18:34 Jim
2017-02-25 22:12 ` Bart Schaefer [this message]
2017-02-25 22:53   ` Jim
2017-02-25 23:41     ` Bart Schaefer
2017-02-26  0:58       ` Jim

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=170225141211.ZM20788@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=linuxtechguy@gmail.com \
    --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).