Arrays and hashes.pl

From JaxHax
Jump to: navigation, search

arrays_and_hashes.pl Overview

This program is a simple program that provides an example of how to use arrays and hashes by setting them, accessing them, and resetting them.


arrays_and_hashes.pl Code

Save the code below in a file called arrays_and_hashes.pl:

#####################################################
#
# Example: arrays_and_hashes.pl
#
# This example introduces how to use arrays
# We covered variables earlier. Arrays work similar
# however start with a @ instead of a $. An array is 
# used when you need to store more than one object.
# these can be useful when we need to work with an
# unknown amount of user objects.
#
# Objects in this array are accessed with an "index".
# an index starts at zero and increments down the
# the row.
#
#####################################################
 
###
#### Here we are going to set an Array named members.
#### It will store the names of 3 members in it. which
#### will be indice 0-2.
###
@members = ("Travis", "Dan", "Jerry");
 
###
#### If we want to. We can have perl just shit the entire
#### array out to the screen like so.
###
print "\nDumping Array...\n\n @members \n\nFinished!\n\n";
 
###
#### However we can also access these values by their index.
#### This will act similar to a variable. We will call the 
#### array with a $ instead of an @ and at the end use a "[x]"
#### where the x is the number of the index. Bare that in mind
#### and try to keep VARIABLE AND ARRAY NAMES DIFFERENT FROM
#### EACH OTHER.
###
print "\nThe member who created this tutorial: $members[0]\n";
print "The Unix admin of Jax Hax: $members[1]\n";
print "The Friday Night Magic creater of Jax Hax: $members[2]\n\n";
 
###
#### Hashes are pretty similar to arrays with a few exceptions.
#### One is instead of using "@" we use "%" when naming it. The
#### Other thing that is nice about hashes it you can name your
#### indice with friendly names. This can be useful for several
#### reasons. if the hash value is the same as the hash index,
#### you can use this to get rid of duplicates since we use the 
#### value as the index, the second time around it would just update
#### it instead of adding it in again, which is great for data scraping. 
###
%coder = ("Author" => "Travis", "Script" => "arrays_and_hashes.pl");
 
###
#### So the first value is a named index that "points to" the value which
#### are comma seperated. Notice that we also use curly brackets rather than
#### square brackets when accessing it in the below print statement.
###
print "The script $coder{'Script'} was written by $coder{'Author'}\n";
 
###
#### Values can be changed in an array or hash assigning the same way you
#### access it, Specify the index and use an equal sign followed by it's 
#### value. Below we will change it.
###
$coder{'Script'} = "Nmap";
$coder{'Author'} = "Fydor";
print "But the script $coder{'Script'} was written by $coder{'Author'}\n\n";

Running arrays_and_hashes.pl from a Shell

[user@localhost]$ perl arrays_and_hashes.pl
Dumping Array...
 
 Travis Dan Jerry 
 
Finished!
 
 
The member who created this tutorial: Travis
The Unix admin of Jax Hax: Dan
The Friday Night Magic creater of Jax Hax: Jerry
 
The script arrays_and_hashes.pl was written by Travis
But the script Nmap was written by Fydor
 
[user@localhost]$


Code Breakdown

At the beginning of our program we have this line:

@members = ("Travis", "Dan", "Jerry");

This line sets an array called "members" using the "@" at the beginning which indicates it is an array. We provide it values it takes inside "()" with each value comma seperated. After we set it up we show that we can print the entire array using the "@members" in the print command. Next we have 3 print statements that access the values separately using the "$members[#]" approach.

print "\nThe member who created this tutorial: $members[0]\n";
print "The Unix admin of Jax Hax: $members[1]\n";
print "The Friday Night Magic creater of Jax Hax: $members[2]\n\n";

If we access an array with a $ instead follow with square brackets at the end of it ("[]"), we can access the values one at a time using the index number. The index numbers of an array start at zero. This is important to remember as it is a common issue that people calulate this off by one due to this issue.


Next we declare a hash to give an example of those. Indice in hashes are called keys. They are like arrays except they have keys which are named. This can make your code a lot more readable and is also a good way to prevent duplicate data when scraping data say via a web spider. When we declare a hash it will use % instead of the @ symbol we use with an array.

%coder = ("Author" => "Travis", "Script" => "arrays_and_hashes.pl");

Now if you notice in our declaration in the () we have a value, an arrow "=>", and another value, followed by a comma. The first value is the name of the key value, the second value is the value that is stored at that key. So in this one, we are creating two keys called "Author" and "Script" which "point" to the values "Travis" and "arrays_and_hashes.pl" respectively. Next we demostrate how to print the values out using the $coder variable from the %coder hash.

print "The script $coder{'Script'} was written by $coder{'Author'}\n";

Notice how instead of the using the square brackets [] like we did in the array values we now use curly brackets {} with the friendly key name. This is specific to hashes.


Next we decide to change those values one at a time, it's easy to change them just like how we print them.

$coder{'Script'} = "Nmap";
$coder{'Author'} = "Fydor";

Lastly we just simply print it again to confirm that the values were updated successfully.