in reply to Array/List Strangeness

Another two cases:

{ my @b; my @a = @b[1,0]; print Dumper(\@a); # $VAR1 = [ undef, undef ] } { my @b; my @a = (@b)[0]; print Dumper(\@a); # $VAR1 = [] }

The first makes sure that it is not about slices vs. non-slices

The second shows that it really is about lists vs. arrays. And also that it is an inconsistency that matters in real code (unlike the academic example ()[0] )

The idiom is somewhat useful: my @a= (@b)[0..99]; would create a new array of max size 100, but won't make the new array larger if @b has fewer than 100 elements. And since there is probably code out there using this I don't think this inconsistency can be changed

Replies are listed 'Best First'.
Re^2: Array/List Strangeness
by LanX (Saint) on Aug 05, 2009 at 17:29 UTC
    The idiom is somewhat useful: my @a= (@b)[0..99]; would create a new array of max size 100, but won't make the new array larger if @b has fewer than 100 elements. And since there is probably code out there using this I don't think this inconsistency can be changed

    I don't know what you mean, can't see any general difference between slicing @b or (@b), except when $#b==0!

    DB<38> @b=1..3 DB<39> x @b[0..6] 0 1 1 2 2 3 3 undef 4 undef 5 undef 6 undef DB<40> x (@b)[0..6] 0 1 1 2 2 3 3 undef 4 undef 5 undef 6 undef

    now 0-element array!

    DB<43> @b=() DB<44> x @b[0..6] 0 undef 1 undef 2 undef 3 undef 4 undef 5 undef 6 undef DB<45> x (@b)[0..6] empty array

    Cheers Rolf

      Look at the time the node was posted, it was when I still had hope ;-) and no indication of further inconsistent behaviour

      You have to make all of the elements referenced out-of-bounds. Compare @b[@b,1+@b] vs (@b)[@b,1+@b] (whether @b is empty or not).

      - tye