Perl array splicing syntax -


why third print fail?

my @a = (0,1,2,3);  print @a[-@a..-2];  # works print @a[0..2];     # works print @a[0..-2];    # prints nothing  

it's not clear me meaning of -@a used in @a[-@a..-2] statement. special syntax? special syntax provide (if @ all) in addition $#a instance? sort of sugar (which curious shorter single character) symbol array used in subindex means "length of array"?

the 3rd print prints nothing because, according perldoc perlop:

if left value greater right value returns empty list.

see perldoc b::deparse:

$ perl -mo=deparse code.pl my(@a) = (0, 1, 2, 3); print @a[-@a .. -2]; print @a[0..2]; print @a[()]; 

i believe print @a[-@a..-2]; "works" because @a[-4 .. -2]. according perldoc perldata:

a negative subscript retrieves value end.

so, 3 @ index -1, 2 @ index -2, 1 @ index -3 , 0 @ index -4.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -