zsh-users
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@brasslantern.com>
To: Sweth Chandramouli <sweth@astaroth.nit.gwu.edu>,
	ZSH Users <zsh-users@math.gatech.edu>
Subject: Re: sorting/uniq-ing an array?
Date: Sun, 9 Aug 1998 04:41:23 -0700	[thread overview]
Message-ID: <980809044123.ZM19065@candle.brasslantern.com> (raw)
In-Reply-To: <19980809025636.18149@astaroth.nit.gwu.edu>

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.

: 	(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?)

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:

: 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.)

: 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;

You probably want to be sure that $tmpvar doesn't already have a value
at the time you start any kind of loop like that.  Also, zsh (and most
other shells) has multi-level loop break/continue, so you can restart
the outer loop without setting/testing $append from the inner loop:

tmpvar=()
for i in $ports ; do
  for b in $tmpvar ; do
    [[ $i == $b ]] && continue 2
  done
  tmpvar=($tmpvar $i)
done

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


  reply	other threads:[~1998-08-09 11:54 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-08-09  6:56 Sweth Chandramouli
1998-08-09 11:41 ` Bart Schaefer [this message]
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=980809044123.ZM19065@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=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).