Associative Array
2002-09-14 00:31:56
Category: perl:general
Description: Associative array's can be very useful in organizing your data. This example will show you how to make use of them.
Author: detour
Viewed: 3474
Rating: (10 votes)


#!/usr/bin/perl
# 
# ass_array.pl written by detour@metalshell.com
#
# Associative array's can be very useful in organizing
# your data.  This example will show how to make use
# of them.
#
# http://www.metalshell.com/
#
 
use strict;
# You can define multiple keys with values when
# you declare your associative array.
my $key;
my %assoc = ("PX", "Philadelphia Experiment",
         "1WG", "One World Governmnet",
         "NWO", "New World Order",
         "MIB", "Men In Black");
 
# You can access a value for a key by doing the following.
print "PX stands for " . $assoc{"PX"} . ".\n\n";
 
# You can add or modify values at any time
$assoc{"CIA"} = "Central Intelligence Agency";
 
# Cycle through every key and print the values.
foreach $key (keys %assoc) {
  print "$key stands for " . $assoc{$key} . ".\n";
}
print "\n";
 
# Delete the unwanted values.
delete $assoc{"NWO"};
delete $assoc{"1WG"};
 
foreach $key (keys %assoc) {
  print "$key stands for " . $assoc{$key} . ".\n";
}