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

Re: Help parsing a file from one regex to another



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.



Messages sorted by: Reverse Date, Date, Thread, Author