shadowline
Arrays << Hashes >> Conditionals
Hashes are very similar to an arrays. A hash is an array of pairs. It is often called an "associative array".

Some differences between a hash and array :
- A hash uses the % symbol instead of the @ symbol.
- Arrays use 0 to whatever number as an index. Hashes are keys and value pairs.
  key, element, key, element, and so on...
- Hashes use the curly brackets {} instead of the square brackets [] to find individual elements.

Here is a quick example of what a hash may do :
%chores = ("Monday", "dishes", "Tuesday", "vacuum", "Wednesday", "garbage");
$value = $chores{Wednesday};
print "$value";

The example has 3 days and 3 chores values. The data is setup in "key","value" pairs. The script will know this is a hash instead of an array because of the % symbol.
We are using a scalar variable to find the result since we are looking for a single piece of data. The script searches for the specified {key} and assigns the corresponding value of that key to the scalar variable.

The output of the example script is : garbage

See it in action!

Hashes are very adaptable when it comes to data holding just like arrays. They are able to contiain regular data (as seen above), scalar variables, other hashes, arrays, and so on...

Printing hash data is different from printing array data. Actually it takes an array to help print a hash. Take this line for example :
print "%chores";

Now if that was an array, you would be expecting all of the data to be printed out. Well, this is a hash so all it will print out is the actual word... %chores. PERL does not consider the hash to be an actual variable, so it displays the result as normal text.

So to print out all of the elements contained in a hash, keys and values, they will have to be put into an array first.
@array=%chores;
print "All of the elements stored in %chores are @array \n";

It probably won't be often when you want all of the elements in a hash to be printed. Here are some examples of other ways to work with hashes.

To find the KEY elements in a hash and store them in an array :
@array = keys(%chores);

The word keys above is actually the word... keys. It is not special for the example like the array name. The array in most of these examples can be called whatever you like.

To find all of the VALUE elements in a hash and store them in an array :
@array = values(%chores);

The word values above is actually the word... values.

To find EACH of the key/value pairs in a hash :
%hash1=(a,1,b,2,c,3,d,4);
while (@temp=each(%hash1)){
print "The key value pair is @temp <br>";
}
- The hash1 is set with values.
- While the value remains true, keep looping.
- @temp is used to temporarily hold two values.
- The EACH command pulls out two pairs of values in each loop and goes to the next two upon the next loop.
- The PRINT command displays the information stored in the temp array during each loop.

To delete a specific key and corresponding value :
delete $hash{$key};
Arrays << Hashes >> Conditionals
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow