* local arrays
@ 1999-01-13 10:12 Phil Pennock
1999-01-13 16:15 ` Bart Schaefer
0 siblings, 1 reply; 6+ messages in thread
From: Phil Pennock @ 1999-01-13 10:12 UTC (permalink / raw)
To: Zsh Users
Is there a way in zsh-3.0.5 (or zsh-3.1.x) to use one command to declare
a variable to be both local and an array?
I can do
local foo; set -A foo a b c
which does create a local array, but can't see a way to do this
manually. And since there's no typeset for it ...
Thanks
--
--> Phil Pennock ; GAT d- s+:+ a23 C++(++++) UL++++/I+++/S+++/H+ P++@ L+++
E-@ W(+) N>++ o !K w--- O>+ M V !PS PE Y+ PGP+ t-- 5++ X+ R !tv b++>+++ DI+ D+
G+ e+ h* r y?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: local arrays
1999-01-13 10:12 local arrays Phil Pennock
@ 1999-01-13 16:15 ` Bart Schaefer
1999-01-13 16:58 ` Phil Pennock
0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 1999-01-13 16:15 UTC (permalink / raw)
To: Zsh Users
On Jan 13, 10:12am, Phil Pennock wrote:
} Subject: local arrays
}
} Is there a way in zsh-3.0.5 (or zsh-3.1.x) to use one command to declare
} a variable to be both local and an array?
No, there isn't. This is even documented, poorly (a bit better in pws-4,
because it was part of my rewrite of the `typeset' documentation).
Why do you need to do this? The variable will assume array-ness upon the
first array assignment to it.
--
Bart Schaefer Brass Lantern Enterprises
http://www.well.com/user/barts http://www.brasslantern.com
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: local arrays
1999-01-13 16:58 ` Phil Pennock
@ 1999-01-13 16:57 ` Peter Stephenson
0 siblings, 0 replies; 6+ messages in thread
From: Peter Stephenson @ 1999-01-13 16:57 UTC (permalink / raw)
To: Zsh Users
Phil Pennock wrote:
> The scripts I write are liable to be modified by others. So the purpose
> of stuff should be clear. Nice clean explicit statements of what a
> variable is help. So integers get declared as such, etc. And if that
> adds type-checking, all the better.
In that case, do
local arr
arr=()
wherever necessary. Now it even looks more like perl :-). But
type-checking's a bit of chimera here: there are many cases where
zsh will silently store a scalar into arr. It does get the behaviour
of a subsequent 'arr[1]=foo' right, however: for example,
% unset arr
% arr[1]=foo arr[2]=foo
% print $arr
foo foo
% unset arr
% local arr
% arr[1]=foo arr[2]=foo
% print $arr
ffooo
In the first case it guesses it has to be an array, in the second it's
already been created as a scalar. I suppose that's more or less what
you're worried about, in which case I recommend what I wrote above: I
sometimes do that myself.
--
Peter Stephenson <pws@ibmth.df.unipi.it> Tel: +39 050 844536
WWW: http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: local arrays
1999-01-13 16:15 ` Bart Schaefer
@ 1999-01-13 16:58 ` Phil Pennock
1999-01-13 16:57 ` Peter Stephenson
0 siblings, 1 reply; 6+ messages in thread
From: Phil Pennock @ 1999-01-13 16:58 UTC (permalink / raw)
To: Zsh Users
Typing away merrily, Bart Schaefer produced the immortal words:
> On Jan 13, 10:12am, Phil Pennock wrote:
> } Is there a way in zsh-3.0.5 (or zsh-3.1.x) to use one command to declare
> } a variable to be both local and an array?
>
> No, there isn't. This is even documented, poorly (a bit better in pws-4,
> because it was part of my rewrite of the `typeset' documentation).
>
> Why do you need to do this? The variable will assume array-ness upon the
> first array assignment to it.
Cleanliness. Obviousness of purpose.
The scripts I write are liable to be modified by others. So the purpose
of stuff should be clear. Nice clean explicit statements of what a
variable is help. So integers get declared as such, etc. And if that
adds type-checking, all the better.
I just prefer zsh to perl ...
--
--> Phil Pennock ; GAT d- s+:+ a23 C++(++++) UL++++/I+++/S+++/H+ P++@ L+++
E-@ W(+) N>++ o !K w--- O>+ M V !PS PE Y+ PGP+ t-- 5++ X+ R !tv b++>+++ DI+ D+
G+ e+ h* r y?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Local arrays
2013-02-06 15:41 Local arrays Atte Peltomäki
@ 2013-02-06 16:01 ` Peter Stephenson
0 siblings, 0 replies; 6+ messages in thread
From: Peter Stephenson @ 2013-02-06 16:01 UTC (permalink / raw)
To: zsh-users
On Wed, 06 Feb 2013 17:41:08 +0200
Atte Peltom채ki <atte.peltomaki@iki.fi> wrote:
> Can someone explain the following behaviour in declaring local arrays?
It's actually simpler than you suppose: you can't declare local arrays
in one go. An expresion like:
local -a foo=(one two three)
isn't an array assignment, because arguments to local are just like any
argument to any other command (to a first approximation). It's a
declaration with the argument "foo=(one two three)". Normally in zsh
that parenthesised expression at the end looks like a set of glob
qualifiers, i.e. a bit like "*(.)" or something like that. That's the
"unknown file attribute" error.
You need to split this up:
local -a foo
foo=(one two three)
pws
^ permalink raw reply [flat|nested] 6+ messages in thread
* Local arrays
@ 2013-02-06 15:41 Atte Peltomäki
2013-02-06 16:01 ` Peter Stephenson
0 siblings, 1 reply; 6+ messages in thread
From: Atte Peltomäki @ 2013-02-06 15:41 UTC (permalink / raw)
To: zsh-users
Hi,
Can someone explain the following behaviour in declaring local arrays?
% F () { local -a foo=(one two three); echo $foo ; }
% F
F: unknown file attribute
% F () { local foo=(one two); echo $foo ; }
% F
F: missing end of string
% F () { local foo=(moi hei); echo $foo ; }
% F
F: number expected
% F () { local -a foo; foo=(one two three); echo $foo ; }
% F
one two three
% echo $ZSH_VERSION
4.3.17
--
Atte Peltomäki
atte.peltomaki@iki.fi <> http://kameli.org
"Your effort to remain what you are is what limits you"
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2013-02-06 16:02 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-01-13 10:12 local arrays Phil Pennock
1999-01-13 16:15 ` Bart Schaefer
1999-01-13 16:58 ` Phil Pennock
1999-01-13 16:57 ` Peter Stephenson
2013-02-06 15:41 Local arrays Atte Peltomäki
2013-02-06 16:01 ` Peter Stephenson
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).