Variables
Assigning to Array Variables

  • Parentheses () define a new list

      @foo = ( "apple", "bat", "cat" );

  • The scalar $ plus [] references one element (note: indexes start at 0)

      $foo[0] = "ape";

  • Creating and setting an element

      $foo[3] = "dog";

  • Assigning multiple element values

      $foo[1,3] = ( "bear", "dear" );

  • Adding new elements

      @foo = ( @foo, "elk" ); # Append
      @foo = ( "ace", @foo ); # Prepend
  • push(@food, "eggs");
    

    which pushes eggs onto the end of the array @food.

    push(@food, "eggs", "lard");
    push(@food, ("eggs", "lard"));
    push(@food, @morefood);