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

Hash Bang - 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



Perl also allows you to replace indices with user-defined "keys", in order to create a slightly different type of array, called a "hash" or "associative array". Each key is unique, and corresponds to a single value within the array.

# define hash 
%dinner = ("starter" => "fried squid rings""main" => "roast chicken""dessert" => "chocolate cake"); 

In this case, %dinner is an associative array variable containing key-value pairs. The % symbol before the variable name indicates that it is an associative array, while the => symbol is used to indicate the association between a key and its value.

Now, in order to access the value "fried squid rings", I would use the key "starter" and the notation $dinner{"starter"}, while the value "chocolate cake" would be accessible via $dinner{"dessert"}. Here's an example:

# define hash 
%dinner = ("starter" => "fried squid rings""main" => "roast chicken""dessert" => "chocolate cake"); 
# prints "fried squid rings" 
print $dinner{"starter"}; 

Note the use of curly braces here around the key name, and contrast it with the use of square braces for the index notation used in regular arrays.

As with arrays, you can also use this notation to define an associative array incrementally - this line of code

# define hash 
%dinner = ("starter" => "fried squid rings""main" => "roast chicken""dessert" => "chocolate cake");

is equivalent to these:

# define hash 
$dinner{"starter"} = "fried squid rings"
$dinner{"main"} = "roast chicken"
$dinner{"dessert"} = "chocolate cake"

Note that when using an associative array, Perl usually re-sorts the key-value pairs such that they are optimized for retrieval. Therefore, it's not a good idea to rely on your original element positions when accessing values in a hash; instead hash values should always be accessed by their respective keys.

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