zsh-users
 help / color / mirror / code / Atom feed
From: Sweth Chandramouli <sweth@astaroth.nit.gwu.edu>
To: ZSH Users <zsh-users@math.gatech.edu>
Subject: sorting/uniq-ing an array?
Date: Sun, 9 Aug 1998 02:56:36 -0400	[thread overview]
Message-ID: <19980809025636.18149@astaroth.nit.gwu.edu> (raw)

	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
time.
	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?)

	tia,
	sweth.

--- 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
         append='false';
      fi;
   done;
   if [[ -z $append ]] ; then
      tmpvar=($tmpvar $i);
   fi;
done;
ports=(${=tmpvar});
--- end quoted text ---

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


             reply	other threads:[~1998-08-09  7:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-08-09  6:56 Sweth Chandramouli [this message]
1998-08-09 11:41 ` Bart Schaefer
1998-08-09 16:36   ` Sweth Chandramouli
1998-08-09 17:50     ` Bart Schaefer
1998-08-10 11:34     ` Bruce Stephens

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19980809025636.18149@astaroth.nit.gwu.edu \
    --to=sweth@astaroth.nit.gwu.edu \
    --cc=zsh-users@math.gatech.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).