This article is focused on the Perl hash table concept, Perl and other programming languages have their own characteristics, here and you share the concept of Perl hash table, in fact, Perl hash table is a structure.
Perl Hash Table
A Perl hash table is a structure.
Key/value.
Accessing Perl hash Table elements
$Perl Hash Table {$some _key}
When choosing a name for a Perl hash table, it's best to think about it: The Perl hash Table element's name and key can be connected by using for. such as Thefamily_nameforfredisflintstone.
To refer to the entire Perl hash table, use the percent sign (%) as the prefix.
Copy Code code 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", "," Bar ", 12.4,25," Hello "," Wilma ", 1.72e30," Betty "," bye\n ");
my@array_array=%some_hash=;
print "@array_array \ n";
Hashi Value Method large arrow symbol (=>)
Copy Code code as follows:
My%last_name= (
"Fred" => "Flintstion",
"Dino" =>undef,
"Barney" => "rubble",
"Betty" => "rubble",
);
The keys function returns all values for this Perl hash table with all keys,values tax-included. If there are no elements in the Perl hash table, this function returns an empty list.
Copy Code code 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
If you want to iterate through each element of the Perl hash table, a common method is to use the each function, which returns the element pairs 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.
Copy Code code as follows:
My$key;
My$value;
while (($key, $value) =each%last_name) {
#foreach (($key, $value) =each%last_name) {
Print "$key => $value. \ n";
}
Notice the results of the two loops because of the different design mechanisms of the two loops.
Copy Code code as follows:
Foreach$key (sortkeys%last_name) {
$value = $last _name{$key};
Print "$key => $value. \ n";
Print "$key => $last _name{$key}.\n";
}