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

Re: Re: sorting/uniq-ing an array?



On Sun, Aug 09, 1998 at 04:41:23AM -0700, Bart Schaefer wrote:
> On Aug 9,  2:56am, Sweth Chandramouli wrote:
> : Subject: sorting/uniq-ing an array?
> :
> : i've come up with a way
> : to remove redundant entries from an array (the appropriate part of
> : my .zshrc is at the end of this message), but it's very slow
> :
> : 	is there a faster way to do this?
> 
> Yes;
> 
> typeset -U ports
> 
> but the completion code makes its internal list of completions unique,
> so it doesn't really help to remove the redundant entries from arrays
> in advance when using them only for completion.
	that's good to know, though there are other places where it would
be useful (such as the addpath fn i mentioned) to unique-ify.  also, i was
using portnums and portnames elsewhere, for non-completion purposes (see
below).
	i actually had thought that there was some sort of typeset switch
to do this, but did a man typeset, and didn't see any.  i now realize that
the typeset manpage on my machine is for the ksh and csh versions, and that
the zsh one is only in the zshbuiltins manpage.  what do people think of
breaking the manpages for the builtins out of zshbuiltins (or rather, copying
them out, since i think man zshall should still always be the reference of
last resort), and maybe merging them in with the equivalent pages for other
shell builtins?  if there's an interest in it, i might take a stab at it; i
just downloaded yodl, so i need a reason to learn it, and this is as good as
any others i could conjure up.

> You almost never need ${=...} when the "..." refers to an array.  Using
> ${=...} on an array causes the individual elements of the array to be
> split into words, which is not very often what is wanted.  So no, you
> didn't do it right here:
	upon further consideration, this is a leftover ksh-ism in my brain--
i wasn't thinking of these as being in an array context, because in ksh an
array without a subscript is actually just the last scalar in that array;
i was then assuming that i needed to break the scalars that i was referencing
up--hence the ${= constructs.

> 
> : portnames=("${${${(f)$(</etc/services)}:#\#*}%%[        ]*}")
> : portnums=("${${${${${(f)$(</etc/services)}:#\#*}%%/*}##*        }##* }")
> : ports=(${=portnums} ${=portnames})
> 
> However, you're over-processing.  You have both the port numbers and names
> at one point during the portnums= computation, but then you throw the names
> away.  Keep them:
> 
> ports=("${=${${(f)$(</etc/services)}:#\#*}%%/*}")
> 
> (Note that there I used ${=...} to re-split the ${(f)...} array of lines,
> so sometimes that _is_ what you want.  I did say _almost_ never.)
	this works, which surprises me, since that implies that SH_WORD_SPLIT
splits on either spaces or tabs (there are both in my /etc/services as
delimiters between portnames and portnumbers); i thought that IFS was by
default just a space.  or am i just misunderstanding what you are doing here
entirely?

	-- sweth.
-- 
Sweth Chandramouli
IS Coordinator, The George Washington University
<sweth@xxxxxxx> / (202) 994 - 8521 (V) / (202) 994 - 0458 (F)
<a href="http://astaroth.nit.gwu.edu/~sweth/disc.html";>*</a>



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