From: Steve Talley <stephen.talley@sun.com>
To: Bart Schaefer <schaefer@candle.brasslantern.com>
Cc: zsh-users@sunsite.auc.dk
Subject: Re: Anonymous array indexing
Date: Wed, 13 Dec 2000 10:05:38 -0700 [thread overview]
Message-ID: <20001213100537.P11810@rmtc> (raw)
In-Reply-To: <1001213040610.ZM11053@candle.brasslantern.com>; from schaefer@candle.brasslantern.com on Wed, Dec 13, 2000 at 04:06:10AM +0000
Thanks Bart/Dan for the replies!
This _almost_ solves my problem, although I think I
oversimplified. The actual usage of this is in:
# Find a value for TERMINFO by looking for xterm package
delegates=(5.9 5.8.1 5.8 5.7 5.6)
terminfo=( $HOME/$^delegates/pkgs/xterm/lib/terminfo(/) )
export TERMINFO=$terminfo[1]
unset terminfo
Unfortunately this doesn't work well with your solution, ie.
export TERMINFO=${${=:-$HOME/$^delegates/pkgs/xterm/lib/terminfo(/)}[2]}
doesn't even come close to working. Any ideas for this slightly
more complicated example?
Thanks,
Steve
Bart Schaefer wrote:
> 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
next prev parent reply other threads:[~2000-12-13 17:06 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-12-13 1:41 Steve Talley
2000-12-13 4:04 ` Dan Nelson
2000-12-13 4:06 ` Bart Schaefer
2000-12-13 17:05 ` Steve Talley [this message]
2000-12-13 17:39 ` Bart Schaefer
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=20001213100537.P11810@rmtc \
--to=stephen.talley@sun.com \
--cc=schaefer@candle.brasslantern.com \
--cc=zsh-users@sunsite.auc.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).