Perl如何使用哈希运算(分析和示例)

    先决条件: Perl哈希, Perl散列
正如大多数读者可能知道的那样, 杂凑通过使用一种称为散列。在散列中, 键用于确定值或数据。这些键必须是独特然后用作存储与键关联的数据的索引。该数据不必是唯一的。它可能是重复的。
散列是一种数据结构, 它通过维护键和值之间的关系来存储数据, 或者键/值对。给定一个密钥, 我们可以找到它的价值。键/值对是可变的对象, 因此, 可以随时更改它们。哈希中的键可以进一步定义为用于检索值的对象。使用散列而不是数组的主要优点是, 散列允许基本操作的执行时间, 例如获取值或在特定键处设置值(在数组的情况下为索引), 甚至对于大型数据集也保持恒定。
请注意, Perl中的哈希为未下令。这意味着, 当你遍历哈希时, 可能无法按插入值的顺序提取值。
存储在哈希罐类型中的值整数, 浮点数, 字符串, 布尔值, 数组和哈希本身。
例子
#!/usr/bin/perl# Creating a simple hash containing # different types of values my %hash = ( # value type string 'MyVehicle' => 'Car' , # value type integer 'Model' => 1234, # value type float 'Speed' => 60.7, # value type hash 'Traffic' => { 'Red' => 'Stop' , 'Yellow' => 'Look and move' , 'Green' => 'Go' }, # value type array 'AllVehicles' => [ 'Car' , 'Cycle' , 'Bus' , 'Auto' ]); # printing values stored # at key 'Traffic' and 'AllVehicles' print "Traffic : $hash{'Traffic'}\n" ; print "AllVehicles : $hash{'AllVehicles'}\n" ;

输出如下:
Traffic : HASH(0x242af30) AllVehicles : ARRAY(0x24471f8)

此处, 键Traffic上的数据类型为哈希, 而键AllVehicles上的数据类型为array。因此, 输出分别是哈希和数组的第一个元素的地址。
此外, 可以对散列执行许多操作或操作, 如下所述:
哈希操作
Perl哈希操作包括对哈希进行操作以更有效地存储和检索数据的各种操作。哈希中最常用的操作是:
  1. 访问哈希中的键和值。
  2. 修改特定键的值。
  3. 循环到哈希。
下面用示例说明Perl Hash中的每个操作:
查找/访问Perl哈希值或键
查找或访问意味着能够访问哈希的每个键/值对以进行进一步操作。 Perl中的键以一种最佳方式存储哈希的元素, 从而可以非常快地获取其值。可以使用嵌入在哈希值之间的相应键名来查找哈希值{}括号。
语法:$ hash_name {key_name};
例子
# Perl program to demonstrate # accessing of the hash values my %hash = ( 'MyVehicle' => 'Car' , 'Model' => 1234, 'Speed' => 60.7, # value type hash 'Traffic' => { 'Red' => 'Stop' , 'Yellow' => 'Look and move' , 'Green' => 'Go' }, # value type array 'AllVehicles' => [ 'Car' , 'Cycle' , 'Bus' , 'Auto' ]); # Creating array containing # keys of the hash @k = keys %hash ; # print all keys of the hash print "Keys are : " ; print join ( ", " , @k ), "\n" ; # Creating array containing # values of the hash @v = values %hash ; # print value of all values print "Values are : " ; print join ( ", " , @v ), "\n" ; # accessing individual values of the keys print "Speed is : $hash{'Speed'}\n" ; print "Yellow light indicates : $hash{'Traffic'}{'Yellow'}\n" ; print "$hash{'AllVehicles'}[3] is a type of vehicle \n" ;

输出如下:
Keys are : AllVehicles, MyVehicle, Speed, Traffic, Model Values are : ARRAY(0x9361f8), Car, 60.7, HASH(0x919f30), 1234 Speed is : 60.7 Yellow light indicates : Look and move Auto is a type of vehicle

