Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: bar='#pat'; ${foo/$bar/...} problem



On Apr 1,  6:04pm, Andrej Borsenkow wrote:
} Subject: bar='#pat'; ${foo/$bar/...} problem
}
} It seems to be impossible to specify `#' (and probably `%') as part of the
} replaced pattern:

What you mean here is, specify left-anchored or right-anchored patterns,
right?  In that case, the # or % is not "part of the pattern", it's part
of the ${...} syntax.  The rest is just a glob pattern and doesn't have
any built-in notion of "anchored."  So really there are three:

	${foo/pat/rep}		pat --> rep, anywhere in foo
	${foo/#pat/rep}		pat --> rep, at left of foo
	${foo/%pat/rep}		pat --> rep, at right of foo

Once you get that, it's easy to see that what you're attempting is the
same as if you did

	% bar=('/#xx' '/zz'}
	% print ${foo$bar[1]$bar[2]}

which you obviously wouldn't expect to work (I hope).

Now, you might argue that the implementation of /# and /% should be as
if # and % are extensions of the glob syntax, and in fact that is how
it appears to be implemented in bash:

	$ foo=xxya
	$ bar='#xx'
	$ echo ${foo/$bar/zz}
	zzya

In the meantime, though, you need an eval step of some kind:

	% foo=(xxya xxyb)
	% bar=('#xx' 'zz')
	% eval print '${foo/'"$bar[1]"'/$bar[2]}'

(Did I get to be article 6000? :-)

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com



Messages sorted by: Reverse Date, Date, Thread, Author