zsh-users
 help / color / mirror / code / Atom feed
* array prepend
@ 2013-01-11  6:58 sergio
  2013-01-12  1:38 ` Oliver Kiddle
  0 siblings, 1 reply; 7+ messages in thread
From: sergio @ 2013-01-11  6:58 UTC (permalink / raw)
  To: zsh-users

Hello.

Is there a way for prepend array like += for append,
or the only possible solution is a = (b $a)?

-- 
sergio.


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

* Re: array prepend
  2013-01-11  6:58 array prepend sergio
@ 2013-01-12  1:38 ` Oliver Kiddle
  2013-01-13 18:51   ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Oliver Kiddle @ 2013-01-12  1:38 UTC (permalink / raw)
  To: sergio; +Cc: zsh-users

serio  wrote:
> Is there a way for prepend array like += for append,
> or the only possible solution is a = (b $a)?

  a=(b $a)
is the only proper documented way to prepend elements

There are some hacks that do the job such as:
  a[-1-$#a]=(b)
I'm not sure whether I'd rely on that always working in the future,
however.

If you dig out the mailing list discussion from when += was added you'll
see that the main reason there isn't a prepend is that we couldn't think
of suitable syntax that would be backward compatible. -= for example
isn't possible because a - character is valid in an identifier.

Oliver


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

* Re: array prepend
  2013-01-12  1:38 ` Oliver Kiddle
@ 2013-01-13 18:51   ` Peter Stephenson
  2013-01-14  4:48     ` rahul
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2013-01-13 18:51 UTC (permalink / raw)
  To: Oliver Kiddle, zsh-users

On Sat, 12 Jan 2013 02:38:13 +0100
Oliver Kiddle <okiddle@yahoo.co.uk> wrote:
> If you dig out the mailing list discussion from when += was added you'll
> see that the main reason there isn't a prepend is that we couldn't think
> of suitable syntax that would be backward compatible. -= for example
> isn't possible because a - character is valid in an identifier.

I was thinking about /=.  I started looking at it once but got bored
with all the varieties of parameter type.

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


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

* Re: array prepend
  2013-01-13 18:51   ` Peter Stephenson
@ 2013-01-14  4:48     ` rahul
  2013-01-14 15:32       ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: rahul @ 2013-01-14  4:48 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Oliver Kiddle, zsh-users

[-- Attachment #1: Type: text/plain, Size: 866 bytes --]

On Mon, Jan 14, 2013 at 12:21 AM, Peter Stephenson <
p.w.stephenson@ntlworld.com> wrote:

>
> I was thinking about /=.  I started looking at it once but got bored
> with all the varieties of parameter type.
>
>
I was wondering what the way for inserting into an array is. Is the
following the simplest or most direct way:

    $FOO[1,2] xxx $FOO[3,-1]

I'd also like to know what the vision for zsh is. Are there any major
changes or enhancements coming up ? Is it to become more of a programming
language or only seen as a glue language.

I've been writing an app in zsh and totally loved the experience (as
against previous experiences with bash). (However, the only thing that
keeps biting me here and there are spaces in filenames for which I do use
the @F notation. I only wish that behaviour would be a default since the
syntax is very cumbersome)

-- 
 rahul

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

* Re: array prepend
  2013-01-14  4:48     ` rahul
@ 2013-01-14 15:32       ` Bart Schaefer
  2013-01-14 18:48         ` rahul
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2013-01-14 15:32 UTC (permalink / raw)
  To: zsh-users

On Jan 14, 10:18am, rahul wrote:
}
} I was wondering what the way for inserting into an array is. Is the
} following the simplest or most direct way:
} 
}     $FOO[1,2] xxx $FOO[3,-1]

Probably

	FOO[N]+=(xxx)

Note this is distinct from

	FOO[N]+=xxx

which will string-append xxx to the value stored at $FOO[N].

It'd be nice if this generalized to FOO[N-1]=(yyy) for prepend, but it
breaks at FOO[0] for backwards-compatibilty of $FOO[0] references and
also with the KSH_ARRAYS option where indexing is zero-based.  Also you
must take care with subtraction because of negative indices wrapping to
the end of the array.

} I'd also like to know what the vision for zsh is. Are there any major
} changes or enhancements coming up ? Is it to become more of a programming
} language or only seen as a glue language.

Well, neither really.  Zsh isn't a language, it's a shell.  It's meant
primarily to be used interactively; a lot of the features are present
only for the sake of making interactive use more powerful/customizable.

