From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18338 invoked from network); 13 Dec 2000 04:07:04 -0000 Received: from sunsite.dk (HELO sunsite.auc.dk) (130.225.51.30) by ns1.primenet.com.au with SMTP; 13 Dec 2000 04:07:04 -0000 Received: (qmail 17509 invoked by alias); 13 Dec 2000 04:06:54 -0000 Mailing-List: contact zsh-users-help@sunsite.auc.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 3562 Received: (qmail 17502 invoked from network); 13 Dec 2000 04:06:52 -0000 From: "Bart Schaefer" Message-Id: <1001213040610.ZM11053@candle.brasslantern.com> Date: Wed, 13 Dec 2000 04:06:10 +0000 In-Reply-To: <20001212184156.I11810@rmtc> Comments: In reply to Steve Talley "Anonymous array indexing" (Dec 12, 6:41pm) References: <20001212184156.I11810@rmtc> X-Mailer: Z-Mail (5.0.0 30July97) To: zsh-users@sunsite.auc.dk Subject: Re: Anonymous array indexing MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii On Dec 12, 6:41pm, Steve Talley wrote: } } Is there a compact version of the following: } } fred=(one two three) } export FRED=$fred[2] } unset fred } } so that I can avoid having to use the temporary variable fred. The general trick is that the nameless parameter ${} always expands to the empty string, so ${:-value} always expands to "value". Then build up from there. } I am looking for something like } } export FRED=(one two three)[2] The value in ${:-value} always starts out as a scalar (a string), so you have to explicitly convert to an array before you can index it. If the individual words of the array do not contain spaces, you can combine word splitting ${=scalar} with ${:-value} to get: export FRED=${${=:-one two three}[2]} If the words will have embedded spaces, you'll have to resort to some other kind of splitting. E.g., ${(f)...} splits at newlines, so you can do: export FRED=${${(f):-$'one\ntwo has spaces\nthree'}[2]} This uses $'...' to turn \n into newlines, then splits on them. However, the $'...' syntax is not available in 3.0.x. In all versions you can instead split on some other character, e.g. on period: export FRED=${${(s(.)):-one.two has spaces.three}[2]} I don't think there is a fully general solution that works when you can't choose in advance a character on which to split. However, I'm guessing that the strings in the array are known in advance, and that it is the subscript that might vary, so you should be able to work something out. -- Bart Schaefer Brass Lantern Enterprises http://www.well.com/user/barts http://www.brasslantern.com Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net