On Tue, May 09, 2006 at 06:43:22PM +0200, Axel Liljencrantz wrote:
> A problem with the set syntax has been nagging me for a long time. Set
> allows you to assign and erase slices of arrays using a convenient
> syntax like:
> 
> set foo[1 3 7] foo bar baz
> 
> in order to assign to the first, third and seventh element of the
> array foo, and with the same logic you can also use 'set -e foo[ 2 4]'
> to erase the second and fourth element of foo. This seems like it's
> all very clever, but unfortunatly it's actually a bit useless. The
> problem is that one rarely specifys the slicing indexes literally like
> that. What you'd want to do is something like
> 
> set foo[(seq 3 5)] foo bar baz

Since () should just run the inner command and leave the result in their
place, I don't see why this couldn't work.  You'd just need to modify
the seq call slightly so that the indices are separated by spaces
instead of newlines.

  set foo[(seq -s" " 3 5)] foo bar baz

On a side note,

  > echo foo{(seq 2)}
  foo1 foo2

already works as one would expect.  It becomes more problematic when you
add items outside the command expansion, but not overly so.  For
example,

  > echo foo{1,(seq -s, 2 3)}
  foo1 foo2 foo3

works as expected.  I'm actually quite surprised by the results of

  > echo foo{1,(seq 2 3)}
  foo1 foo2 foo1 foo3

as I see no reason why foo1 would be repeated between foo2 and foo3.

> or
> 
> set idx 1 3 7
> set -e foo[$idx]

Again, it seems like this should work since $idx should just be replaced
by the '1 3 7' and then the slicing would be performed.

> [snip]
> There are still a few gotchas left, though:
> 
> * Some people might expect e.g. 'set foo[1 (seq 4 6)] a b c d' to work
> and use the slice indexes '1 4 5 6', but it won't, since it will
> result in the slice indexes '1 4 1 5 1 6'. This is the right thing in
> my opinion, but it is a bit confusing at first.

I'd like to hear an explanation as to why that is correct as it makes no
sense to me.  I have no problem with expecting the user to use

  set foo[1 (seq -s" " 4 6)] a b c d

though since that would properly replicate manually typing the indices.

What it seems to come down to is that you 'just' need to change the
parsing algorithm so that operations are performed from the inner-most
'scope' outwards.  I think this would also behave more like users tend
to expect.

James

P.S. Sorry if I repeated some ideas from the other replies in the
thread, but I decided this one email would be easier than replying to
the various different emails I wanted to in the thread.
-- 
GPG Key: 1024D/61326D40 2003-09-02 James Vega <[EMAIL PROTECTED]>

Attachment: signature.asc
Description: Digital signature

Reply via email to