From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5540 invoked from network); 19 Jul 1999 05:05:22 -0000 Received: from sunsite.auc.dk (130.225.51.30) by ns1.primenet.com.au with SMTP; 19 Jul 1999 05:05:22 -0000 Received: (qmail 6694 invoked by alias); 19 Jul 1999 05:05:12 -0000 Mailing-List: contact zsh-workers-help@sunsite.auc.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 7189 Received: (qmail 6687 invoked from network); 19 Jul 1999 05:05:11 -0000 From: "Bart Schaefer" Message-Id: <990719050457.ZM8827@candle.brasslantern.com> Date: Mon, 19 Jul 1999 05:04:57 +0000 In-Reply-To: Comments: In reply to Tanaka Akira "Re: string range between 1 and 0." (Jul 19, 10:01am) References: <990719000259.ZM8741@candle.brasslantern.com> X-Mailer: Z-Mail (5.0.0 30July97) To: Tanaka Akira , zsh-workers@sunsite.auc.dk Subject: Re: string range between 1 and 0. MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii On Jul 19, 10:01am, Tanaka Akira wrote: } Subject: Re: string range between 1 and 0. } } region="$buffer[pos1,pos2]" } } I represent a region by pos1 and pos2. } To represent null region, I assign pos2 to pos1 - 1. } I think it's natural, but it's not works when pos1 is 1. That scheme wouldn't work in ksh mode in any case ... ${buffer[0,-1]} is the whole buffer, not the empty string. I presume you want pos1 to continue to refer to the start of the region, so doing something like ((pos1=pos2+1)) is right out. } So, my preference is "treat [n,0] as null string if 0 < n". The difficulty with doing this is that C arrays are indexed from 0. So when zsh uses 1-based indices, it has to decrement them before indexing into the internal representation of the value. This decrement is done at parse time (unless the value is already zero), so that thereafter the same code can be used for ksh and zsh arrays. Since negative numbers are already used for indexing back from the end, there isn't any value to represent the nonexistent position, which is in effect what you're asking for (because the only position to the left of the leftmost is the one that doesn't exist). This shows up in other places: $x[1,-$#x] == $x[1] but $[2,-$#x] == "". Anything that might refer to a position "off the low end" of the array is instead forced to refer to position 1. Ksh emulation aside, this is the result of a decision that only bad syntax (not bad numbers) would be permitted to result in a subscripting error. It might have been better if zsh subscripts had been $var[pos,len] form, so that a length of 0 could represent an empty range, but it's a lot too late for that now ... } Is there exists a more smart code? } # For example, is it representable with only variable expansion? If you represent the null region by unsetting pos2 or setting it empty, you can do this: ${pos2:+$buffer[pos1,pos2]} -- Bart Schaefer Brass Lantern Enterprises http://www.well.com/user/barts http://www.brasslantern.com