zsh-users
 help / color / mirror / code / Atom feed
* Reverse the order of an array?
@ 2002-02-08 22:35 Steve Talley
  2002-02-08 22:59 ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Steve Talley @ 2002-02-08 22:35 UTC (permalink / raw)
  To: zsh-users

Hi there,

How can I easily reverse the order of an array in zsh?  I was hoping
that something like

    osvers=($osvers[{$#osvers..1}])
    
would work, but no luck.

Any help would be greatly appreciated.

Thanks,

Steve


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

* Re: Reverse the order of an array?
  2002-02-08 22:35 Reverse the order of an array? Steve Talley
@ 2002-02-08 22:59 ` Bart Schaefer
  2002-02-08 23:55   ` Steve Talley
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2002-02-08 22:59 UTC (permalink / raw)
  To: Steve Talley; +Cc: zsh-users

On Fri, 8 Feb 2002, Steve Talley wrote:

> How can I easily reverse the order of an array in zsh?

Depends what you mean by "easily."

> I was hoping that something like
>
>     osvers=($osvers[{$#osvers..1}])
>
> would work, but no luck.

Array "slices" in zsh only have start and end points, and are always in
ascending order by numeric position in the array.  So you can't do this
purely with parameter expansion in the general case.

This works:

for ((i=$#osvers; i>0; --i)) { osvers[i*2]=($osvers[1]); shift osvers }


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

* Re: Reverse the order of an array?
  2002-02-08 22:59 ` Bart Schaefer
@ 2002-02-08 23:55   ` Steve Talley
  2002-02-09  0:09     ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Steve Talley @ 2002-02-08 23:55 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

Thanks Bart.  The best that I had come up with was:

    indexes=({$#osvers..1})
    tmp=('$osvers['$^indexes']')
    osvers=${(e)tmp}

Do you see any way (other than skipping the whole thing :) to simplify
this?

Thanks,

Steve

Bart Schaefer wrote:

> On Fri, 8 Feb 2002, Steve Talley wrote:
> 
> > How can I easily reverse the order of an array in zsh?
> 
> Depends what you mean by "easily."
> 
> > I was hoping that something like
> >
> >     osvers=($osvers[{$#osvers..1}])
> >
> > would work, but no luck.
> 
> Array "slices" in zsh only have start and end points, and are always in
> ascending order by numeric position in the array.  So you can't do this
> purely with parameter expansion in the general case.
> 
> This works:
> 
> for ((i=$#osvers; i>0; --i)) { osvers[i*2]=($osvers[1]); shift osvers }


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

* Re: Reverse the order of an array?
  2002-02-08 23:55   ` Steve Talley
@ 2002-02-09  0:09     ` Bart Schaefer
  2002-02-09  0:17       ` Steve Talley
  2002-02-11 13:26       ` Oliver Kiddle
  0 siblings, 2 replies; 7+ messages in thread
From: Bart Schaefer @ 2002-02-09  0:09 UTC (permalink / raw)
  To: Steve Talley; +Cc: zsh-users

On Fri, 8 Feb 2002, Steve Talley wrote:

> Thanks Bart.  The best that I had come up with was:
>
>     indexes=({$#osvers..1})
>     tmp=('$osvers['$^indexes']')
>     osvers=${(e)tmp}
>
> Do you see any way (other than skipping the whole thing :) to simplify
> this?

Hmm, I should have thought of that.

	eval osvers\=\( \"\$osvers\[{$#osvers..1}\]\" \)


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

* Re: Reverse the order of an array?
  2002-02-09  0:09     ` Bart Schaefer
@ 2002-02-09  0:17       ` Steve Talley
  2002-02-11 13:26       ` Oliver Kiddle
  1 sibling, 0 replies; 7+ messages in thread
From: Steve Talley @ 2002-02-09  0:17 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

Perfect!  I knew there was a better solution.

Thanks Bart!

Steve

Bart Schaefer wrote:

> On Fri, 8 Feb 2002, Steve Talley wrote:
> 
> > Thanks Bart.  The best that I had come up with was:
> >
> >     indexes=({$#osvers..1})
> >     tmp=('$osvers['$^indexes']')
> >     osvers=${(e)tmp}
> >
> > Do you see any way (other than skipping the whole thing :) to simplify
> > this?
> 
> Hmm, I should have thought of that.
> 
> 	eval osvers\=\( \"\$osvers\[{$#osvers..1}\]\" \)


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

* Re: Reverse the order of an array?
  2002-02-09  0:09     ` Bart Schaefer
  2002-02-09  0:17       ` Steve Talley
@ 2002-02-11 13:26       ` Oliver Kiddle
  2002-02-11 18:41         ` Bart Schaefer
  1 sibling, 1 reply; 7+ messages in thread
From: Oliver Kiddle @ 2002-02-11 13:26 UTC (permalink / raw)
  To: Bart Schaefer, Steve Talley; +Cc: zsh-users

 --- Bart Schaefer <schaefer@brasslantern.com> wrote: 
> 
> Hmm, I should have thought of that.
> 
> 	eval osvers\=\( \"\$osvers\[{$#osvers..1}\]\" \)
> 

Not very intuitive that is it.

I recently wanted to loop over an array's elements in reverse and
though I could do it with a for loop counting the indices backwards I
did think that there ought to be a better way.

Allowing array slices to go backwards, is a possibility though I'd not
be suprised if implementing it caused other things to break. The
easiest might be a parameter expension flag (r and R are gone so we'd
need a letter. ^ perhaps as we used that for the reversed prompt
state). Does anyone else have any views on whether we should add
something for this and if so how?

Oliver

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com


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

* Re: Reverse the order of an array?
  2002-02-11 13:26       ` Oliver Kiddle
@ 2002-02-11 18:41         ` Bart Schaefer
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2002-02-11 18:41 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Steve Talley, zsh-users

On Mon, 11 Feb 2002, Oliver Kiddle wrote:

> Allowing array slices to go backwards, is a possibility though I'd not
> be suprised if implementing it caused other things to break.

Right now, using a backwards array slice references an imaginary "empty
element" to the left of the left end of the slice; assigning to that
imaginary element makes it real.  E.g.:

zsh% x=(a b c d e)
zsh% x[4,2]=(y)
zsh% echo $x
a b c y d e

This was motivated by desiring to have a syntax for inserting elements
into the array; "forwards" array slices only allow for replacement.

> The easiest might be a parameter expension flag (r and R are gone so
> we'd need a letter.

We could also use a subscripting flag (though that doesn't help with r/R).

> ^ perhaps as we used that for the reversed prompt state).

No, I don't like that.  ${(^)^x} is just too confusing.

How about if (oa) means "sort in array index order" and (Oa) means "sort
in reverse array index order"?  There's precedent with (oi) and (Oi) for
case-insensitive sorting.  Obviously (oa) is equivalent to the default,
but so what?


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

end of thread, other threads:[~2002-02-11 18:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-08 22:35 Reverse the order of an array? Steve Talley
2002-02-08 22:59 ` Bart Schaefer
2002-02-08 23:55   ` Steve Talley
2002-02-09  0:09     ` Bart Schaefer
2002-02-09  0:17       ` Steve Talley
2002-02-11 13:26       ` Oliver Kiddle
2002-02-11 18:41         ` 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).