* Anonymous array indexing
@ 2000-12-13 1:41 Steve Talley
2000-12-13 4:04 ` Dan Nelson
2000-12-13 4:06 ` Bart Schaefer
0 siblings, 2 replies; 5+ messages in thread
From: Steve Talley @ 2000-12-13 1:41 UTC (permalink / raw)
To: zsh-users
Hello,
Is there a compact version of the following:
fred=(one two three)
export FRED=$fred[2]
unset fred
I am looking for something like
export FRED=(one two three)[2]
so that I can avoid having to use the temporary variable fred.
The above doesn't work. Is there something equivalently short I
can use? I'm running zsh 3.1.9.
Thanks,
Steve
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Anonymous array indexing
2000-12-13 1:41 Anonymous array indexing Steve Talley
@ 2000-12-13 4:04 ` Dan Nelson
2000-12-13 4:06 ` Bart Schaefer
1 sibling, 0 replies; 5+ messages in thread
From: Dan Nelson @ 2000-12-13 4:04 UTC (permalink / raw)
To: Steve Talley; +Cc: zsh-users
In the last episode (Dec 12), Steve Talley said:
> Is there a compact version of the following:
>
> fred=(one two three)
> export FRED=$fred[2]
> unset fred
>
> I am looking for something like
>
> export FRED=(one two three)[2]
Assuming "one two three" is actually stored in a shell variable (we'll
call it a):
export FRED=${${=a}[2]}
will work. = turns on SH_WORD_SPLIT, essentially turning the variable
into an array temporarily.
--
Dan Nelson
dnelson@emsphone.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Anonymous array indexing
2000-12-13 1:41 Anonymous array indexing Steve Talley
2000-12-13 4:04 ` Dan Nelson
@ 2000-12-13 4:06 ` Bart Schaefer
2000-12-13 17:05 ` Steve Talley
1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2000-12-13 4:06 UTC (permalink / raw)
To: zsh-users
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Anonymous array indexing
2000-12-13 4:06 ` Bart Schaefer
@ 2000-12-13 17:05 ` Steve Talley
2000-12-13 17:39 ` Bart Schaefer
0 siblings, 1 reply; 5+ messages in thread
From: Steve Talley @ 2000-12-13 17:05 UTC (permalink / raw)
To: Bart Schaefer; +Cc: zsh-users
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Anonymous array indexing
2000-12-13 17:05 ` Steve Talley
@ 2000-12-13 17:39 ` Bart Schaefer
0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2000-12-13 17:39 UTC (permalink / raw)
To: Steve Talley; +Cc: zsh-users
On Dec 13, 10:05am, Steve Talley wrote:
} Subject: Re: Anonymous array indexing
}
} # 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]}
You've got two problems. One is that you're trying a file glob, which
isn't going to work in the same shell-word with TERMINFO= (unless you
resort to using `setopt globassign'). Two is that =:- is in the wrong
place.
For the second problem, you'd want
$HOME/${^${=:-5.9 5.8.1 5.8 5.7 5.6}}/pkgs/xterm/lib/terminfo(/)
You can get around the glob problem by using $(print ...), but I don't
know why avoiding a temp parameter would be important enough to fork a
new process.
export TERMINFO=${$(print \
$HOME/${^${=:-5.9 5.8.1 5.8 5.7 5.6}}/pkgs/xterm/lib/terminfo(/))[1]}
--
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2000-12-13 17:39 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-13 1:41 Anonymous array indexing Steve Talley
2000-12-13 4:04 ` Dan Nelson
2000-12-13 4:06 ` Bart Schaefer
2000-12-13 17:05 ` Steve Talley
2000-12-13 17:39 ` Bart Schaefer
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).