zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@sunsite.dk
Subject: Re: Help parsing a file from one regex to another
Date: Sat, 09 Jul 2005 17:16:33 +0000	[thread overview]
Message-ID: <1050709171633.ZM17742@candle.brasslantern.com> (raw)
In-Reply-To: <20050709064103.GC3825@localhost.localdomain>

On Jul 9,  4:41pm, Doug Kearns wrote:
} Subject: Re: Help parsing a file from one regex to another
}
} I always forget that a non-matching pattern in the second expression
} causes the rest of the array to be selected. Is this documented
} somewhere that I'm missing?

Hmm, no, I guess not.

The way the (i) and (I) subscript flags work (on an ordinary array, not
on an associative one) is that either they resolve to a matching index,
or they "fall off" the array in the direction that they're searching and
thus resolve to an index that has the correct magnitude but that can be
distinguished from any successful match.

So, given an array in which no element has the value "missing":

  $array[(I)missing] == 0
  $array[(i)missing] == $(($#array+1))

This is so you can get sensible results from tests like

  if [[ $array[(i)foreward] -ge $array[(I)backward] ]]; then : ...
  fi

Despite the documentation explaining (i) in terms of (r), the actual
implementation of (r) is more like $array[$array[(i)missing]], so the
side-effect of $array[0] being the same as $array[1] comes into play.

  ${(k)array[(R)missing]} == 0
  ${(v)array[(R)missing]} == $array[1]
  ${(k)array[(r)missing]} == $(($#array+1))
  ${(v)array[(r)missing]} == ""

and

  $array[(R)missing,(r)missing] == $array[*]

As a final note ... this behavior of (i) differs in really old versions
of zsh, where (as I recall) a missing element was always resolved to 0,
which meant that $array[$array[(i)missing]] != $array[(r)missing], which
seemed like a bad thing, so it was changed.


      reply	other threads:[~2005-07-09 17:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-08  6:36 Travis Spencer
2005-07-08  7:30 ` Doug Kearns
2005-07-08 17:16   ` Bart Schaefer
2005-07-08 19:24     ` Travis Spencer
2005-07-09  6:41     ` Doug Kearns
2005-07-09 17:16       ` Bart Schaefer [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=1050709171633.ZM17742@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@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).