in reply to Re^2: Array/List Strangeness (why)
in thread Array/List Strangeness

Talking about bull ;-)

BTW, if you want to avoid this particular bit of magic, you can rewrite your list slice as an array slice: [get_a_list()]->[7,0]. And, as quietly noted in the documentation, if you want this behavior for an array slice, just write it as a list slice: (@array)[7,0].

I am pretty sure that slicing while dereferencing with right-arrow isn't (yet) implemented in perl!

DB<71> x [qw/a b c/]->[2] # works like expected 0 'c' DB<72> x [qw/a b c/]->[1,0] # oops! 0 'a' DB<73> x [qw/a b c/]->[0,1] 0 'b'

Seems that instead to try slicing, the comma is interpreted in scalar context. Hence the last element of the slice-list is chosen as index!

UPDATE: the only way I know to slice lists like arrays is:

DB<83> x @{[ qw// ]}[0,1] 0 undef 1 undef DB<84> x @{[ qw/a b c/ ]}[0,1] 0 'a' 1 'b'

Cheers Rolf

Replies are listed 'Best First'.
Re^4: Array/List Strangeness (why)
by tye (Sage) on Aug 05, 2009 at 18:14 UTC

    Sorry, multi-value slices require more complex syntax: @{[1,2,3]}[8,1,9]. The -> only works when you just want one value back. The @{ } form isn't actually much more verbose, but it is rather uglier. There was a patch to allow [...]->@[...] syntax (and I believe there is even a fork of Perl5 that includes such a patch) but it never got applied to official Perl 5 (I don't think I ever saw a good reason given for why it shouldn't be applied, though).

    - tye        

      multi-value slices require more complex syntax

      which is really a pity since it breaks the "always dereference with arrow" rule in PBP.

      I don't think I ever saw a good reason given for why it shouldn't be applied, though

      "Mythical" backwards compability? ;-)

      Personally i'd like to have a pragma like strict that bans the "scalar comma operator" ... I have much more trouble than fun with it ... 8(

      Cheers Rolf