修改哈希元素
哈希中的值不是固定的, 也就是说, 它们很容易更改, Perl提供了修改和更新哈希中值的能力。对于给定的键, 使用以下语法来修改或更新其对应的值:
语法:$ hash {'Key'} = modified_value;
要在不更改给定键的情况下更改其对应值, 只需创建一个附加键(即修改后的键), 然后将值(你想要此新键)分配给该键, 然后使用删除前一个键/值对删除关键词。
范例:
# Perl program to demonstrate the # Modification of an element of a Hash # creating a hash my %hash = ( 'MyVehicle' => 'Car' , 'Model' => 1234, 'Speed' => 60.7, # value type hash 'Traffic' => { 'Red' => 'Stop' , 'Yellow' => 'Look and move' , 'Green' => 'Go' }, # value type array 'AllVehicles' => [ 'Car' , 'Cycle' , 'Bus' , 'Auto' ]); # previous value of key 'Model' print ( "Previous Model number is " , $hash { 'Model' }, "\n" ); # modifying value of key 'Model' $hash { 'Model' } = 7717; # new value of key 'Model' print ( "New Model number is " , $hash { 'Model' }, "\n" ); # Changing key from 'MyVehicle' to 'Mine' # without changing its corresponding value @k = keys %hash ; # printing previous keys print "Previous Keys are : \n" ; print join ( ", " , @k ), "\n" ; $hash { 'Mine' } = 'Car' ; # deleting 'MyVehicle' key/value pair delete $hash { 'MyVehicle' }; @k_n = keys %hash ; print "New Keys are : \n" ; # printing new keys print join ( ", " , @k_n ), "\n" ;

输出如下:
Previous Model number is 1234 New Model number is 7717 Previous Keys are : MyVehicle, AllVehicles, Model, Traffic, Speed New Keys are : Mine, Speed, Traffic, Model, AllVehicles

遍历Perl哈希值
Perl允许循环其哈希值。这意味着散列是迭代类型, 可以使用" for"循环和" while"循环遍历其键和值。在Perl中, keys()函数提供了类似于Python编程语言中的哈希数据结构。此keys()函数允许你获取标量的哈希键列表, 该列表可进一步用于遍历哈希键的值。
语法键%Hash
此外, Perl提供了两种方法来遍历哈希中的所有元素。
  1. Perl前言循环
  2. Perl而与循环每函数
# Perl program to demonstrate the # looping over a hash using its keys# creating a hash my %hash = ( 'MyVehicle' => 'Car' , 'Model' => 1234, 'Speed' => 60.7, # value type hash 'Traffic' => { 'Red' => 'Stop' , 'Yellow' => 'Look and move' , 'Green' => 'Go' }, # value type array 'AllVehicles' => [ 'Car' , 'Cycle' , 'Bus' , 'Auto' ]); # Case 1: When hash is not large # for loop to loop over the hash foreach my $key ( keys %hash ) {# do stuff $value = https://www.lsbin.com/$hash { $key }; print"Value of $key is $value\n" ; }# Case 2: When hash is very large # traversing the hash using "each" function while (( $key , $value ) = each ( %hash )) {# do stuff $value = https://www.lsbin.com/$hash { $key }; print"Value of $key is $value\n" ; }

输出如下:
Value of Model is 1234 Value of MyVehicle is Car Value of Traffic is HASH(0x1049f30) Value of AllVehicles is ARRAY(0x10661f8) Value of Speed is 60.7 Value of Model is 1234 Value of MyVehicle is Car Value of Traffic is HASH(0x1049f30) Value of AllVehicles is ARRAY(0x10661f8) Value of Speed is 60.7

在上面的示例中, 代码给出了数组第一个元素的地址以及分别存储在键" AllVehicles"和" Traffic"中的哈希。为了克服这个问题, 我们必须遍历数组内部并进行哈希处理。
例子
# Perl program to demonstrate the # looping over a multidimensional hash# creating a hash my %hash = ( 'MyVehicle' => 'Car' , 'Model' => 1234, 'Speed' => 60.7, # value type hash 'Traffic' => { 'Red' => 'Stop' , 'Yellow' => 'Look and move' , 'Green' => 'Go' }, # value type array 'AllVehicles' => [ 'Car' , 'Cycle' , 'Bus' , 'Auto' ]); # Loop over the key storing array my @array = @{ $hash { 'AllVehicles' }}; print "AllVehicles include \n" ; # for loop to loop over the array foreach my $ele ( @array ) { print "$ele\n" ; }# Loop over the key storing hash print "\nTraffic includes\n" ; # for loop to loop over the hash foreach my $val ( keys %{ $hash { 'Traffic' }}) { print "Key : $val, value : $hash{'Traffic'}{$val}\n" ; }

【Perl如何使用哈希运算(分析和示例)】输出如下:
AllVehicles include Car Cycle Bus AutoTraffic includes Key : Yellow, value : Look and move Key : Green, value : Go Key : Red, value : Stop

    推荐阅读