Home Scripting Perl Tutorial Associative Arrays
Main Menu
System Verilog
Open Vera
Digital Concepts
Verification Basics
    - Perl Tutorial
        -- Perl Definition
        -- Syntax & Variables
        -- strings
        -- Arrays
        -- Associative Arrays
        -- If & While Loop
        -- File Input
        -- Print Output
        -- Strings and Regular Expressions
        -- Subroutines
        -- Running External Programs
        -- References
        -- Terse Perl
    - Bash
    - Makefile Tutorial
    - Perl Examples
    - Bash Examples
Interview Questions
Computer Architechture
C and C++
AsicGuru Blog
Tags Cloud
Usefull Sites
Know Your IP/Location
Local Information India
Buy Car/Inverter Batteries
Real Estate India
Sports Accessories India
Perl Associative Arrays
Share This Articale:

Associative Arrays/Hash Arrays

Hash arrays, also known as "associative" arrays, are a built-in key/value data structure. Hash arrays are optimized to find the value for a key very quickly. Hash array variables begin with a percent sign (%) and use curly braces { } to access the value for a particular key. If there is no such key in the array, the value returned is undef. The keys are case sensitive, so you may want to consistently uppercase or lowercase strings before using them as a key (use lc and uc).
$dict{"bart"} = "I didn't do it";
$dict{"homer"} = "D'Oh";
$dict{"lisa"} = "";

## %dict now contains the key/value pairs (("bart" => "I didn't do it"),
## ("homer" => "D'oh"), ("lisa" => ""))
$string = $dict{"bart"};               ## Lookup the key "bart" to get
                                                ## the value "I didn't do it"
$string = $dict{"marge"};           ## Returns undef -- there is no entry for
$dict{"homer"} = "Mmmm, scalars";                   ## change the value for the key
                                                                       ## "homer" to "Mmmm, scalars"
A hash array may be converted back and forth to an array where each key is immediately followed by its value. Each key is adjacent to its value, but the order of the key/value pairs depends on the hashing of the keys and so appears random. The "keys" operator returns an array of the keys from an associative array. The "values" operator returns an array of all the values, in an order consistent with the keys operator.

@array = %dict;
## @array will look something like
## ("homer", "D'oh", "lisa", "", "bart", "I didn't do it");
## (keys %dict) looks like ("homer", "lisa, "bart")
## or use (sort (keys %dict))
You can use => instead of comma and so write a hash array value this cute way...
%dict = (
   "bart" => "I didn't do it",
   "homer" => "D'Oh",
   "lisa" => "",
In Java or C you might create an object or struct to gather a few items together. In Perl you might just throw those things together in a hash array.


@ARGV and %ENV
The built-in array @ARGV contains the command line arguments for a Perl program. The following run of the Perl program critic.pl will have the ARGV array ("-poetry", "poem.txt").

unix% perl critic.pl -poetry poem.txt
%ENV contains the environment variables of the context that launched the Perl program.
@ARGV and %ENV make the most sense in a Unix environment.

This Articles is written/submitted by puneet (Puneet Aggarwal). You can also contribute to Asicguru.com. Click here to start

Prev << Arrays

Next >> If & While Loop

Sign In
Login with :-
| | |  
  • Bookmark