Getting Started
Introduction
Perl IDEs
History
Advice
Tools
Mini-Tutorial
Tutorial
Code Snippets

Resources
Top Sites
More Tutorials
Books
Magazines
Articles
NewsLetters
Mailing Lists
NewsGroups
Forums
User Groups
Talk Shows
Blogs
Clothing

GBIC >> Perl >> Information Center Tutorials >> Arrays

Perl Information Center Tutorials - Arrays
These tutorials were written to help you get a quick, but thorough, understanding of Perl - the scope of the language as well as it's specific capabilities.

Beginners Built-In Functions     Advanced CGI Applications
Array Functions Summary
Manipulation of arrays is one of Perl's strong points. The array functions can be categorized roughly into four areas, depending on how they manipulate or return information about arrays.

    • Add Elements
     push, unshift
    • Delete Elements
     pop, shift
    • Combline/Split Elements    
     splice, split, join
    • Rearrange Elements
     sort, reverse

Array Functions Reference
Here's a quick reference of the available array functions, in alphabetical order.

Unless otherwise noted, these functions operate on $_ by default.

  • join - joins a list/array into a string
        $result = join EXPR, @array
        
  • pop - removes last element of array, and returns it
        pop @array    # same effect as $array[$#array--]
        
  • push - adds elements to end of array
        push @array, LIST
        
  • shift - remove first element of an array, and returns it
        shift @array
        
  • sort - sorts a list/array
        $result = sort [SUBNAME,BLOCK] list
        
  • splice - add/remove elements anywhere in the array
         splice @array, OFFSET, LENGTH, LIST
              # removes elements designated by OFFSET and LENGTH 
              # replaces them with the elements of LIST    
              # negative OFFSET starts from end of array
              # LENGTH may be omitted
        

  • split - separate a string into substrings
        split /PATTERN/, (expression), LIMIT   
        Examples:
        ($one, $two, $three) = split ( /:/ "111:222:333" )
        @result = split ( / / "111 222 333" )
        
    Note: The expression default is $_ and can be a regex or simply an exact string.

  • unshift - adds elements to start of array
        unshift @array, LIST
        
  • reverse - reverses elements of a list
        @result = reverse (1,2,3)
        

Array Code Snippets
The following code shows several methods of manipulating arrays.

     @x = (3,"dog",1.9)    # assign a list of values 
     $x[5]=3               # assigns value to array element number 6
     @newarray = @x[0..3]  # slice of values from existing array

     $size = @x                 # $size is # elements in array
     $last index = $#x          # $lastindex is highest index in array
     $#x = 5                    # truncates array at 5 elements
     @x = ()                    # clears an array
     @x = sort @x               # sort an array
     @x = reverse @x            # reverse an array
     splice (@x, 0, 5)          # remove elements 0-4
     @new = splice (@x, 0, 5)   # @new has the removed elements 0-4
     splice (@x, 2, 1, "dog")   # element 2 replaced with "dog"
     @new = @x[1,4,6]           # @new has only elements 1,4,6
     ($a,$b) = @x               # assigns $a/$b from array

If you have any suggestions or corrections, please let me know.