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

sorting/uniq-ing an array?

	while playing around with zsh completion, i noticed that the
ports variable set in the comctl-examples file doesn't distinguish
between tcp and udp services in /etc/services, resulting in a non-unique
array (e.g. multiple instances of "echo").  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--on
a single-user ultrasparc/30, it adds about two seconds to my login
	is there a faster way to do this?  for some uses, it's okay--
i've got a function called addpath, for example, that uses this sort
of looping to scan $PATH to see if the argument passed to addpath is
already in it, and iff it isn't, adds it, and a second or two to
process that function (usually almost unnoticeable, actually, since
$PATH is so much shorter than $ports) doesn't seem strange.  as an
addition to the login process, however, it seems like it takes an
eternity.  i was thinking of creating a file that stored the array,
and on login, first populate $ports with the contents of that file, and
then spawned a bg process to do the parsing from /etc/services,
write the output out to the file, and then repopulate $ports.  any
suggestions on how to do that, or of other (quicker) ways to do the 
parsing of the array?
	(as always, of course, comments on style are also appreciated.
i'm fairly certain that i still don't understand when ${= is needed
and when it isn't, so i just tend to use it everywhere that splitting
might be needed.  did i do it right here?)


--- begin quoted text ---
#### ports is made unique in an ugly way.  is this fixable?
portnames=("${${${(f)$(</etc/services)}:#\#*}%%[        ]*}")
portnums=("${${${${${(f)$(</etc/services)}:#\#*}%%/*}##*        }##* }")
ports=(${=portnums} ${=portnames})
# unique-ify ports
for i in $ports ; do
   unset append;
   for b in ${=tmpvar} ; do
      if [[ $i == $b ]] ; then
   if [[ -z $append ]] ; then
      tmpvar=($tmpvar $i);
--- end quoted text ---

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