zsh-users
 help / color / mirror / code / Atom feed
* Exporting arrays
@ 1998-01-11 18:16 Adam Spiers
  1998-01-12  3:51 ` Geoff Wing
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Adam Spiers @ 1998-01-11 18:16 UTC (permalink / raw)
  To: zsh users mailing list

% foo=(a b c)
% typeset | grep zzz
array foo=(a b c)
% export foo=(a b c)
% typeset | grep zzz 
array exported foo=(a b c)

However,

% unset foo
% export foo=(a b c)
% typeset | grep zzz 
exported foo='(a b c)'

Presumably there's a decent reason for this behaviour; would
someone be good enough to explain it to me?


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

* Re: Exporting arrays
  1998-01-11 18:16 Exporting arrays Adam Spiers
@ 1998-01-12  3:51 ` Geoff Wing
  1998-01-12  9:01 ` Peter Stephenson
  1998-01-12 10:45 ` Andrew Main
  2 siblings, 0 replies; 11+ messages in thread
From: Geoff Wing @ 1998-01-12  3:51 UTC (permalink / raw)
  To: zsh-users

Adam Spiers <adam@thelonious.new.ox.ac.uk> typed:
:% foo=(a b c)
:% typeset | grep zzz
:array foo=(a b c)
:% export foo=(a b c)
:% typeset | grep zzz 
:array exported foo=(a b c)
:However,
:
:% unset foo
:% export foo=(a b c)
:% typeset | grep zzz 
:exported foo='(a b c)'
:Presumably there's a decent reason for this behaviour; would
:someone be good enough to explain it to me?

One important thing to note is that there is no defined method for exporting
arrays.  You can only export text strings.  In the first case, even though it 
says 'array exported foo=(a b c)' it won't be exported as anything, though
it will be used in the current shell as an array.  In the second, well, 
you haven't previously told it that foo is an array but you've said you
want to export it, so it'll make it something it can export, ie. a string.
-- 
Geoff Wing [gcw@pobox.com]                         Phone    : +61-3-9818 2977
 Technical Manager: PrimeNet Computer Consultants  Facsimile: +61-3-9818 5155
 Work URL: http://www.primenet.com.au/             Mobile   : 0412 162 441
 Ego  URL: http://pobox.com/~gcw/


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

* Re: Exporting arrays
  1998-01-11 18:16 Exporting arrays Adam Spiers
  1998-01-12  3:51 ` Geoff Wing
@ 1998-01-12  9:01 ` Peter Stephenson
  1998-01-12 10:45 ` Andrew Main
  2 siblings, 0 replies; 11+ messages in thread
From: Peter Stephenson @ 1998-01-12  9:01 UTC (permalink / raw)
  To: Zsh users list

Adam Spiers wrote:
> % unset foo
> % export foo=(a b c)
> % typeset | grep zzz 
> exported foo='(a b c)'
> 
> Presumably there's a decent reason for this behaviour; would
> someone be good enough to explain it to me?

Geoff explained most of it, but didn't point out that in fact the export
command doesn't create arrays at all, because there's no special syntax
for it:  it just treats "foo=(a b c)" as a string.  (At least, I don't
think he pointed that out.  I've just got back am reading through my
mail rather rapidly.  I've got about 100 different ways of becoming a
millionaire in a month.  This is worse than Reader's Digest.)

-- 
Peter Stephenson <pws@ifh.de>       Tel: +39 50 911239
WWW:  http://www.ifh.de/~pws/
Gruppo Teorico, Dipartimento di Fisica
Piazza Torricelli 2, 56100 Pisa, Italy


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

* Re: Exporting arrays
  1998-01-11 18:16 Exporting arrays Adam Spiers
  1998-01-12  3:51 ` Geoff Wing
  1998-01-12  9:01 ` Peter Stephenson
@ 1998-01-12 10:45 ` Andrew Main
  1998-01-12 13:48   ` Bernd Eggink
  2 siblings, 1 reply; 11+ messages in thread
From: Andrew Main @ 1998-01-12 10:45 UTC (permalink / raw)
  To: adam.spiers; +Cc: zsh-users

Adam Spiers wrote:
>% foo=(a b c)
>% typeset | grep zzz
>array foo=(a b c)
>% export foo=(a b c)
>% typeset | grep zzz 
>array exported foo=(a b c)
>
>However,
>
>% unset foo
>% export foo=(a b c)
>% typeset | grep zzz 
>exported foo='(a b c)'

This sort of thing has cropped up before, and it has always been due to
human error.  </hal>

In the case of "export foo=(a b c)", the parens do not actually delimit
an array.  They are treated as globbing metacharacters.  This command runs
the builtin "export", with argument "foo=(a b c)", which it interprets as
a request to export the variable "foo" and set its value to the scalar
"(a b c)".  In the second of your dialogues, that's precisely what
happens.  In the first dialogue, you first set foo to be an array,
and the export command refused to change it to a scalar.

Btw, it's impossible to export an array.  Unix environment variables are
only strings.  There are ways an array could be encoded as a string,
but some other shells get very unhappy about apparently malformed
environment entries.

-zefram


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

* Re: Exporting arrays
  1998-01-12 10:45 ` Andrew Main
