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

Looping the Loop - 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



Want to obtain the size of an array? Sure - just assign the array to a scalar variable and print the value of the scalar:

# define array 
@friends = ("Rachel""Monica""Phoebe""Chandler""Joey""Ross"); 
# obtain size of array 
$count = @friends
# print size and contents 
print ("I have $count friends: @friends "); 

If you're using a hash, the keys() and values() functions come in handy to get a list of all the keys and values within the array.

# define hash 
%matrix = ("hero" => "neo""villain" => "smith""teacher" => "morpheus""babe" => "trinity"); 
# returns an array of keys 
@keys keys(%matrix); 
# returns an array of values 
@values values(%matrix); 

Most of the time, size and key information is used in the context of a loop that iterates over the array. Here's an example of how this might work in a numerically-indexed array,

# define array 
@ducks = ("Huey""Dewey""Louie"); 
# get array size 
$size = @ducks
"And heeeeeeeeeeeeeere are the ducks: "
# iterate over array 
for ($i=0$i<$size$i++) 

"$ducks[$i] "

and here's an example of iterating over an associative array using the keys() function:

# define hash 
%matrix = ("hero" => "neo""villain" => "smith""teacher" => "morpheus""babe" => "trinity"); 
"Say hello to the characters in The Matrix: n"
# get the keys of the array with keys() 
# then use the keys to get the corresponding value 
# in a loop 
foreach $k (keys(%matrix)) 

$k": "$matrix{$k}, "n"

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