Skip to content
asicguru.tech@gmail.com
REGISTER LOGIN
AsicGuru Ventures
  • All Courses
  • CONTACT
  • GALLERY
  • HOME
  • Tutorials

0

Verilog Tutorial

17
  • Verilog Introduction
  • Lexical Tokens
  • Gate-Level Modelling
  • Data Types
  • Operators
  • Operands
  • Modules
  • Behavioral Modeling
  • Timing Controls
  • Procedures: Always and Initial Blocks
  • Functions
  • Tasks
  • Component Inference
  • Finite State Machines.
  • Compiler Directives
  • System Tasks and Functions
  • Test Benches

System Verilog

23
  • System Verilog Introduction
  • Data Types
  • Type Casting
  • Arrays
  • Associative Arrays
  • Dynamic Arrays
  • Queue
  • Operators
  • Procedural statements and Control flow
  • Classes
  • Inheritance
  • Encapsulation
  • This and Super operator
  • In-Line Constraints
  • Class Constraints
  • Virtual Classes
  • Parameterized Classes
  • Classes Summary
  • Singleton Classes
  • Functional Coverage Introduction
  • Covergroup
  • Tools
  • Books

Scripting

15
  • Introduction
  • Perl Tutorial
  • What is Perl
  • Perl: Syntax And Variable
  • Perl Strings
  • Perl Arrays
  • Perl Associative Arrays
  • If/While Syntax
  • File Input
  • Print Output
  • String Processing with Regular Expressions
  • Subroutines
  • Running External Programs
  • References
  • Terse Perl

Makefile

1
  • Makefile Tutorial
View Categories
  • Home
  • Tutorials
  • Scripting
  • Perl Associative Arrays

Perl Associative Arrays

2 min read

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
“marge”
$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.

Updated on May 4, 2025

What are your Feelings

  • Happy
  • Normal
  • Sad
Share This Article :
  • Facebook
  • X
  • LinkedIn
  • Pinterest
Perl ArraysIf/While Syntax

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • May 2025

Categories

  • Slider Post
  • Uncategorized

Copyright @ Asicguru Ventures by Misbah WP | Proudly powered by WordPress