对Perl中哈希用法的总结

用胖箭头对哈希键值对进行赋值

  1. %some_hash = ('jim' => '20''tom' => '18''jerry' => '21'); 

将哈希转换为列表

  1. %any_hash = (k1,'v1',k2,'v2',k3,'v3',k4,'v4'); 
  2. @arr = %any_hash; 

keys函数, 将哈希中的键提取出来, 例如存放到数组@k中

  1. my @k = keys %any_hash; 

values函数,将哈希中的值提取出来,例如存放到数组@v中

  1. my @v = values %any_hash; 

each函数,将哈希中的键和值分别提取出来,例如分别返回键-值给$k, $v

  1. while (($k,$v) = each %any_hash){ 
  2.                 print "$k => $v\n"

exists函数,检查哈希中是否存在某个键, 有则返回真,否则返回假

  1. if (exists $any_hash{k1}){ 
  2.                 print "This key exists.\n"

delete函数, 删除哈希中的键

  1. delete $any_hash{k2}; 

 

[未完]