Home
Perl How To
Perl Books
Perl Basics
Perl Editors
Perl Modules
Perl Database
Net Programming
Sitemap
About Me
Contact Me
Blog
 

Perl Array Functions - Perl List Functions



NEW!!!

Check my new resource:    Perl How To Tutorial eBooks



The Perl array functions are one of the most important category of the built-in Perl functions. As you know, three data types are available in the Perl language: scalars, arrays and hashes.

A scalar, which represents the fundamental type of Perl data, can be a string, a number or a reference. When you group together two or more scalars, you have a list. If you want to refer a list throughout a Perl script, you need an array variable, represented in Perl by an identifier preceded by the @ symbol. If some of the elements of an array are references to other arrays or structures, we say that we have a multidimensional array.

The Perl language supplies a lot of functions to manipulate lists and arrays. Every time you call a Perl array function, you must distinguish between a simple array and a multidimensional one. For instance, if you have an array which has as elements references to other arrays or hashes and if you want to delete all its elements, you need to step down through all its subarrays and delete their elements too.

In he following considerations, I used either list or array, these topics are somehow interchangeable, but keep in mind that an array is a variable name used to refer to a list. But for calling any of the below Perl array functions, you can use either a list or an array variable.

Next, I’ll describe briefly the most important Perl array functions.

  • defined – using it on an entire array reports if the array has ever been allocated; you can also use it against the elements of the array, which are scalars
  • grep – is used as a filter to extract those elements of a list or array for which an expression (or a regular expression) is evaluated true; it returns the sublist/subarray of the elements that passed the filter test
  • join – with this function you can merge any number of strings you want; you can specify with what token you want to delimitate the strings; in other words you can concatenate all the string elements of an array in a string, using a separator
  • map – takes a list and evaluates an expression or block on each element of the list; it returns a list with the results
  • pack – converts a list of values into a template string, enabling you to write data in a format that would be compatible with other program languages such as C; practically, it packs the list of values into a binary structure that will be returned as a string
  • pop – removes and returns the last element of an array; the size of the array decreases by 1
  • print – is used to display a list of strings on the screen (either to your browser using a CGI script or to your terminal running a script from a command line) or to send it to a file; it is a very simple output function and provides no important formatting capabilities (use printf if you need more sophisticated formatting)
  • push – appends the elements of a list to an array; the size of the array increases by the size of the list
  • qw – this 'quote word' function is used to generate a list of words and returns the list containing the words; it automatically quotes the strings for you
  • reverse – in a scalar context, it concatenates the elements of a list in a string and returns the string with all the characters in an opposite order; in a list context, it returns the list in the reversed order
  • scalar – enables you to evaluate an expression in a scalar context and returns the value of that expression
  • shift – removes and returns the first element of an array, reducing the number of the array elements by one;
  • sort – sorts a list by an alphabetical or numerical order and returns the sorted list
  • splice – removes a subarray from an array and eventually replaces it with another array
  • split – converts a string into a list and returns that list; the words of the string must be delimitated by a separator
  • undef – if you use it for an entire array, this function will clear the array and free up the system memory allocated for it
  • unpack – takes a binary string and uses a template string to convert it into a list of values, according to the supplied format
  • unshift – inserts a value or a list of values at the beginning of an array and returns the new total number of the array’s elements.

I enumerated above some of the most important Perl array functions that you can meet in your Perl Script. In the next developing of this site I intend to provide a lot of examples about how to use Perl array functions in Perl language.




NEW!!!

Do you want more information about the basic Perl topics?

Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :


Perl How To Tutorial eBooks


Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Array Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        grep
        hex
        index
        join
        lc
        lcfirst
        length
        map
        oct
        ord
        pack
        pop
        push
        qw
        reverse
        rindex
        scalar
        shift
        splice
        split
        substr
        uc
        undef
        unshift

return from Perl Array Functions to Perl Basics



Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!


footer for perl array functions page