Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: (s) splitting – is there any way to provide «dynamic» separator
On Thu, 09 Oct 2014 21:00:32 +0600
Vasiliy Ivanov <beelzebubbie.logs@xxxxxxxxx> wrote:
> Sorry if I (maybe) missed something obvious, but I failed to find a way to use separator from
> parameter (e.g. a='1:2:3'; sep=':'; print -l ${(s.$sep.)a}).
> I think this is because expansion/substitution are not available in
> flags? If so, is there any another way for such a «dynamic» splitting?
I don't think you've missed anything obvious or elegant.
One way is
(){
local IFS=$sep
print -l ${=a}
}
Or if you can rely on not having whitespace
print -l ${=${a//:/ }}
Or, of course,
eval print -l '${(s.'$sep'.)a}'
as long as you can sanity check $sep.
Of course there's nothing to stop us adding a simple parameter
substitution as a special case at this point (or any similarly delimited
parameter argument) ... Can't see how this can do much harm since it
sure as heck *looks* like a parameter expansion.
diff --git a/Src/subst.c b/Src/subst.c
index 1aa9b98..5727c12 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -1385,12 +1385,23 @@ static char *
untok_and_escape(char *s, int escapes, int tok_arg)
{
int klen;
- char *dst;
+ char *dst = NULL;
- untokenize(dst = dupstring(s));
- if (escapes) {
- dst = getkeystring(dst, &klen, GETKEYS_SEP, NULL);
- dst = metafy(dst, klen, META_HREALLOC);
+ if ((*s == String || *s == Qstring) && s[1]) {
+ char *pstart = s+1, *pend;
+ for (pend = pstart; *pend; pend++)
+ if (!iident(*pend))
+ break;
+ if (!*pend) {
+ dst = dupstring(getsparam(pstart));
+ }
+ }
+ if (dst == NULL) {
+ untokenize(dst = dupstring(s));
+ if (escapes) {
+ dst = getkeystring(dst, &klen, GETKEYS_SEP, NULL);
+ dst = metafy(dst, klen, META_HREALLOC);
+ }
}
if (tok_arg)
shtokenize(dst);
--
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
Messages sorted by:
Reverse Date,
Date,
Thread,
Author