Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Array index (menu)

  • $#array

  • $array[0]

  • $#array - the largest index

  • $array[1] - array elements are scalar

#!/usr/bin/perl
use strict;
use warnings;

my $color;

my @colors = ("Blue", "Yellow", "Brown", "White");
print "Please select a number:\n";
foreach my $i (0..$#colors) {
    print "$i) $colors[$i]\n";
}
my $num = <STDIN>;
chomp($num);
if (defined $colors[$num]) {
    $color = $colors[$num];
} else {
    print "Bad selection\n";
    exit;
}

print "The selected color is $color\n";

  • $array[-1]}