zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-workers@zsh.org
Subject: Word splitting/joining inside [[ ]]
Date: Fri, 11 Dec 2015 20:31:47 -0800	[thread overview]
Message-ID: <151211203147.ZM4122@torch.brasslantern.com> (raw)

I have a string of words:

words="I have a string of words"

What I want to learn is whether the words are already in sorted order.

So my thought is to split the string on spaces, sort the resulting array,
and join the string back together again on spaces, and compare that string
to the original string.

So I wrote this:

[[ $words = ${(on)=words} ]]

Doesn't work.  It's always true.  Confused, I setopt xtrace and discover:

torch% [[ $words != ${(on)=words} ]]
+zsh:6> [[ 'I have a string of words' = I\ have\ a\ string\ of\ words ]]

Oh, I have the (on) at the wrong nesting level.

torch% [[ $words = ${(on)${=words}} ]]
+zsh:7> [[ 'I have a string of words' == I\ have\ a\ string\ of\ words ]]

Er, apparently that's not it.  Works fine outside [[ ]]:

torch% print ${(on)${=words}}
+zsh:8> print a have I of string words
a have I of string words

But then so does the original:

torch% print ${(on)=words} 
+zsh:9> print a have I of string words
a have I of string words

By accident I discovered that adding a supposedly-meaningless extra level
of nested expansion makes it work:

torch% [[ $words = ${${(on)=words}} ]]
+zsh:12> [[ 'I have a string of words' == a\ have\ I\ of\ string\ words ]]

What's going on here?


             reply	other threads:[~2015-12-12  4:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-12  4:31 Bart Schaefer [this message]
2015-12-13 18:28 ` Peter Stephenson

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=151211203147.ZM4122@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@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).