Only the keys of a hash must be unique. Its values may repeat.Printing the reverse of a hash does not reverse keys and values in the hash. For that to happen you must re-assign the reversed data back to the hash:
%hash = reverse %hash;
The order in which a hash "prints" (or the order of the result of the keys, values or each function) is not random but determined by the internal hashing algorithm. For security reasons the order is likely to change between executions of your script although it is guaranteed to be the same order within one run of your script on the same (unmodified) hash.So you cannot easily predict the order. If you need the keys (or values) ordered, the best way is to use sort.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] [select] |
| [reply] |
A hash in list context is just an even-sized list, so reverse %hash is also just a list. If you want to remove duplicates, you have to assign the result to an array again. | [reply] [d/l] |
What I expect is, the hash won't print duplicate value. ( b is a duplicate ).
Hashes do not have duplicate keys; hash values may be any scalar, duplicated or not.
Also I believe, the hash will print the value in the same order (not random and not sort by default).
The 'order' of keys or values in a hash varies from run to run of a Perl script. See the section on Algorithmic Complexity Attacks in perlsec. Also see the discussion on apparent order in the docs for keys, values and each (the paragraph is the same in each case).
| [reply] |
| [reply] |