Changes

Jump to navigation Jump to search
40 bytes added ,  16:03, 28 December 2013
Line 120: Line 120:  
==== Arrays ====
 
==== Arrays ====
   −
    An array represents a list of values:
+
An array represents a list of values:
    
         my @animals = ("camel", "llama", "owl");
 
         my @animals = ("camel", "llama", "owl");
Line 126: Line 126:  
         my @mixed = ("camel", 42, 1.23);
 
         my @mixed = ("camel", 42, 1.23);
   −
    Arrays are zero-indexed. Here's how you get at elements in an array:
+
Arrays are zero-indexed. Here's how you get at elements in an array:
    
         print $animals[0]; # prints "camel"
 
         print $animals[0]; # prints "camel"
 
         print $animals[1]; # prints "llama"
 
         print $animals[1]; # prints "llama"
   −
    The special variable $#array tells you the index of the last element of an array:
+
The special variable '''$#array''' tells you the index of the last element of an array:
    
         print $mixed[$#mixed]; # last element, prints 1.23
 
         print $mixed[$#mixed]; # last element, prints 1.23
   −
    You might be tempted to use $#array + 1 to tell you how many items there are in an array. Don't bother. As it happens, using @array where Perl expects to find a scalar value ("in scalar context") will give you the number of elements in the array:
+
You might be tempted to use '''$#array + 1''' to tell you how many items there are in an array. Don't bother. As it happens, using '''@array''' where Perl expects to find a scalar value ("in scalar context") will give you the number of elements in the array:
    
         if (@animals < 5) { ... }
 
         if (@animals < 5) { ... }
   −
    The elements we're getting from the array start with a $ because we're getting just a single value out of the array; you ask for a scalar, you get a scalar.
+
The elements we're getting from the array start with a '''$''' because we're getting just a single value out of the array; you ask for a scalar, you get a scalar.
   −
    To get multiple values from an array:
+
To get multiple values from an array:
    
         @animals[0,1]; # gives ("camel", "llama");
 
         @animals[0,1]; # gives ("camel", "llama");
Line 147: Line 147:  
         @animals[1..$#animals]; # gives all except the first element
 
         @animals[1..$#animals]; # gives all except the first element
   −
    This is called an "array slice".
+
This is called an "array slice".
   −
    You can do various useful things to lists:
+
You can do various useful things to lists:
    
         my @sorted = sort @animals;
 
         my @sorted = sort @animals;
 
         my @backwards = reverse @numbers;
 
         my @backwards = reverse @numbers;
   −
    There are a couple of special arrays too, such as @ARGV (the command line arguments to your script) and @_ (the arguments passed to a subroutine). These are documented in perlvar.
+
There are a couple of special arrays too, such as '''@ARGV''' (the command line arguments to your script) and '''@_''' (the arguments passed to a subroutine). These are documented in [http://perldoc.perl.org/perlvar.html perlvar].
 +
 
 
==== Hashes ====
 
==== Hashes ====
  

Navigation menu