I hope this overview helps starters understanding some of the logic behind all those terms.
I. Bits, bytes and memory
What is this:
0
1
2
3
4
A bunch of numbers, you say? Well, yes, but the computer doesn't care about the type of data, internally everything (a number, a letter, a sound, a color, ...) is represented by switches that have the state 'on' or 'off', and all those switches together are called RAM (Random Access Memory). Such a switch is called a 'bit', and the states are referred to as the bit being set or not set. One bit represents 2 states, which is rather limiting. We could say, that the bit not set stands for the number 0 and the bit set for the number 1. But how would we then represent the number 2 or 3 or 4, etc?
The answer is: by combining several bits. This grouping of bits is so essential, that there are names for all kinds of grouped bits. For example, the combination of 8 bits is called 'byte', the first 4 bits of such a byte are called 'low nibble', the other 4 are called 'high nibble'. To differentiate between those 8 bits of a byte, every bit is given a "weight", a unique valence based on the position of a bit within a byte. The power of two valency fits prefectly, because every bit represents two states.
8 7 6 5 4 3 2 1 position of the bit
7 6 5 4 3 2 1 0 the power of two represented by the bit if set[/code:1e7v91ps]
To represent 0, we just don't set any bit:
00000000
To represent 1, we set the first bit, because 2^0 = 1:
0000000X
To represent 2, well, you guess:
000000X0
3 is represented by setting bit 1 and bit 2, that's 2^0 + 2^1 = 3:
000000XX
Now that we grouped bits to represent numbers, how do we tell the processor, what number we want to access? By giving every byte an address, the place (or position), where in our RAM the byte is stored. But it would be very annoying to keep all those addresses in mind. We replace those addresses with something that we can use more naturally.
[b]II. Constants and variables[/b]
When we type '1 + 2', then we are typing two numbers, right? No, we type two addresses. Fortunately they lead the processor to the place in memory, where the numbers 1 and 2 are stored. Isn't that clever? So, the address '1' always leads to the same place in memory, where the number 1 is stored, this will never change. [i]An address that is used this way is called a 'constant'.[/i]
myNumber = 1
myNumber = 2
What is happening here? We reserved a place in memory for our own use and gave the address a name we can remember: 'myNumber'. Then we ordered to copy the content of the constant '1' to our address. But in the next line we copy the content of the constant '2' to our address. The content of 'myNumber' changes. [i]An address that is used that way is called a 'variable'.[/i]
Constants and variables are the base of programming. The number of hearts collected in a game changes as we play, so we use a variable. The speed of a car changes as we play: variable. But what, if we need to keep track of the speeds of 100 cars? We could create 100 variables and give them names like 'car1', 'car2', etc. But that is a very tedious task. Luckily, there's a shortcut.
[b]III. Arrays[/b]
We need 100 variables, but they all refer to the same aspect in our game, in this case the speed of the cars. Wouldn't it be much easier to enumerate the cars while accessing all of the variables with the same name? We would know, that speed(1) refers to the speed of the first car, while speed(2) refers to the second car, etc. That's the purpose of an array. It is grouping a greater number of variables and knows their addresses, and the array itself also has an address. When we access the address of the array, 'speed', together with an index, '(1)', the array looks for the variable at the address that is stored at the index.
[i]You should use an array, whenever you need to manage a lot of variables for the same purpose.[/i] You make an oldschool adventure with a parser understanding hundreds of words? Use an array to store those words! You have a map editor to place different objects on fixed positions? Use an array!
As good as arrays are, there is a disadvantage. Imagine another task filling our array with data. Worst of all, it fills the array differently everytime. For example, the cars only add their speeds to the array, if a certain value is reached. Now car 5 may be the first one to store its speed, car 9 the second one, etc. If we then access speed(1), we simply don't know to what car this speed variable belongs to. Don't worry, a solution to our problem is already existent.
[b]IV. Hash tables[/b]
To know exactly what variable refers to the car's speed we are interested in, we need something unique to identify it. We could use simple variables again, but that would make our project very complex. Also we have still a group of variables representing the same aspect, the speeds of the cars. Something like "speed('car1')" instead of "speed(1)" would help a lot here.
But what is speed('car1')? Nothing more than an address to an address to a variable. With this indirect addressing it isn't important anymore, at what exact positions the speeds are stored. The address of the speed of the first car may be stored at index 1 or at index 5, we access it by a name and the functionality behind it finds the right index to our name. That's what a hash table does. [i]Think of a hash table as an array with a clever index aliasing on top.[/i]
The name we use is called a 'key'. This key will always lead us to the correct variable, no matter where exactly it is stored in the array.
You should use a hash table whenever you have to manage a lot of variables representing the same aspect of something with changing count or content and still need to access them quickly.