* Build a variable name from another variable?
@ 2004-01-23 22:42 Omen Wild
2004-01-23 23:19 ` Philippe Troin
2004-01-23 23:47 ` Cosmo
0 siblings, 2 replies; 5+ messages in thread
From: Omen Wild @ 2004-01-23 22:42 UTC (permalink / raw)
To: zsh users
[-- Attachment #1: Type: text/plain, Size: 627 bytes --]
I am not totally sure what it is called, but I want to build a variable
name dynamically, from the contents of another variable. That is, I
have:
----- Begin script -----
barcontents="bar contents"
baz="bar"
echo $barcontents # <---- effect I want
echo ${${baz}contents} # <---- how I want to get there
----- End script -----
I get this error: bad substitution
I think the answer involves the (P) flag, as in:
echo ${${(P)baz}contents}
But that dies with the same error.
I am running Debian unstable, zsh version 4.0.9.
Thanks,
Omen
--
I used up all my sick days, so I'm calling in dead.
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Build a variable name from another variable?
2004-01-23 22:42 Build a variable name from another variable? Omen Wild
@ 2004-01-23 23:19 ` Philippe Troin
2004-01-24 0:51 ` Omen Wild
2004-01-23 23:47 ` Cosmo
1 sibling, 1 reply; 5+ messages in thread
From: Philippe Troin @ 2004-01-23 23:19 UTC (permalink / raw)
To: Omen Wild; +Cc: zsh users
Omen Wild <Omen.Wild@Dartmouth.EDU> writes:
> I am not totally sure what it is called, but I want to build a variable
> name dynamically, from the contents of another variable. That is, I
> have:
>
> ----- Begin script -----
> barcontents="bar contents"
> baz="bar"
>
> echo $barcontents # <---- effect I want
> echo ${${baz}contents} # <---- how I want to get there
> ----- End script -----
% t="${baz}contents"
% echo ${${(P)t}}
bar contents
Works...
i could not manage to do it without an intermediate variable.
Phil.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Build a variable name from another variable?
2004-01-23 22:42 Build a variable name from another variable? Omen Wild
2004-01-23 23:19 ` Philippe Troin
@ 2004-01-23 23:47 ` Cosmo
1 sibling, 0 replies; 5+ messages in thread
From: Cosmo @ 2004-01-23 23:47 UTC (permalink / raw)
To: Omen Wild; +Cc: zsh users
Omen Wild wrote:
>
>
>
> I am not totally sure what it is called, but I want to build a variable
> name dynamically, from the contents of another variable. That is, I
> have:
>
> ----- Begin script -----
> barcontents="bar contents"
> baz="bar"
>
> echo $barcontents # <---- effect I want
> echo ${${baz}contents} # <---- how I want to get there
> ----- End script -----
>
> I get this error: bad substitution
>
> I think the answer involves the (P) flag, as in:
> echo ${${(P)baz}contents}
Back in the old days, before these fancy complicated shells, I'd just do this:
eval echo \$${baz}contents
;-)
Cosmo
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Build a variable name from another variable?
2004-01-23 23:19 ` Philippe Troin
@ 2004-01-24 0:51 ` Omen Wild
2004-01-24 2:36 ` Bart Schaefer
0 siblings, 1 reply; 5+ messages in thread
From: Omen Wild @ 2004-01-24 0:51 UTC (permalink / raw)
To: zsh users
[-- Attachment #1: Type: text/plain, Size: 592 bytes --]
Quoting Philippe Troin <phil@fifi.org> on Fri, Jan 23 15:19:
>
> % t="${baz}contents"
> % echo ${${(P)t}}
> bar contents
>
> Works...
> i could not manage to do it without an intermediate variable.
Ah, this will probably work for me.
Quoting Cosmo <cosmo@uk.ibm.com> on Fri, Jan 23 23:47:
>
> Back in the old days, before these fancy complicated shells, I'd just do
> this:
I simplified a bit for the example, I'm actually trying to do stuff
much more complicated than just a echo...
Thanks,
Omen
--
Where do forest rangers go to "get away from it all"?
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Build a variable name from another variable?
2004-01-24 0:51 ` Omen Wild
@ 2004-01-24 2:36 ` Bart Schaefer
0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2004-01-24 2:36 UTC (permalink / raw)
To: zsh users
On Jan 23, 7:51pm, Omen Wild wrote:
}
} Quoting Philippe Troin <phil@fifi.org> on Fri, Jan 23 15:19:
} >
} > % t="${baz}contents"
} > % echo ${${(P)t}}
} > bar contents
} >
} > Works...
} > i could not manage to do it without an intermediate variable.
}
} Ah, this will probably work for me.
Here it is without the intermediate variable:
${(P)${:-${baz}contents}}
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2004-01-24 2:37 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-23 22:42 Build a variable name from another variable? Omen Wild
2004-01-23 23:19 ` Philippe Troin
2004-01-24 0:51 ` Omen Wild
2004-01-24 2:36 ` Bart Schaefer
2004-01-23 23:47 ` Cosmo
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).