Double index in Perl, why is this so?

with

@a=(6,3,5,7);

@b=(@a[0..3])[2..3];

print @b;

#print 57

but for

@b=@a[0..3][2..3];

I get a syntax error. Can someone explain why?

+3
source share
2 answers

$ a [1] [2] for is used for two-dimensional tables, in fact it is short for $ a [1] → [2]

therefore, for the first indexing, you need to return a link, not a slice of the array.

A syntax error occurs because Perl does not know how to dereference an array.

+8
source
@a=(6,3,5,7);

This creates an array with 4 elements.

(@a[0..3])

This returns a list with the same four elements as @a.

(@a[0..3])[2..3];

This selects the last two items from a list of 4 items inside the brackets.

print( join( ",", @b ) );

5,7, @a.

:

@a=(6,3,5,7);
@b=(@a[0..3]);
print( "\@b=" . join(",",@b) . "\n" );
@c=@b[2..3];
print( "\@c=" . join(",",@c) . "\n" );

, Perl . , , Perl .

+3

Source: https://habr.com/ru/post/1751009/


All Articles