The dreaded reply-to strikes again:
On 8/9/07, Paul Lalli <[EMAIL PROTECTED]> wrote: > > > $ perl -wle'my @bar = qw/alpha beta gamma/; @bar[()] = (1, 2, 3); > > > > > > ^^^^^^^ > > > > Can you explain this? The index is not intuitive to me. > > It's an empty array slice. A "normal" array slice would be something > like: > > @bar[2, 3, 6] = ('foo', 'bar', 'baz'); > > which assigns foo, bar, and baz to the third, fourth and seventh > elements of @bar (respectively). It's exactly the same as: > ($bar[2], $bar[3], $bar[6]) = ('foo', 'bar', 'baz'); > > That is, @bar[2, 3, 6] represents a list containing the third, fourth, > and seventh elements of @bar. > > Saying @bar[2] is a one-element array slice. It is a list containing > only a single element - the third element of @bar. > > @bar[()] is a zero-element array slice. It is a list containing > nothing. There is no point in ever using such a thing - I merely > posted it to illustrate my disagreement with John's assertion that a > slice requires more than one index. No. a single scalar subscript is, well, a scalar, not a single-element list. And it refers to an index, not a slice. The definition of a slice is more than one. from perldata: "A slice accesses several elements of a list, an array, or a hash simultaneously using a list of subscripts. It's more convenient than writing out the individual elements as a list of separate scalar values." Therefore: (qw/one two three four)[0]; # not a slice (qw/one two three four)[2,3]; # slice That you can create cases where you use a single-item list as an index doesn't not mean that all instances of using a scalar as an index are somehow special "single item" cases of slices. For that to be true, the subscript notation itself would have to force list context. It demonstrably doesn't. Consider: @bar = (1,2,3); @baz = (1,2,3,4,5); $foo = @[EMAIL PROTECTED]; ($foo) = @[EMAIL PROTECTED]; As you can see, the slice or index takes its context from the surrounding code, just as we would expect. The presence of square brackets doesn't somehow turn things that would otherwise be scalars into lists. HTH, -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org values of β will give rise to dom! -- -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org values of β will give rise to dom!