Home > Enterprise >  Why it returns last element of array?
Why it returns last element of array?

Time:01-01

new to Perl, and I found Perl scalar and list quite confusing.

At code below,

$a = ("a", "b", "c");

print "$a \n";

the result will be "c"

But if I give name to the array

@arr = ("a", "b", "c");
$a = @arr;

print "$a \n";

the result is 3, which is the number of element

I think the secound one is specification, but I don't get the logic why it returns "c" on the first one.

CodePudding user response:

@arr = ("a", "b", "c");

This is a list assignment operator because left-hand side of the = "looks listy". The list assignment operator evaluates its right-hand side in list context.

The comma operator in list context evaluates each operand in list context, and produces the all the scalars produced by its operands.


$a = @arr;

This is a scalar assignment operator because left-hand side of the = "doesn't look listy". The scalar assignment operator evaluates its right-hand side in scalar context.

An array in scalar context produces the number of elements in the array.


$a = ("a", "b", "c");

This is a scalar assignment operator because left-hand side of the = "doesn't look listy". The scalar assignment operator evaluates its right-hand side in scalar context.

The comma operator in scalar context evaluates all but the last operand in void context, then it evaluates the last operand in scalar context. It produces the scalar produced by this last operand.

This means that

EXPR, EXPR2, EXPR3           # In scalar context

is similar to

do { EXPR; EXPR2; EXPR3 }    # In scalar context

(Just no lexical scope.)


Ref:

CodePudding user response:

Summary on array usage

use strict;
use warnings;
use feature 'say';

my @array     = ( 'a', 'b', 'c', 'd' );
my $aref      = [ 'a', 'b', 'c', 'd' ];
my $elem      = ( 'a', 'b', 'c', 'd' );
my $last      = @array[-1];
my @slice     = @array[0,2..3];
my $count     = scalar @array;
my $index_max = $#array;

say "
    \@array     = @array        array
    \$aref      = $aref         array reference (holds address of anonymous array)
    \$elem      = $elem         last element of array (useless void context warnings)
    \$last      = $last         last element of array
    \@slice     = @slice        array slice 
    \$count     = $count        count array elements
    \$index_max = $index_max    max array index
    
    Note: array index starts with 0
";

Output

Useless use of a constant ("a") in void context at C:\temp\work\perl\examples\array_demo.pl line 7.
Useless use of a constant ("b") in void context at C:\temp\work\perl\examples\array_demo.pl line 7.
Useless use of a constant ("c") in void context at C:\temp\work\perl\examples\array_demo.pl line 7.

        @array     = a b c d            array
        $aref      = ARRAY(0x107a790)   array reference (holds address of anonymous array)
        $elem      = d                  last element of array (useless void context warnings)
        $last      = d                  last element of array
        @slice     = a c d              array slice
        $count     = 4                  count array elements
        $index_max = 3                  max array index

        Note: array index starts with 0

A few array tutorials:

  •  Tags:  
  • perl
  • Related