zsh-users
 help / color / mirror / code / Atom feed
* Assign to parameter in parameter -- opposite of ${(P)name}?
@ 2010-07-09 23:17 Benjamin R. Haskell
  2010-07-10  1:13 ` Frank Terbeck
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Benjamin R. Haskell @ 2010-07-09 23:17 UTC (permalink / raw)
  To: Zsh Users

It's the end of the week, and I'm tired, so I'm sure I'm completely 
overlooking something obvious, but how do you *assign* to a parameter 
whose name is in a parameter?

E.g., my latest guess:

name=foo
: ${${(P)name}::=something}
echo $foo
# should echo 'something'

Instead I get:
zsh: not an identifier:

Earlier attempt:
: ${${name}::=something}

Do I need to resort to 'eval'?

-- 
Best,
Ben


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Assign to parameter in parameter -- opposite of ${(P)name}?
  2010-07-09 23:17 Assign to parameter in parameter -- opposite of ${(P)name}? Benjamin R. Haskell
@ 2010-07-10  1:13 ` Frank Terbeck
  2010-07-10 15:36   ` Benjamin R. Haskell
  2010-07-10 14:57 ` Julius Plenz
  2010-07-10 17:39 ` Bart Schaefer
  2 siblings, 1 reply; 8+ messages in thread
From: Frank Terbeck @ 2010-07-10  1:13 UTC (permalink / raw)
  To: Benjamin R. Haskell; +Cc: Zsh Users

Benjamin R. Haskell wrote:
> It's the end of the week, and I'm tired, so I'm sure I'm completely 
> overlooking something obvious, but how do you *assign* to a parameter 
> whose name is in a parameter?
[...]
> Do I need to resort to 'eval'?

% typeset foobar=baz
% print ${foobar}
baz

Regards, Frank

-- 
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
                                                  -- RFC 1925


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Assign to parameter in parameter -- opposite of ${(P)name}?
  2010-07-09 23:17 Assign to parameter in parameter -- opposite of ${(P)name}? Benjamin R. Haskell
  2010-07-10  1:13 ` Frank Terbeck
@ 2010-07-10 14:57 ` Julius Plenz
  2010-07-10 15:52   ` Benjamin R. Haskell
  2010-07-10 17:39 ` Bart Schaefer
  2 siblings, 1 reply; 8+ messages in thread
From: Julius Plenz @ 2010-07-10 14:57 UTC (permalink / raw)
  To: zsh-users

Hi, Benjamin!

* Benjamin R. Haskell <zsh@benizi.com> [2010-07-10 01:35]:
> but how do you *assign* to a parameter whose name is in a parameter?

> name=foo
> : ${${(P)name}::=something}
> echo $foo
> # should echo 'something'

How about:

    zsh> name=foo
    zsh> typeset $name=something
    zsh> echo $foo
    something

Julius


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Assign to parameter in parameter -- opposite of ${(P)name}?
  2010-07-10  1:13 ` Frank Terbeck
@ 2010-07-10 15:36   ` Benjamin R. Haskell
  2010-07-10 15:44     ` Frank Terbeck
  0 siblings, 1 reply; 8+ messages in thread
From: Benjamin R. Haskell @ 2010-07-10 15:36 UTC (permalink / raw)
  To: Frank Terbeck; +Cc: Zsh Users

On Sat, 10 Jul 2010, Frank Terbeck wrote:

> Benjamin R. Haskell wrote:
> > It's the end of the week, and I'm tired, so I'm sure I'm completely 
> > overlooking something obvious, but how do you *assign* to a 
> > parameter whose name is in a parameter?
> [...]
> > Do I need to resort to 'eval'?
> 
> % typeset foobar=baz
> % print ${foobar}
> baz
> 

I was tired... but not thaaat tired... :-)

Using different variable names, I was looking for:

name=xyzzy
value=asdf

# <-- something that doesn't involve the string xyzzy

echo $xyzzy # echoes 'asdf'


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Assign to parameter in parameter -- opposite of ${(P)name}?
  2010-07-10 15:36   ` Benjamin R. Haskell
@ 2010-07-10 15:44     ` Frank Terbeck
  2010-07-10 15:54       ` Benjamin R. Haskell
  0 siblings, 1 reply; 8+ messages in thread
From: Frank Terbeck @ 2010-07-10 15:44 UTC (permalink / raw)
  To: Benjamin R. Haskell; +Cc: Zsh Users

Benjamin R. Haskell wrote:
> On Sat, 10 Jul 2010, Frank Terbeck wrote:
>> Benjamin R. Haskell wrote:
>> > It's the end of the week, and I'm tired, so I'm sure I'm completely 
>> > overlooking something obvious, but how do you *assign* to a 
>> > parameter whose name is in a parameter?
>> [...]
>> > Do I need to resort to 'eval'?
>> 
>> % typeset foobar=baz
>> % print ${foobar}
>> baz
>> 
>
> I was tired... but not thaaat tired... :-)
>
> Using different variable names, I was looking for:
>
> name=xyzzy
> value=asdf
>
> # <-- something that doesn't involve the string xyzzy
>
> echo $xyzzy # echoes 'asdf'

Yes. But the "foobar=baz" part is just a parameter to a builtin
command. Hence you can do this:

% name=foobar
% typeset ${name}=baz
% print $foobar
baz

I thought that was what you were after. If not, I must have
misunderstood the problem entirely. :)

Regards, Frank


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Assign to parameter in parameter -- opposite of ${(P)name}?
  2010-07-10 14:57 ` Julius Plenz