@ 1998-01-12 13:48   ` Bernd Eggink
  1998-01-12 15:15     ` Andrew Main
  0 siblings, 1 reply; 11+ messages in thread
From: Bernd Eggink @ 1998-01-12 13:48 UTC (permalink / raw)
  To: Andrew Main; +Cc: adam.spiers, zsh-users

Andrew Main wrote:

[ snip ]

> Btw, it's impossible to export an array.  Unix environment variables are
> only strings.  There are ways an array could be encoded as a string,
> but some other shells get very unhappy about apparently malformed
> environment entries.
> 
> -zefram

As a workaround, you could convert the array into a word list, using a
suited delimiter:

   a=(one two three four)
   export aex=${(j(:))a}

Then, in the subshell, convert the list back into an array:

   a=(${(s(:))aex})

Unfortunately this doesn't work if an array element is empty! Example:

   a=(one '' three four)
   export aex=${(j(:))a}   # aex=one::three:four
   # ...
   a=(${(s(:))aex})        # a=(one three four), $#a == 3

I reported this (IMHO) bug already some weeks ago. Has anybody tried to
fix it? I wasn't able yet to localize it in the sources.

	Bernd
   



--
Bernd Eggink
Regionales Rechenzentrum der Uni Hamburg
eggink@rrz.uni-hamburg.de
http://www.rrz.uni-hamburg.de/eggink/BEggink.html


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

* Re: Exporting arrays
  1998-01-12 13:48   ` Bernd Eggink
@ 1998-01-12 15:15     ` Andrew Main
  1998-01-12 21:24       ` Bernd Eggink
  1998-01-12 22:12       ` Adam Spiers
  0 siblings, 2 replies; 11+ messages in thread
From: Andrew Main @ 1998-01-12 15:15 UTC (permalink / raw)
  To: Bernd Eggink; +Cc: zefram, adam.spiers, zsh-users

Bernd Eggink wrote:
>   a=(${(s(:))aex})
>
>Unfortunately this doesn't work if an array element is empty! Example:

Both behaviours are useful.  What would really be nice would be to
make the delimiter a glob pattern, so that both behaviours (and more)
are available.  That would make the default behave the way you want it,
which is probably better overall, because if one really wanted the other
form of splitting then this would become immediately obvious.

-zefram


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

* Re: Exporting arrays
  1998-01-12 15:15     ` Andrew Main
@ 1998-01-12 21:24       ` Bernd Eggink
  1998-01-12 22:12       ` Adam Spiers
  1 sibling, 0 replies; 11+ messages in thread
From: Bernd Eggink @ 1998-01-12 21:24 UTC (permalink / raw)
  To: zsh mailing list; +Cc: zefram

Zefram wrote:
> 
> Bernd Eggink wrote:
> >   a=(${(s(:))aex})
> >
> >Unfortunately this doesn't work if an array element is empty! Example:
> 
> Both behaviours are useful.  What would really be nice would be to
> make the delimiter a glob pattern, so that both behaviours (and more)
> are available.  

Hm, for me just the two of them would be enough...

> That would make the default behave the way you want it,
> which is probably better overall, because if one really wanted the other
> form of splitting then this would become immediately obvious.

Why not simply supply a separate flag? At present, things are really
confusing: the 's' flags works this way, option SH_WORD_SPLIT the other
way. And, to deepen confusion, the documentation of the 's' flag refers
to option SH_WORD_SPLIT as if both were equivalent!

	Bernd
-- 
Bernd Eggink
Regionales Rechenzentrum der Universitaet Hamburg
eggink@rrz.uni-hamburg.de
http://www.rrz.uni-hamburg.de/eggink/BEggink.html


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

* Re: Exporting arrays
  1998-01-12 15:15     ` Andrew Main
  1998-01-12 21:24       ` Bernd Eggink
@ 1998-01-12 22:12       ` Adam Spiers
  1998-01-12 23:15         ` Mirar
  1 sibling, 1 reply; 11+ messages in thread
From: Adam Spiers @ 1998-01-12 22:12 UTC (permalink / raw)
  To: zsh-users

Andrew Main (zefram@tao.co.uk) wrote:
> Bernd Eggink wrote:
> >   a=(${(s(:))aex})
> >
> >Unfortunately this doesn't work if an array element is empty! Example:
> 
> Both behaviours are useful.  What would really be nice would be to
> make the delimiter a glob pattern, so that both behaviours (and more)
> are available.  That would make the default behave the way you want it,
> which is probably better overall, because if one really wanted the other
> form of splitting then this would become immediately obvious.

Many thanks to all who replied.  If I had had my brain switched
on at the time I would have realised that environment variables
cannot be arrays; I even missed the rather obvious hint that
some shells don't have arrays at all!  Doh.

Anyway, while we're on the topic of making tiny nooks and
crannies of zsh into potential glob patterns, can I ask what's
the deal with the pattern in

    compctl ... -x C[offset,pattern] ...
                       ^^^^^^^

and also for W[] and R[]?

Is that glob patterns also, or something else?  I ask, because I
can't get it working.  I want a condition like C[0,abcd????]
i.e. only completes when the current word is abcd followed by
four other characters (preferably digits only, if that was
possible).  When I try that compctl, it offers no completions at
all :-(

An another related note, can there be some provision in globbing
for the equivalents of the regexps 

    x{n}
    x{n,}
    x{,m}
    x{n,m}

for non-trivial n and m?  At the moment it seems you can only
specify none, one or many.

By the way, if anyone's interested, I'm slowly making
progressing with a patch that implements dabbrev-expand (see my
post on Jan 2nd).  However I'm a complete novice at hacking the
internals of shells, so I'm not sure whether the end product
would be worthwhile even if it worked (although I think the
patch required should be pretty small so maybe there's not too
much scope for error).  Should I join the zsh-workers list?


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

* Re: Exporting arrays
  1998-01-12 22:12       ` Adam Spiers
