Perl Basics
Introduction to Perl
FAQ's
CGI
Regular Expressions

PC Overview
Cool Stuff
My Modules
Success Stories
Links
Perl in the News
Logos
Perl @ msn
Perl Chat

PC Internals
About
Contact
Guest Book
Handy Logos
What's new

Hashes

See also
P-friendly

[ home ] - [ search ] - [ sitemap ]

Using Arrays and Hashes

Using arrays and hashes in Perl is easy. Complexity begins when you continue to work with references to these data types (Find some examples below).

It can be powerful to combine the array and hash data type. For example you might assign a reference of an array to a hash key: $hash{'foo'} = \@array;
The benefit of the performance might be worth the cost of complexity.

Recursion is of unlimited depth as the general philosophy in Perl itself.

Nested Hashes in Perl

[ Playing with hashes ]

#!/usr/bin/perl -w

use strict;

my %myhash = (

 "image" => {
         "version"      => "" ,
         "name"         => "DSCN4757" ,
         "type"         => "jpg" ,
         "raw_width"    => "" ,
         "raw_height"   => "" ,
         "width"        => "" ,
         "height"       => "" ,
         "resizedName"  => "DSCN4757.sized" ,
} ,
 "thumbnail" => {
         "version"      => "" ,
         "name"         => "DSCN4757.thumb" ,
         "type"         => "jpg" ,
         "raw_width"    => "" ,
         "raw_height"   => "" ,
         "width"        => "" ,
         "height"       => "" ,
         "caption"      => "DSCN4757" ,
} ,
 "itemCaptureDate" => {
         "hours"        => "21" ,
         "minutes"      => "52" ,
         "seconds"      => "59" ,
         "mday"         => "12" ,
         "mon"          => "06" ,
         "year"         => "2003" ,
         "uploadDate"   => "" ,
         "clicks"       => "" ,
} ,

"highlight" => {}

);

print "---> ", $myhash{'image'}->{'resizedName'}, "\n";

Playing with hashes


my $categ = &CATEG();

print $categ->{'fookey'}[0]; # prints out 'aa'

sub CATEG () {
	my @array1 = qw ( aa bb cc );
	my @array2 = qw ( ddd eee fff );

	my %hash = (
		fookey => \@array1 ,
		barkey => \@array2
	);

	\%hash;
}
home - feedback - search - web jobs
© 1998-2004 retoh :)