Perl 哈希操作
正如大多数读者可能知道的那样, 散列 通过使用一种叫做 Hashing的 机制来存储数据 。 在散列中,一个键被用来确定一个值或数据。这些键必须是 唯一的 ,然后被用作索引,与该键相关的数据被存储在该索引中。这个数据不一定是唯一的。它可能是重复的。
散列是一种数据结构,通过维护键和值 或键/值对 之间的关系来存储数据 。 给定一个键,我们可以找到它的值。键/值对是可变的对象,因此,只要你想,它们就可以被改变。散列中的键可以进一步定义为一个用于检索值的对象。与数组相比,使用哈希的主要优点是,哈希允许基本操作的执行时间保持不变,例如,在一个特定的键(在数组的情况下是索引)上获取一个值或设置一个值,即使是大型数据集。
请注意,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的数据类型是数组。因此,输出是哈希和数组的第一个元素的地址。
此外,许多操作或操作可以在散列上进行,下面将解释。
对哈希的操作
Perl的哈希操作包括各种操作,对哈希进行操作可以更有效地存储和检索数据。散列中最常用的操作是 :
- 访问散列中的键和值。
- 修改特定键的值。
- 循环到散列。
下面用例子解释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;
要修改一个给定的键而不改变其相应的值,只需创建一个额外的键,即被修改的键,并为这个键赋值(你想要这个新键),然后使用delete关键字删除之前的键/值对。
示例 :
# 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()函数允许你在标量中获得一个哈希键的列表,可以进一步用来迭代哈希键的值。
语法 keys %Hash
此外,Perl提供了两种方法来循环处理哈希中的所有元素。
- Perl的foreach循环
- Perl的while循环与each函数
# 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 = hash{key};
print "Value of key isvalue\n";
}
# Case 2: When hash is very large
# traversing the hash using "each" function
while((key,value) = each (%hash))
{
# do stuff
value =hash{key};
print "Value ofkey 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 myele (@array)
{
print "ele\n";
}
# Loop over the key storing hash
print "\nTraffic includes\n";
# for loop to loop over the hash
foreach myval(keys %{hash{'Traffic'}})
{
print "Key :val, value : hash{'Traffic'}{val}\n";
}
输出:
AllVehicles include
Car
Cycle
Bus
Auto
Traffic includes
Key : Yellow, value : Look and move
Key : Green, value : Go
Key : Red, value : Stop