@ 1998-01-12 23:15         ` Mirar
  1998-01-13  1:39           ` Adam Spiers
  0 siblings, 1 reply; 11+ messages in thread
From: Mirar @ 1998-01-12 23:15 UTC (permalink / raw)
  To: Adam Spiers; +Cc: zsh-users

> Is that glob patterns also, or something else?  I ask, because I
> can't get it working.  I want a condition like C[0,abcd????]

I got this line to work, at least:

compctl -k '(foobar)' -x C[-1,foo???] -k '(foo bar)' - -- foo 

/Mirar

__________________________________________________________________________
Idonex AB               Telefon      Telefax      nalle
Skolgatan 10		013-376814   013-376801   0708-376867
582 34 Linköping        mirar@idonex.se           http://www.idonex.se/


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

* Re: Exporting arrays
  1998-01-12 23:15         ` Mirar
@ 1998-01-13  1:39           ` Adam Spiers
  1998-01-13 14:12             ` Mirar
  0 siblings, 1 reply; 11+ messages in thread
From: Adam Spiers @ 1998-01-13  1:39 UTC (permalink / raw)
  To: zsh users mailing list

Mirar (mirar@idonex.se) wrote:
> > Is that glob patterns also, or something else?  I ask, because I
> > can't get it working.  I want a condition like C[0,abcd????]
> 
> I got this line to work, at least:
> 
> compctl -k '(foobar)' -x C[-1,foo???] -k '(foo bar)' - -- foo 

That provided enough of a clue for me to realise what I was
doing wrong :-)

Essentially I wanted it to add a suffix if the prefix matched
foo???? but had forgotten that the prefix was counted as part of
the completion and so any valid completions would have to
begin with this prefix.  Hence I needed a dummy completion which
always completed successfully by adding an empty string, and
found one in -K compctl_dummy, where

compctl_dummy () {
              reply=($1)
}

Is there a better way of doing this?  Should there not be the
flexibility of choosing whether or not a specified string or
pattern to match is part of the completion?


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

* Re: Exporting arrays
  1998-01-13  1:39           ` Adam Spiers
@ 1998-01-13 14:12             ` Mirar
  0 siblings, 0 replies; 11+ messages in thread
From: Mirar @ 1998-01-13 14:12 UTC (permalink / raw)
  To: Adam Spiers; +Cc: zsh users mailing list

> That provided enough of a clue for me to realise what I was
> doing wrong :-)
> 
> Essentially I wanted it to add a suffix if the prefix matched
> foo???? but had forgotten that the prefix was counted as part of
> the completion and so any valid completions would have to
> begin with this prefix. 

Could this (n[...]) be of any help?

compctl -k hosts \
	-x "n[1,-class=]" -k "(any in chaos hesiod)" \
	- "n[1,-query=]" -k "(a cname hinfo md mx mb mg minfo ns ptr soa txt uinfo wks any)" \
	-- + -k "(-query= -all\  -class= -d2\  -nod2\  -debug\  -nodebug\  -defname\  -nodefname\  -domain= -ignoretc\  -noignoretc\ )" -Q -S '' \
	nslookup

/Mirar
__________________________________________________________________________
Idonex AB               Telefon      Telefax      nalle
Skolgatan 10		013-376814   013-376801   0708-376867
582 34 Linköping        mirar@idonex.se           http://www.idonex.se/


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

end of thread, other threads:[~1998-01-13 14:20 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-01-11 18:16 Exporting arrays Adam Spiers
1998-01-12  3:51 ` Geoff Wing
1998-01-12  9:01 ` Peter Stephenson
1998-01-12 10:45 ` Andrew Main
1998-01-12 13:48   ` Bernd Eggink
1998-01-12 15:15     ` Andrew Main
1998-01-12 21:24       ` Bernd Eggink
1998-01-12 22:12       ` Adam Spiers
1998-01-12 23:15         ` Mirar
1998-01-13  1:39           ` Adam Spiers
1998-01-13 14:12             ` Mirar

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).