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

Re: "joining" an array with tabs (default is space)



On Wed, Dec 10, 2008 at 07:00:30AM +0000, Stephane Chazelas wrote:
> On Tue, Dec 09, 2008 at 11:27:38AM -0800, Webb Sprague wrote:
> > Hi all,
> > 
> > If I set IFS='\t', it works great with read -A LINE.  However I want
> > to join the output using '\t' instead of spaces-- is there a simple
> > way to do that?  I can write a for loop, or maybe recycle print -f
> > "\t%s", or maybe use columns in the print, but I was hoping for a
> > super graceful (ie typical zsh) approach.
> [...]
> 
> Try IFS=$'\t'
> Then "${var[*]}" will be joined with spaces.
[...]

Oops, brain is going slow in winter....


I meant "with tabs" above not spaces obviously.

"$*" (quotes are important) joins the elements of the array with
the first character of $IFS (or sticks them together if $IFS is
empty).

> It's the same in
> every Bourne-like shell but the Bourne shell (and earlier Almquist
> shells)
> 
> $ IFS=$'\t'
> $ set a b
> $ echo "$*" | cat -t
> a^Ib
> $ a=(a b)
> $ echo "${a[*]}" | cat -t
> a^Ib
> 
> Actually, with zsh (and zsh only) $array is the same as
> ${array[*]}, so:
> 
> $ echo "$a" | cat -t
> a^Ib
[...]

-- 
Stephane



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