Home arrow Perl Programming arrow Lists and Arguments in Perl

Lists and Arguments in Perl

In this conclusion to a six-part article series on subroutines and functions in Perl, you'll learn more about lists and arrays, and take a look at default argument values. This article was excerpted from chapter six of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. Lists and Arguments in Perl
  2. Default Argument Values
  3. Named Parameters
By: Apress Publishing
Rating: starstarstarstarstar / 0
May 08, 2012

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Lists Are One Dimensional

Recall that all lists and all arrays are one dimensional. If we have this list:

(@a, @b)

it becomes a one-dimensional list containing the contents of@afollowed by the contents of@b. This is an important rule when it comes to passing arrays into functions, since they will be passed in as a one-dimensional list. This is illustrated in the following example:

#!/usr/bin/perl -w
# passarrays.pl

use strict;

my(@nums1, @nums2);
@nums1 = (2, 4, 6);
@nums2 = (8, 10, 12);

process_arrays(@nums1, @nums2);

sub process_arrays {
my(@a, @b) = @_;

print "contents of \@a\n";
print "[$_] " foreach @a;
print "\n\n";

print "contents of \@b\n";
print "[$_] " foreach @b;
print "\n";
}

This program creates two 3-element arrays,@nums1and@nums2. These arrays are then passed intoprocess_arrays()and are immediately copied into two arrays,@aand@b. We might think that@areceives the contents of@nums1and@breceives the contents of@nums2, but that is not what happens. Since the arguments are passed in as

process_arrays(@nums1, @nums2);

the elements are flattened into this one-dimensional list:

(2, 4, 6, 8, 10, 12)

and this list is passed in and assigned to the assignable list:

my(@a, @b) = @_;

Since this assignable list contains an array,@a, it will consume all the elements that are assigned to it. Therefore,@bwill be empty because there are no elements remaining to assign to it. So, when we execute this program, we will see that@acontains all the elements passed in and@bcontains no elements:

$ perl passarrays.pl
contents of @a
[2] [4] [6] [8] [10] [12]

contents of @b

$

Later, when we discuss references in Chapter 11, we will see how to pass two arrays (or hashes) into a function and treat them as two separate variables.



 
 
>>> More Perl Programming Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: