Using Array Variables

This section describes how an array variable can be assigned with a list value. Array elements are referred by subscription notation as scalar expressions, like $identifier[i]. An unassigned array variable contains the (undef) value.

Array variables can be used in a Perl script under these rules:

Here are some examples of array variable assignment operations with list value constructors:

   @messages = 'hello';
   @primes = 3,5,7,11;
   @vacations = 'Mon','Tue','Wed';
   @vacations = ('Mon','Tue','Wed');
   @vacations = (Mon,Tue,Wed);
   @menu = 'Mon',2,'Apple';
   @dates = 'Jan',31,'Feb',28,'Mar',31;

Here is a Perl tutorial script on how to use array variables:

#- Copyright (c) All Rights Reserved.
   @links;                             # Undefined
   print(@links, "\n");

   @links = ("", "", "");
   print(@links, "\n");                # Defined now

   $url = $links[0];                   # Refers to the first element
   print($url, "\n");

   $links[2] = "";           # Updates the 3rd element
   print($links[2], "\n");

   $links[9] = "";            # Extending the array size
   print($links[9], "\n");

   print($links[-10], "\n");           # Backward index

   $size = $#links + 1;                # last index value
   print($size, "\n");

   print ("undef", "\n") if ($links[100] == undef);
                                       # Out of bound index

Here is the output of the tutorial script:

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

Data Types: Values and Variables

 Scalar Values and List Values

 Scalar Value Constructors

 Scalar Value Interpretation

 List Value Constructors

 Variables - Scalar, Array and Hash

 Using Scalar Variables

Using Array Variables

 Using Hash Variables

 "undef" Value and Undefined Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Image and Picture Processing

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials


 Full Version in PDF/EPUB