Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: easy



On Sep 23,  8:59am, Ray Andrews wrote:
} Subject: easy
}
}      $ test () {  echo @[2,-1]; }

Presume you're missing a $ in $@ there ...

}      $ test one two three four five
}       two three four five
} 
} ... why is it that the leading number works intuitively but the second
} one is 'off by one' so to speak?

If you grasp that indexing from the left starts at one rather than
at zero, why is it not "intuitive" that indexing from the right also
starts at (negative) one rather than zero?

Note that there is no zero'th position in $* / $@ / $argv -- the use
of $0 as name of shell / script / function is just a convention.

(Unless you setopt ksharrays, but then $0 and ${argv[0]} are two very
different things, and even in ksh itself reverse index starts at -1.)

In any case mathematically +0 == -0 so if you think of them as array
positions those ought to refer to the same place (which in this case
is nothing, because there is no position zero).

Where you came up with thinking in terms of something being "removed
from the array" baffles me.  Nothing is "removed" from the original
array in either case; the indices identify what is *included* in the
result.

I suppose you're expecting ${argv[-1]} congruent with ${argv[$# - 1]}
and if ksharrays is set that works out to be true (but then $@[2] would
not mean what it does in your example, either).



Messages sorted by: Reverse Date, Date, Thread, Author