The language is closely tied to what's now standardized as the POSIX
shell language syntax and it's not going to stray too much farther from
that than it already has, if only because we're running out of syntax
that doesn't already have other semantics.

To the extent that POSIX shell language is a "glue language," that is
what zsh's language is likely to remain.

} I've been writing an app in zsh and totally loved the experience (as
} against previous experiences with bash). (However, the only thing that
} keeps biting me here and there are spaces in filenames for which I do
} use the @F notation. I only wish that behaviour would be a default
} since the syntax is very cumbersome)

Zsh's default behavior is to NOT split the string value of parameters
on spaces, so unless you set the SH_WORD_SPLIT option the need for @F
should be pretty small.  Of course it depends on what you're doing with
the file names, whether you're attempting to port a bash script, etc.


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

* Re: array prepend
  2013-01-14 15:32       ` Bart Schaefer
@ 2013-01-14 18:48         ` rahul
  2013-01-17  4:34           ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: rahul @ 2013-01-14 18:48 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 960 bytes --]

On Mon, Jan 14, 2013 at 9:02 PM, Bart Schaefer <schaefer@brasslantern.com>wrote:

> On Jan 14, 10:18am, rahul wrote:
> }
> } I was wondering what the way for inserting into an array is. Is the
> } following the simplest or most direct way:
> }
> }     $FOO[1,2] xxx $FOO[3,-1]
>
> Probably
>
>         FOO[N]+=(xxx)
>
> Note this is distinct from
>
>         FOO[N]+=xxx
>
> which will string-append xxx to the value stored at $FOO[N].
>

Wow! I was not going to ask this, since i have been asking too many
questions, but after reading your reply I can't resist asking this.

I was hoping to assign lists (arrays) to an associative array, and got the
error which is well documented on the internet. But no one has given any
workaround or alternative to this.

   FOO[a]=("a command" "some text" "etc etc")

zsh: FOO: attempt to set slice of associative array

Is the only alternative to do the following:

FOO_a=( a list)
FOO_b=(another list)

-- 
thx,  rahul

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

* Re: array prepend
  2013-01-14 18:48         ` rahul
@ 2013-01-17  4:34           ` Bart Schaefer
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2013-01-17  4:34 UTC (permalink / raw)
  To: zsh-users

On Jan 15, 12:18am, rahul wrote:
}
} I was hoping to assign lists (arrays) to an associative array, and got the
} error which is well documented on the internet. But no one has given any
} workaround or alternative to this.
} 
}    FOO[a]=("a command" "some text" "etc etc")
} 
} zsh: FOO: attempt to set slice of associative array

Right, that syntax means to take the element at [a] and splice the
words inside the parens into the array at that position, replacing
the value at [a].  You can't splice an associative array.

} Is the only alternative to do the following:
} 
} FOO_a=( a list)
} FOO_b=(another list)

It took me a while but I think I finally understand what you are asking
about here.  You want to implement a two-dimensional array.  This is not
directly supported in most shells (I don't know whether recent ksh has
come up with some kind of syntax for it).

The best you can do is to simulate the 2D array by placing a value at
the position in the "outer" array which represents the name of another
array that holds the "inner" array.

E.g. to assign an array to FOO[a] you need something like

    typeset -i _x=0
    typeset -A FOO

    set -A ${FOO[a]::=FOO$[++_x]} "a command" "some text" "etc etc"

Then ${(P)FOO[a]} will retrieve the array.  Of course, you also end up
needing to fiddle with a few other conventions as well, e.g., you must
do both

    unset $FOO[a]
    unset "FOO[a]"

to delete the "outer" element and its value.

This trick does not extend 2D associative arrays because ${(P)...}
cannot be told how to retrieve a named element of the "inner" hash;
it retrieves all the elements (in this case all the values in random
order) and returns them as an ordinary array with positional index.
I.e., ${(P)FOO[x][y]} is interpreted as ${${(@P)FOO[x]}[y]}.


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

end of thread, other threads:[~2013-01-17  4:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-11  6:58 array prepend sergio
2013-01-12  1:38 ` Oliver Kiddle
2013-01-13 18:51   ` Peter Stephenson
2013-01-14  4:48     ` rahul
2013-01-14 15:32       ` Bart Schaefer
2013-01-14 18:48         ` rahul
2013-01-17  4:34           ` 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).