散列Hash当中主键Key与值value之间的交换
时间:2007-05-23 来源:白熊
这几天一直看O`Reilly Learning Perl一书,当看到散列Hash一章的时候,发现一个散列当中主键Key与值value之间交换的方法,以前自己没注意。它是这样描述的:
You can construct a hash with keys and values swapped using the reverse operator, which works well here:
%backwards = reverse %normal;
Of course, if %normal has two identical values, those will end up as only a single element in %backwards, so this is best performed only on hashes with unique keys and values. 我们可以用reverse操作符 来进行散列Hash当中主键与其对应的值的交换,也就是说原来散列的值是新散列的主键,而原来散列的主键变为新散列的值,并且需要注意的是,如果原来散列的值中有相同的,则新散列当中与其对应的主键只有一个。所以这是一个好的方法实现主键与值的唯一对应。 我不知道这种方法能应用的什么地方,但是感觉这是一个值得记录的地方,先写下来再说
%backwards = reverse %normal;
Of course, if %normal has two identical values, those will end up as only a single element in %backwards, so this is best performed only on hashes with unique keys and values. 我们可以用reverse操作符 来进行散列Hash当中主键与其对应的值的交换,也就是说原来散列的值是新散列的主键,而原来散列的主键变为新散列的值,并且需要注意的是,如果原来散列的值中有相同的,则新散列当中与其对应的主键只有一个。所以这是一个好的方法实现主键与值的唯一对应。 我不知道这种方法能应用的什么地方,但是感觉这是一个值得记录的地方,先写下来再说
相关阅读 更多 +