From: Bart Schaefer <schaefer@brasslantern.com>
To: "zsh-users@zsh.org" <zsh-users@zsh.org>
Subject: Re: Tip of the day: (@)...[(R)...] is equal to (@M)...:#... and faster by 18-21%
Date: Fri, 21 Oct 2016 09:20:27 -0700 [thread overview]
Message-ID: <161021092027.ZM8789@torch.brasslantern.com> (raw)
In-Reply-To: <536851559.167591.1477040396961@mail.yahoo.com>
On Oct 21, 8:59am, <psprint@yahoo.com> wrote:
}
} found=( "${(@M)history:#(#i)*$~search_pattern*}" )
}
} can be replaced by:
}
} found=( "${(@)history[(R)(#i)*$~search_pattern*]}" )
}
} I wonder if there are there any pitfalls in such replacement?
There are some differences in the way the pattern may be interpreted
when it appears inside a subscript expression. There is an entire
section of the documentation (15.2.4 in "info" or HTML docs) devoted
to explaining this. One paragraph in particular applies here:
A last detail must be considered when reverse subscripting is
performed. Parameters appearing in the subscript expression are
first expanded and then the complete expression is interpreted
as a pattern. This has two effects: first, parameters behave as
if GLOB_SUBST were on (and it cannot be turned off); second,
backslashes are interpreted twice, once when parsing the array
subscript and again when parsing the pattern. In a reverse
subscript, it's necessary to use _four_ backslashes to cause a
single backslash to match literally in the pattern. For complex
patterns, it is often easiest to assign the desired pattern to a
parameter and then refer to that parameter in the subscript, because
then the backslashes, brackets, parentheses, etc., are seen only
when the complete expression is converted to a pattern. To match
the value of a parameter literally in a reverse subscript, rather
than as a pattern, use `${(q)NAME}' to quote the expanded value.
So in your example you don't need "$~search_pattern", the expansion is
always interpreted as a pattern. I think the literal match advice in
the final sentence could preferentially be ${(b)...} nowadays; there
are a couple of things about the array subscripting doc that might
need update.
prev parent reply other threads:[~2016-10-21 16:20 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <536851559.167591.1477040396961.ref@mail.yahoo.com>
2016-10-21 8:59 ` psprint
2016-10-21 16:20 ` 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=161021092027.ZM8789@torch.brasslantern.com \
--to=schaefer@brasslantern.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).