@ 2010-07-10 15:52   ` Benjamin R. Haskell
  0 siblings, 0 replies; 8+ messages in thread
From: Benjamin R. Haskell @ 2010-07-10 15:52 UTC (permalink / raw)
  To: zsh-users

On Sat, 10 Jul 2010, Julius Plenz wrote:

> Hi, Benjamin!
> 
> * Benjamin R. Haskell [2010-07-10 01:35]:
> > but how do you *assign* to a parameter whose name is in a parameter?
> 
> > name=foo
> > : ${${(P)name}::=something}
> > echo $foo
> > # should echo 'something'
> 
> How about:
> 
>     zsh> name=foo
>     zsh> typeset $name=something
>     zsh> echo $foo
>     something

Thanks.  That'll do.  I often need the flexibility of the ${name=value} 
and ${name:=value} constructs, but not in this case.

Also, I tended to shy away from defining things via typeset, because it 
echoes their values if already set.  I didn't realize until just now 
rereading the man page that TYPESET_SILENT exists.  And I didn't realize 
until just testing it that an explicit assignment disables the echo.  
The usual case where this bites me is when I try to localize a variable 
that I used outside a function.

-- 
Thanks,
Ben


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Assign to parameter in parameter -- opposite of ${(P)name}?
  2010-07-10 15:44     ` Frank Terbeck
@ 2010-07-10 15:54       ` Benjamin R. Haskell
  0 siblings, 0 replies; 8+ messages in thread
From: Benjamin R. Haskell @ 2010-07-10 15:54 UTC (permalink / raw)
  To: Frank Terbeck; +Cc: Zsh Users

On Sat, 10 Jul 2010, Frank Terbeck wrote:

> Benjamin R. Haskell wrote:
> > On Sat, 10 Jul 2010, Frank Terbeck wrote:
> >> Benjamin R. Haskell wrote:
> >> > It's the end of the week, and I'm tired, so I'm sure I'm 
> >> > completely overlooking something obvious, but how do you *assign* 
> >> > to a parameter whose name is in a parameter?
> >> [...]
> >> > Do I need to resort to 'eval'?
> >> 
> >> % typeset foobar=baz
> >> % print ${foobar}
> >> baz
> >> 
> >
> > I was tired... but not thaaat tired... :-)
> >
> > Using different variable names, I was looking for:
> >
> > name=xyzzy
> > value=asdf
> >
> > # <-- something that doesn't involve the string xyzzy
> >
> > echo $xyzzy # echoes 'asdf'
> 
> Yes. But the "foobar=baz" part is just a parameter to a builtin
> command. Hence you can do this:
> 
> % name=foobar
> % typeset ${name}=baz
> % print $foobar
> baz
> 
> I thought that was what you were after. If not, I must have
> misunderstood the problem entirely. :)

Ah, gotcha.  I hadn't seen Julius's reply yet, and didn't take the 
logical step on my own.

-- 
Thanks,
Ben


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Assign to parameter in parameter -- opposite of ${(P)name}?
  2010-07-09 23:17 Assign to parameter in parameter -- opposite of ${(P)name}? Benjamin R. Haskell
  2010-07-10  1:13 ` Frank Terbeck
  2010-07-10 14:57 ` Julius Plenz
@ 2010-07-10 17:39 ` Bart Schaefer
  2 siblings, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2010-07-10 17:39 UTC (permalink / raw)
  To: Benjamin R. Haskell; +Cc: Zsh Users

On Fri, Jul 9, 2010 at 4:17 PM, Benjamin R. Haskell <zsh@benizi.com> wrote:
> It's the end of the week, and I'm tired, so I'm sure I'm completely
> overlooking something obvious, but how do you *assign* to a parameter
> whose name is in a parameter?

Your first attempt wasn't complicated enough, but your second attempt
was too complicated. :-)

% name=foo
% : ${(P)name::=something}
% print $foo
something
% print $name
foo

The (P)name is interpreted before the ::= so you don't need nested expansion.


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2010-07-10 17:39 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-09 23:17 Assign to parameter in parameter -- opposite of ${(P)name}? Benjamin R. Haskell
2010-07-10  1:13 ` Frank Terbeck
2010-07-10 15:36   ` Benjamin R. Haskell
2010-07-10 15:44     ` Frank Terbeck
2010-07-10 15:54       ` Benjamin R. Haskell
2010-07-10 14:57 ` Julius Plenz
2010-07-10 15:52   ` Benjamin R. Haskell
2010-07-10 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).