www.perl.com
Perl Programming
Arrays
  • Indexed by number
    • An index in square brackets refers to an array item
    • Each item is a scalar, so use scalar syntax $array[$index]
    • By default, first item is index 0
    • Last item is index $#array
    • Negative numbers count from end of list
    • Can directly assign an array item

        my @fruits = ("apples", "bananas", "cherries");

        print "Fruit flies like $fruits[1].\n";	            
        print "Life is like a bowl of $fruits[$#fruits].\n";
        print "We need more $fruits[-3] to make the pie.\n";

        $fruits[0] = "oranges"; # Replace apples with oranges
      

        Output:
        Fruit flies like bananas.
        Life is like a bowl of cherries.
        We need more apples to make the pie.
      
Previous http://stuff.mit.edu/iap/perl/