4 ms·
Hash arrays are great; I use them all the time in AWK. All searches using hash indexed arrays in AWK are O(1). I believe that the biggest problem around using
by Annatar 10y ago
Hash arrays are great; I use them all the time in AWK. All searches using hash indexed arrays in AWK are O(1).
I believe that the biggest problem around using hashes as indices entails people wrapping their head around the concept that the array index is a string (for all intents and purposes), rather than a number of a field in an array (or an address of a region in memory, which it eventually is anyway, once it runs). For example, I used entire lines of text as the hash for an array. My colleague who I was showing this technique to was completely dumbfounded as to how that worked. Why was I only storing the value "1" into Array["ORA-1234: blablabla"]?
if(Array[$0] != 1)
{
#
# This record is different, so print the entire record.
#
print;
}
"How is that used to filter out lines which are identical the ones we have in the canonical file?" He seemed completely flabbergasted. He just couldn't wrap his head around this concept, and ended up re-implementing the entire thing using several lines of grep -v, sed, and if [ ...] then. I think there were even several temporary files in the game, whereas the hash array technique in AWK loaded both the canonical file and the input from stdin into memory, rather than using intermediate files.