📜  珀尔 |删除()函数

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

珀尔 |删除()函数

Perl 中的Delete()用于从散列中删除指定的键及其关联值,或者在数组的情况下删除指定的元素。此操作仅适用于单个元素或切片。

示例 1:在 Hash 上实现 delete()

#!/usr/bin/perl
  
# Initializing hash 
%hash1 = ('Geeks' => 45, 'for' => 30, 'Now' => 40); 
  
# To delete the List passed as parameter
$deleted_element = delete($hash1{'for'});
  
# Printing elements of Hash 
print "$hash1{'Geeks'}\n"; 
print "$hash1{'for'}\n"; 
print "$hash1{'Now'}\n"; 
  
# Printing the deleted element
print "Deleted element: $deleted_element";

输出:

45

40
Deleted element: 30

示例 2:在数组上实现 delete()

#!/usr/bin/perl
  
# Initializing array
@array1 = (10, 20, 30, 40, 50, 60); 
  
# To delete the array element at index 2
$deleted_element = delete(@array1[2]);
  
# Printing elements of Array 
print "Updated Array: @array1";
  
# Printing the deleted element
print "\nDeleted element: $deleted_element";

输出:

Updated Array: 10 20  40 50 60
Deleted element: 30