David Korn wrote: > > Is there a way to return a compound variable value from a function > > without dismanteling each single value first (which would be very > > painfull in the case when the compound variable has hundreds or > > thousands of members which itself are compound variables (see > > http://opensolaris.pastebin.ca/raw/683236 for an example)) ? > > > > I've tried the following example but it doesn't really work: > > -- snip -- > > function g > > { > > typeset x=(float x=1 y=2) > > print $x > > } > > > > typeset l=$(g) > > > > print "${l.x}" > > -- snip -- > > > > The variable "l.x" is empty in this case. > > There are two ways to do this: > > FIRST: > > function foo > { > typeset x=(typeset -lE x=1 y=2) > print -r "$x" > } > eval "l=$(g)" > > The eval is needed because ( and ) are only recognized by the parser. > The quotes are needed around "$x" to prevent expansions.
... but the problem still remains when I want to copy a compound variable with all members, e.g. $ (d=(typeset -lE x=5.5 y=6.7) ; eval "j=$(print -- "$d")" ; print ${j.x} ) # ... I guess in such a case the "eval" is unavoidable, right ? Another scenario may be object serialisation (where "object" means "compound variable", e.g. convert compound variable to a string, save string to a file and load it later again (or send the whole thing via network etc.)) ... and in all these cases the use of "eval" is... uhm... a bit scary... > Note that I used > typeset -lE > rather than float. For some reason (I guess a bug), the alias is > not being expanded so that this is being treated as an indexed > array rather than a compound assignment. > > x=(float x=1 y=2) > works, but > typeset x=(float x=1 y=2) > > I suspect that this is becasue aliases are only recognized for the > first word of a command. > > SECOND WAY (the recommended way) > > function g > { > nameref x=$1 > x=(float x=1 y=2) > } > g l *bing* ... I forgot that "nameref" can look into the local variables of the calling function... ... Thanks! :-) ---- Bye, Roland -- __ . . __ (o.\ \/ /.o) roland.mainz at nrubsig.org \__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer /O /==\ O\ TEL +49 641 7950090 (;O/ \/ \O;)