Home arrow Perl Programming arrow Page 6 - Array Manipulation in Perl

A Difficult Assignment - Perl

perlOver the course of this tutorial, I'll be examining Perl's arrays in detail, explaining what they are, how they work, and how you can use them to get things done faster, better and cheaper. In addition to providing a gentle introduction to Perl arrays and hashes in general, this article will also offer you a broad overview of Perl's array manipulation functions, providing you with a handy reference that should help you write more efficient code.

  1. Array Manipulation in Perl
  2. Back to Basics
  3. Hash Bang
  4. Harnessing Elements
  5. Looping the Loop
  6. A Difficult Assignment
  7. Push and Pull
  8. Slice and Dice
  9. Sorting Things Out
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 171
January 08, 2004

print this article



You can assign the elements of an array to scalar variables, as in the following example:

# define array 
@human = ("John""Doe"); 
# assign array contents to variables 
($fname$lname) = @human
# print variables 
print ("My name is $fname $lname");

This won't work with an associative array, though - for that, you need the each() function. Every time each() runs on a hash, it creates an array containing two elements: the hash key and the corresponding hash value.

# define hash 
%matrix = ("hero" => "neo""villain" => "smith""teacher" => "morpheus""babe" => "trinity"); 
# get first pair 
($character$name) = each (%matrix); 
"$character = $namen"
# get second pair 
($character$name) = each (%matrix); 
"$character = $namen"
# and so on... 

The each() function comes in particularly handy when you need to iterate through an associative array, as it is well-suited for use in a "while" loop:

# define hash 
%matrix = ("hero" => "neo""villain" => "smith""teacher" => "morpheus""babe" => "trinity"); 
# iterate through hash with each 
# returns villain = smith hero = neo babe = trinity teacher = morpheus 
while (($character$name) = each (%matrix)) 

"$character = $namen"

You can assign the array itself to another variable, thereby creating a copy of it, as below:

# define array 
@john = ("John""Doe"); 
# copy array 
@clone = @john
# print copy 
print ("I am a clone named @clone"); 

>>> More Perl Programming Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: