📜  珀尔 |哈希运算

📅  最后修改于: 2022-05-13 01:54:25.744000             🧑  作者: Mango

珀尔 |哈希运算

    先决条件: Perl 哈希,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 散列操作包括各种操作,这些操作对散列起作用以更有效地存储和检索数据。哈希中最常用的操作是:

    1. 访问散列中的键和值。
    2. 修改特定键的值。
    3. 循环到哈希。

    下面通过示例解释 Perl Hash 中的每个操作:

    查找/访问 Perl 哈希值或键

    查找或访问意味着能够访问散列的每个键/值对以进行进一步操作。 Perl 中的键以最佳方式存储 Hash 的元素,以便可以非常快速地获取其值。可以使用嵌入在{}括号之间的相应键名来查找哈希中的值。

    例子

    # 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 提供了修改和更新散列中的值的能力。对于给定的键,要修改或更新其对应的值,请使用以下语法:

      要修改给定键而不更改其对应值,只需创建一个附加键,即修改后的键并将值(您想要此新键的值)分配给该键,然后使用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()函数允许您以标量获取散列键的列表,该列表可进一步用于迭代散列的各个键的值。

    此外,Perl 提供了两种方法来遍历散列中的所有元素。

    1. Perl foreach循环
    2. Perl while循环每个函数
    # 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 is $value\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 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";
    }
    
    输出:
    AllVehicles include 
    Car
    Cycle
    Bus
    Auto
    
    Traffic includes
    Key : Yellow, value : Look and move
    Key : Green, value : Go
    Key : Red, value : Stop