Perl Tutorials - Herong's Tutorial Examples - v6.03, by Herong Yang
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:
#- ArrayVariable.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.
#
@links; # Undefined
print(@links, "\n");
@links = ("herongyang.com", "perl.org", "cpan.org");
print(@links, "\n"); # Defined now
$url = $links[0]; # Refers to the first element
print($url, "\n");
$links[2] = "google.com"; # Updates the 3rd element
print($links[2], "\n");
$links[9] = "yahoo.com"; # 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:
herongyang.comperl.orgcpan.org herongyang.com google.com yahoo.com herongyang.com 10 undef
Table of Contents
►Data Types: Values and Variables
Variables - Scalar, Array and Hash
"undef" Value and Undefined Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
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 Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
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
CGI.pm Module for Building Web Pages
LWP::UserAgent and Web Site Testing
Converting Perl Script to Executable Binary