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

Push and Pull - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

You can add an element to the end of an existing array with the push() function,


#!/usr/bin/perl 
# define array 
@meals = ("lunch""tea"); 
 
# add a new element to the end of the array 
push (@meals"dinner"); 
 
# array now looks like this 
@meals = ("lunch""tea""dinner"); 

and remove an element from the end with the pop() function.


#!/usr/bin/perl 
# define array 
@meals = ("lunch""tea"); 
 
# remove an element from the end of the array 
pop (@meals); 
 
# array now looks like this 
@meals = ("lunch"); 

If you need to remove an element off the top of the array instead of the bottom, you need to use the shift() function,


#!/usr/bin/perl 
# define array 
@meals = ("lunch""tea"); 
 
# remove an element from the beginning of the array 
shift (@meals); 
 
# array now looks like this 
@meals = ("tea"); 

while the unshift() function takes care of adding elements to the beginning of the array.


#!/usr/bin/perl 
# define array 
@meals = ("lunch""tea"); 
 
# add an element to the beginning of the array 
unshift (@meals"breakfast"); 
 
# array now looks like this 
@meals = ("breakfast""lunch""tea"); 

When dealing with associative arrays, however, it's not a good idea to use these functions, since key-value pairs in a Perl associative array are not always stored in the order in which they were defined. Therefore, to remove an element from an associative array, you should instead use the delete() function, as in the example below:


#!/usr/bin/perl 
# define hash 
%matrix = ("hero" => "neo""villain" => "smith""teacher" => "morpheus""babe" => "trinity"); 
 
# delete key 
delete ($matrix{"villain"}); 
 
# hash now looks like this 
%matrix = ("hero" => "neo""teacher" => "morpheus""babe" => "trinity"); 

You can delete all the values in a hash simply by combining the delete() function with a "foreach" loop, as below:


#!/usr/bin/perl 
# define hash 
%matrix = ("hero" => "neo""villain" => "smith""teacher" => "morpheus""babe" => "trinity"); 
 
foreach 
$k (keys(%matrix)) 

        
delete ($matrix{$k}); 


The grep() function can tell you whether or not a particular value exists in an array. It accepts two arguments, a pattern to match and the array in which to search, and it scans every element of the named array for elements matching the pattern. Results, if any, are returned as another array. The following example illustrates how this works:


#!/usr/bin/perl 
# define array 
@tools = ("hammer""chisel""screwdriver""boltcutter""tape""punch""pliers"); 
 
# search for pattern "er" in array elements 
@match grep (/er/i, @tools); 
 
# print matching elements 
# returns "hammer screwdriver boltcutter pliers" 
print "@match "

The exists() function is useful to check if a particular key exists in a hash.


#!/usr/bin/perl 
# define hash 
%matrix = ("hero" => "neo""villain" => "smith""teacher" => "morpheus""babe" => "trinity"); 
 
# check for key existence 
if (exists($matrix{"hero"})) 

        print 
"Neo is alive!"




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

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: