Differences
This shows you the differences between two versions of the page.
Both sides previous revision
Previous revision
|
|
notes:perl_cheat_sheet [2013/09/22 19:27] smthng [Hashes] |
notes:perl_cheat_sheet [2013/09/22 19:28] (current) smthng [Hashes] |
* ''%revhash = reverse %hash'' to reverse a hash (for non-unique values, last one wins). | * ''%revhash = reverse %hash'' to reverse a hash (for non-unique values, last one wins). |
* ''keys %hash;'' and ''values %hash;'' return a list of keys or value in same order (or # of keys/values in scalar context). | * ''keys %hash;'' and ''values %hash;'' return a list of keys or value in same order (or # of keys/values in scalar context). |
* '' %hash' is true only if hash has at least one key-value pair. | * '' %hash'' is true only if hash has at least one key-value pair. |
* To iterate over hash : <code perl>while ( ($key, $value) = each %hash ) { | * To iterate over hash : <code perl>while ( ($key, $value) = each %hash ) { |
print "$key => $value\n"; }</code> or in order of keys <code perl>foreach $key (sort keys %hash) { | print "$key => $value\n"; }</code> or in order of keys <code perl>foreach $key (sort keys %hash) { |