Perl Tutorial : Perl data types
Perl Tutorial : Perl Operators
Perl Tutorial : Perl loops
Perl Tutorial : Perl Conditionals
Perl Tutorial : Perl Subroutines
Perl Tutorial : Perl regex
Perl Tutorial : Perl Files
Perl Tutorial : Perl & Databases
Perl Tutorial : Perl OOPs
Perl Tutorial : Perl signals
Perl Tutorial : Perl threads
Perl Tutorial : Perl Debugging

Introcution to Perl CGI

Introduction to modperl
Home >> perldatatypes >>

Perl Arrays

  • Array constants are specified using parenthesis ( ) and the elements are separated with commas.
  • Array variable names begin with the at-sign (@).
  • The elements in an array do not all need to be the same type.
    @array = (11, 22, "perlhome","perl array");  ## a 4 element array 
    @empty_arr = ();               ## the array with 0 elements 
    $x = 1; $y = 2; @nums = ($x + $y, $x - $y); ## @nums is now (3, -1)
  • Just like most of other programming languages square brackets [ ] are used to refer to elements,
  • Array indexes start at 0.
  • $a[6] is the element at index 6 in the array @a. Note: @a[6] is wrong!
    @array_learn = (1, 2, "hello", "world"); 
    $array_learn[0] = $array_learn[0] + $array_learn[1];## $array_learn[0] is now 3 
    
    If code attempts to read an element outside the array size, undef is returned. If code writes outside the array size, the array grows automatically to be big enough. like in above case $array_learn[10] will return undef.
    @array = (1, 2, "hello", "there"); 
    $sum = $array[0] + $array[27];  
    ## $sum is now 1, since $array[27] returned undef 
    $array[99] = "the end";
    ## array grows to be size 100 
    
    When used in a scalar context, an array evaluates to its length. The "scalar" operator will force the evaluation of something in a scalar context, so you can use scalar() to get the length of an array. As an alternative to using scalar, the expression $#array is the index of the last element of the array which is always one less than the length.
    @array = (1, 2, "hello", "there"); 
    $len = @array;                
    ## $len is now 4 (the length of @array) 
    $len = scalar(@array);
    ## same as above, since $len represented a scalar 
    ## context anyway, but this is more explicit 
    
    @letters = ("a", "b", "c"); 
    $i = $#letters;## $i is now 2 
    
    That scalar(@array) is the way to refer to the length of an array is not a great moment in the history of readable code. At least I haven't showed you the even more vulgar forms such as (0 + @a).

    The sort operator (sort @a) returns a copy of the array sorted in ascending alphabetic order. Note that sort does not change the original array. Here are some common ways to sort...
    (sort @array)
    ## sort alphabetically, with uppercase first 
    (sort {$a <=> $b} @array)            
    ## sort numerically 
    (sort {$b cmp $a} @array)            
    ## sort reverse alphabetically 
    (sort {lc($a) cmp lc($b)} @array)    
    ## sort alphabetically, ignoring case (somewhat inefficient) 
    

  • Perl functions

    scalars and strings

    lc
    ord

    regex (regular expressions)

    arrays

    shift
    split

    More..

    eval
    Share |

    Perl @ARGV %ENV
    Perl Data Types
    Perl Hash
    Perl list
    Perl references
    Perl Scalar data types
    Perl Special Variables

    Perl examples

  • How to use Data::Dumper?
  • How to parse config file?
  • Include external module in perl..
  • Download perl

    download stable version of perl
    Perl 5.10.1
    Perl interview Questions
    sed tutorial
    awk tutorial

    Join us on Google Groups

    Subscribe to perl
    Email:
    Visit this group
    Perl Jobs


    privacy | sitemap | disclaim | contact us
    © 2009 perlhome.com