zsh-users
 help / color / mirror / code / Atom feed
* help with dereferencing variables
@ 2004-02-14 17:59 S. Cowles
  2004-02-14 21:24 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: S. Cowles @ 2004-02-14 17:59 UTC (permalink / raw)
  To: zsh users


In the following code, method 1 gives the expected results.  Method 2, 
however, breaks with the error:  unknown file attribute.  Is the eval in 
Method 1 required, or is there a correct syntax to make the dereference occur 
in Method 2 without an eval?

(This is a code fragment of a routine that will assign array values to 
parameters.)

Thanks, scowles at earthlink dot net

#!/bin/zsh
# variable 1 initialize.
m1=
# variable 1 values.
m2="
	/ld1
	/ld2
"
# variable 2 initialize.
m3=
# variable 2 values.
m4="
	/pk1
	/pk2
"
# correspondence matrix
cm="
	m1 m2
	m3 m4
"
rows=${#${(f)cm}}
for (( row = 1 ; $row <= $rows ; row++ ))
do
	key=${${=${${(f)cm}[${row}]}}[1]}
	val=${${=${${(f)cm}[${row}]}}[2]}
	# method 1:
	b=$(echo "${key}=( ${(@)${(P)val}} )")
	eval $b
	echo "	>${key}<:  ${(P)${key}}"
	# method 2:
	${key}=( ${(@)${(P)${val}}} )
	echo "	>${key}<:  ${(P)${key}}"
done
exit 0



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

* Re: help with dereferencing variables
  2004-02-14 17:59 help with dereferencing variables S. Cowles
@ 2004-02-14 21:24 ` Bart Schaefer
  2004-02-15  4:12   ` S. Cowles
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2004-02-14 21:24 UTC (permalink / raw)
  To: zsh users

On Feb 14,  9:59am, S. Cowles wrote:
} 
} In the following code, method 1 gives the expected results. Method
} 2, however, breaks with the error: unknown file attribute. Is the
} eval in Method 1 required, or is there a correct syntax to make the
} dereference occur in Method 2 without an eval?

The short answers are "not precisely" and "no".

} 	# method 1:
} 	b=$(echo "${key}=( ${(@)${(P)val}} )")
} 	eval $b

You don't need the $(echo) and the assignment to $b here.  It should be
enough to do

	eval ${key}'=( ${(P)val} )'

(note placement of single quotes).

} 	# method 2:
} 	${key}=( ${(@)${(P)${val}}} )

This syntax is not an assignment, because the stuff to the left of the
equals sign is not an identifier name.  (This used to work in older
versions of zsh because the variable was expanded before zsh looked
for assignment syntax, but that was incompatible with other shells.)

Even if this were an assignment, The ${(@)...} is extraneous when the
whole thing is not in double quotes.  And you don't need ${(P)${val}},
just ${(P)val} is enough.


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

* Re: help with dereferencing variables
  2004-02-14 21:24 ` Bart Schaefer
@ 2004-02-15  4:12   ` S. Cowles
  2004-02-15 17:04     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: S. Cowles @ 2004-02-15  4:12 UTC (permalink / raw)
  To: zsh users


Many thanks; that was the info I was missing.  One small aesthetics preference 
following up on your suggestion and I'm away (does not preserve newlines in 
the array element assignments):

eval ${key}\=\( ${(P)val} \)

cheers.


On Saturday 14 February 2004 13:24, Bart Schaefer wrote:
> On Feb 14,  9:59am, S. Cowles wrote:
> }
> } In the following code, method 1 gives the expected results. Method
> } 2, however, breaks with the error: unknown file attribute. Is the
> } eval in Method 1 required, or is there a correct syntax to make the
> } dereference occur in Method 2 without an eval?
>
> The short answers are "not precisely" and "no".
>
> } 	# method 1:
> } 	b=$(echo "${key}=( ${(@)${(P)val}} )")
> } 	eval $b
>
> You don't need the $(echo) and the assignment to $b here.  It should be
> enough to do
>
> 	eval ${key}'=( ${(P)val} )'
>
> (note placement of single quotes).
>
> } 	# method 2:
> } 	${key}=( ${(@)${(P)${val}}} )
>
> This syntax is not an assignment, because the stuff to the left of the
> equals sign is not an identifier name.  (This used to work in older
> versions of zsh because the variable was expanded before zsh looked
> for assignment syntax, but that was incompatible with other shells.)
>
> Even if this were an assignment, The ${(@)...} is extraneous when the
> whole thing is not in double quotes.  And you don't need ${(P)${val}},
> just ${(P)val} is enough.




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

* Re: help with dereferencing variables
  2004-02-15  4:12   ` S. Cowles
@ 2004-02-15 17:04     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2004-02-15 17:04 UTC (permalink / raw)
  To: zsh users

On Feb 14,  8:12pm, S. Cowles wrote:
} 
} Many thanks; that was the info I was missing. One small aesthetics
} preference following up on your suggestion and I'm away (does not
} preserve newlines in the array element assignments):
}
} eval ${key}\=\( ${(P)val} \)

This is not equivalent to my suggestion nor to your "method 2".  It's
a bit closer to your "method 1".  In the above formulation, ${(P)val}
is expanded _before_ being eval'd.  That means that spaces and quotes
and other shell syntax in the expansion are re-parsed.

Compare:

key=tmp
val=row
row='this is a row that may );( print oops in a subshell'
eval ${key}\=\( ${(P)val} \)
print "$tmp"
eval ${key}'=( ${(P)val} )'
print "$tmp"


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

end of thread, other threads:[~2004-02-15 17:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-14 17:59 help with dereferencing variables S. Cowles
2004-02-14 21:24 ` Bart Schaefer
2004-02-15  4:12   ` S. Cowles
2004-02-15 17:04     ` 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).