I can see plenty of example of how to read from a hash or add to a hash (using put), but how do I replace a current value? Any simple examples would be cool.
I haven't worked much with hashes, but you might look at Mike Grace's example here:
http://kynetxappaday.wordpress.com/2010/12/29/day-28-updating-users-list-when-user-joins-app/
That will replace the entire hash. I'm not sure how to replace just one value in the hash.
Related
I'm currently working on some code written by the previous internship student. I'm not familiar to Perl so I have some problems in understanding what his code actually do. So it looks like:
$Hash{Key1}{Key2}++;
The original code was:
$genotypes_parent2_array{$real_genotype}{$individu_depth}++;
I use to see hashes in this form $Hash{Key} in order to get the value but I struggle with this one. Any help out there ?
Thanks!
%genotypes_parent2_array is a hash (so that's not a very good name for the variable!) Each value in the hash is a hash reference. So effectively you have a hash of hashes.
$genotypes_parent2_array{$real_genotype} looks up the key $real_genotype in the hash. And that value is (as we said above) a hash reference. If you have a hash reference, then you can look up values in the referenced hash using an arrow. So we can get to a value in the second-level hash using code like this:
$genotypes_parent2_array{$real_genotype}->{$individu_depth}
However, Perl has a nice piece of syntactic sugar. When you have two pairs of "look-up brackets" next to each other (as we have here) you can omit the arrow. So you can get exactly the same effect with:
$genotypes_parent2_array{$real_genotype}{$individu_depth}
And that's what we have here. We look up the key $real_genotype in the hash %genotypes_parent2_array. This gives us a hash reference. We then look up the key $individu_depth in the referenced array and that gives us the value in the second-level hash. Your code then increments that value.
The manual page perldoc perldsc is a good introduction to using references to build complex data structures in Perl. In addition, I find Data::Dumper very useful for showing what a complex data structure looks like.
%Hash is a hash of hashes.
That codes add 1 to the value of $Hash{Key1}{Key2} which is the value of a hash element.
I am converting some Perl code to php, and I've stumbled on something, which I don't know for sure what it is.
if(!$continenttxt_cached{$savedcontinentid.'_'.$savedcountrygroupid})
What does the {} bracket do here? Is this a standard array element accessed this way? Because I am converting only a small part of a rather large code, I can't find how was $continenttxt_cached defined, so I can only presume this is an array. Is the {} used for something else in Perl?
{} in this context denotes a hash accessor - hashes are key-value pairs.
So you have a hash called %continenttxt_cached from which you're trying to extract the value associated with $savedcontinentid.'_'.$savedcountrygroupid
See perldata for more information.
I am working on writing a SHA512 function. When i check the file I am encrypting on different sources, a Linux SHA512SUM tool, a couple websites, and run it through the old source code i have for SHA512, they all give different hash values. My thought going into this project is that all Hash algorithms will output the same hash value if implemented correctly, to be used as a check sum. Am I wrong in thinking this? If I am wrong how would I really check to see if my work is correct?
Thanks in advance.
Yes, that's one of the basic building block of PKI: the same data block passed to a hash should always return the same hash value.
beware of the interpretation, though: the result of a SHA-2(512) hash is a block of 512 bits, not a string value so it will first be encoded for human consumption and it is therefore possible that you see what looks like visually different results when it's simply a matter of using different encodings.
I've been wondering how the CryptographyManager is able to compare a salted hash with the plain text. It has to save the salt for each hash somewhere, right? Has anyone any insight on this?
We ship source code. Take a look at CryptographyManagerImpl.cs in the Cryptography solution.
Also, you may want to review our unit tests - the ones that start with HashProvider should give you additional insight.
So I checked out the source code and it is actually quite trivial: The salt is prepended to the actual hash value. When the hash is compared to a plaintext the salt is extracted and used to hash the plaintext. These two hash values (= salt + hash) are then compared.
I'm looking for a special hash-function. Let's say I have a large list of strings, if I order them by their hash-values they should be ordered quasi randomly.
The most important point is: it must be super fast. I've tried md5 and sha1 and they're using to much cpu power.
Clashes are not a problem.
I'm using javascript, so it shouldn't be too complicated to implement.
Take a look at Murmur hash. It has a nice space/collision trade-off:
http://sites.google.com/site/murmurhash/
It looks as if you want the sort of hash function used in a hash table, not the sort used to detect duplicates or tampering.
Googling will yield you a wealth of information on alternative hash functions. To start with, stay away from cryptographic signature hashes (like MD-5 or SHA-1), they solve another problem.
You can read this, or this, or this, to start with.
If speed is paramount, you can implement a simple ad-hoc hash, e.g. take the first and last letter and order your string by the last and then first letter. The result would look, as you say, "quasi random" and it would be fast. For instance, part of my answer sorted that way would look like this:
ca ad-hoc
el like
es simple
gt taking
hh hash
nc can
ti implement
uy you
Hsieh, Murmur, Bob Jenkin's comes to my mind.
A nice page about hash functions that has some tests for quality and a simple S-box hash as well.