This article focuses on the concept of a Perl hash table. The Perl language and other programming languages have different features. Here we will share with you the concept of a Perl hash table, in fact, a Perl hash table is a structure.
Perl hash table
A Perl hash table is a structure.
Key/value.
Access Perl Hash Table Elements
$ Perl hash table {$ some_key}
When selecting a name for a Perl hash table, it is best to think like this: the names and keys of the elements in the Perl hash table can be connected using. For example, thefamily_nameforfredisflintstone.
To reference the entire Perl hash table, use the percent sign (%) as the prefix.
The Code is as follows :#! /Bin/perl
Usewarnings;
Usestrict;
My $ person;
My % family_name;
$ Family_name {"fred"} = "flintstone ";
$ Family_name {"barney"} = "rubble ";
Foreach $ person (qw <barneyfred> ){
Print "I 'veheardof $ person $ family_name {$ person}. \ n ";
}
My % some_hash = ("foo", 35, "bar", 12.4, 25, "hello", "wilma", 1.72e30, "betty", "bye \ n ");
My @ array_array = % some_hash =;
Print "@ array_array \ n ";
Hash Value assignment (=>)
The Code is as follows: my % last_name = (
"Fred" => "flintstion ",
"Dino" => undef,
"Barney" => "rubble ",
"Betty" => "rubble ",
);
The keys function returns all keys of this Perl hash table, and values includes tax. If there are no elements in the Perl hash table, this function returns an empty list.
The Code is as follows: my @ k = keys % last_name;
My @ v = values % last_name;
My $ count = keys % last_name; # scalar-producing, key/valuepairs
Print "thekeyare @ k. \ n ";
Print "thevalueare @ v. \ n ";
Print "thecountare $ count. \ n ";
Each function
To iterate to get each element in the Perl hash table, a common method is to use the each function, which returns the element pair of the key/value pair. When an iteration is performed on the same Perl hash table function, the next key/value pair is returned until all elements are accessed.
The Code is as follows: my $ key;
My $ value;
While ($ key, $ value) = each % last_name ){
# Foreach ($ key, $ value) = each % last_name ){
Print "$ key => $ value. \ n ";
}
Note that the two cycles have different design mechanisms.
The Code is as follows: foreach $ key (sortkeys % last_name ){
$ Value = $ last_name {$ key };
Print "$ key => $ value. \ n ";
Print "$ key => $ last_name {$ key}. \ n ";
}