From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10650 invoked by alias); 17 May 2012 21:56:28 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 17092 Received: (qmail 24330 invoked from network); 17 May 2012 21:56:26 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 Received-SPF: neutral (ns1.primenet.com.au: 74.125.82.171 is neither permitted nor denied by SPF record at ntlworld.com) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-proxyuser-ip:date:from:to:subject:message-id:in-reply-to :references:x-mailer:mime-version:content-type :content-transfer-encoding:x-gm-message-state; bh=l+2Cuv5iy8A+r2m8TDWTUS3IJ67h4V/xoi14h9dg6vM=; b=jkcNRLuhveOGoc7StIGHxPfnXaCer90uks/GF5mU7k4yLII2/6ygBi8jnUTRmkWB5W xwqNf4A3CYMob2gQtv26s+s3YQczqRvbChOgSll3sXWoUC6H1t8d5Pcas20qSbslOy77 PoX8qnwQr/AI33MErnxqJl8f2PRPv0TpwLQemPIB6UeOUliaNIqSrWxNKjzLZn3MYfYq 3whBFqBnQkdqmIBiZS8z5sEHKRGbaPPsNSKFR5E9L20kDT1AlLtu8UboznxHtrdIYbY+ ar5ySXhvub6Stfq28UNFhoOKvWTRIWVQ5bZjl4gttmb6mj0qM7fld1NihL0L0V/bqV6h no9g== X-ProxyUser-IP: 86.6.29.42 Date: Thu, 17 May 2012 21:04:42 +0100 From: Peter Stephenson To: Zsh-Users List Subject: Re: How do I find shortest match? Message-ID: <20120517210442.68155e61@pws-pc.ntlworld.com> In-Reply-To: References: <20120517200503.7017ac87@pws-pc.ntlworld.com> X-Mailer: Claws Mail 3.8.0 (GTK+ 2.24.7; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQk/2FF+PjJin+qlFCz26D8OmIDrGAQJgRjDK/GR0PlAcv3fQe3bW0TtebwfI44RXeSuL/bJ On Thu, 17 May 2012 15:11:09 -0400 TJ Luoma wrote: > > local min > > min=(${${(o)${MATCHES//?/\?}}[1]}) > > print -l ${MATCHES:#^${~min}} > > > > I'm sure it's completely obvious what this is doing. It's been > posted before. > I have the memory and attention span of a tsetse fly, so I'll take > your word for it, I think it was some years ago now. > although I myself have no idea what this is doing. (I should have pointed out it needs EXTENDED_GLOB for the "^".) First it replaces all the characters in all the elements of $MATCHES with a question mark, then orders them in the standard collating order with (o). As all the characters are the same, the shortest string comes first and [1] picks it. So $min is a single element array containing as many ?'s as there characters in the shortest string. The next match "simply" eliminates from MATCHES (':#') anything that doesn't match ('^') the pattern ${~min}, where the ~ turns the ?'s into active pattern characters. So this matches any element of MATCHES which has as many characters as the shortest string. In fact, that could be more than one, so you really need ${${MATCHES:#^${~min}}[1]} to select the first. -- Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/