zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: Zsh hackers list <zsh-workers@sunsite.dk>
Subject: Re: Updated _acroread completer
Date: Mon, 25 Apr 2005 14:36:40 +0100	[thread overview]
Message-ID: <200504251336.j3PDafGF025206@news01.csr.com> (raw)
In-Reply-To: <20050425130002.GA3137@fox>

Haakon Riiser wrote:
> By the way, it uses "sed -n 's/^ver=//p' FILE" to extract the
> version.  Is it preferable to use zsh's own string functions to
> do this?  The code tends to become quite hard to read, and I usually
> need to spend much more time writing it than when I use sed or awk.

As you need to scan an entire file for this, sed is probably as good as
anything.  However, you'll see things as bad as the following, and even
worse, inside the completion code.

The zsh version of:

local ver=$(sed -n 's/^ver=//p' $commands[$words[1]] 2>/dev/null)

would be (with EXTENDED_GLOB set):

ver=${${${(f)"$(<$commands[$words[1]])"}:#^ver=*}##ver=}

which isn't fantastically easy to understand:

  "$(<$commands[$words[1]])"

Output the contents of the file given by $commands[$words[1]] as a
single string.

  ${(f)"$(<$commands[$words[1]])"}

Split the result into an array of lines.  (This forces array context
despite the scalar assignment.)

  ${${(f)"$(<$commands[$words[1]])"}:#^ver=*}

Remove all elements of the resulting array (lines of the file) that
match ^ver=*, i.e. all lines except those that match ver=*.

  ${${${(f)"$(<$commands[$words[1]])"}:#^ver=*}##ver=}

Remove the ver= from the head of the result.  At this point it will be
treated as a scalar for the assignment, but that's OK if there's just
the one match.

There's no equivalent of sed's p (do the substitution and output the
result if the substitution succeeded), which is why we need to match on
the ver= twice.  But there are still spare letters for flags...

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************


      reply	other threads:[~2005-04-25 13:37 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-14 13:47 Haakon Riiser
2005-04-15 11:11 ` Haakon Riiser
2005-04-25  7:43   ` Oliver Kiddle
2005-04-25  8:41     ` Haakon Riiser
2005-04-25  8:54       ` Oliver Kiddle
2005-04-25  8:58         ` Haakon Riiser
2005-04-25  9:39       ` Peter Stephenson
2005-04-25 13:00         ` Haakon Riiser
2005-04-25 13:36           ` Peter Stephenson [this message]

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=200504251336.j3PDafGF025206@news01.csr.com \
    --to=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /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).