Introduction to Programmin
Written by Rusty NejdlVariables
Variables are simply a piece of memory on a computer that is used to indicate a value that changes. For example, suppose that we had a variable used to indicate your age. It would change in value once a year. That is, if we called this variable $age, it would increase in value 1 at each birthday you have.
There are three main types of variables that we will be concerned with as they pertain to both Perl and PHP. Other programming languages use many more.
Scalars
A scalar can contain any single number or letter combination. When it contains a number, it can be treated mathematically, such as adding numbers to it or dividing it by the number 2. When this variable contains a combination of letters, then it is treated differently. It can have new letters attached to it or have the spaces removed from it.
Scalars generally use the $ character to denote that they are a scalar variable and appear in the form $variable.
Array
An array is simply a group of strings. Arrays can have an infinite size of contents depending only on the size of memory on the computer. Each item in the array can be retrieved and manipulated as it is was a string. The first item in an array is called item 0.
Arrays use the @ symbol to denote that the variable is an array and appear in the form @variable. Within PERL, to refer to the first item in that array, you can use the format of $variable[0].
Hash
A hash is similar to an array in that it is a container for scalars. However, whereas arrays are indexed by numbers, hashes are indexed by whatever you want, typically by other strings. For example, supposed we need to keep track of each trainer and how old they are. To do this using arrays would require 2 arrays that were used side by side. With a hash, we can have a data pair for each trainer and their age.
Order in hashes is maintained by the program, so the data you put into a hash may not come out in the same order. Because of this, hashes are a much more efficient use of memory.
Math functions
Most programming languages have support for a large range of mathematical functions. These look almost identical to any mathematics you took in high school for all the basics.
+ Add - Subtract * Multiply / Divide % Modulus ^ To the power of abs Absolute value Cos Cosine sin Sine tan Tangent Rand Returns a random value sqrt Square root Other functions are available, but this is just a basic list to get you going.
Loops
Loops are used to run a set of code multiple times. This can be done to go through an array until all of its contents have been used, to go through information within database, or to repeat print statements.
For
For loops are used to repeat through a set of data a given number of times. They are generally formatted something like this:
for ($i=0;$i<5;$i++) { insert code here }This would run the given code 5 times with I being 0,1,2,3, and 4 for the five repetitions.
While
While loops are generally used so that a loop will run while there is still data available. A common example is to read in the contents of a file and do some processing on each line of that file until there are no lines left. This is generally seen as:
while ($line) { insert code here }This would simply continue while $line contains any data.
Foreach
This is PERL's special (and very handy!) looping structure. It receives a list, either an array or a hash, and loops once for each item in the list. On each pass, it optionally places the current list item in $var. The structure is:
foreach my $item (@list) { stuff you want it to do for each item }Unless
While and unless are virtually identical. However, unless is simply the negative version of while. While will run as long as a variable is true. Unless will run until the variable is true. Using the operator "!=", not equals, however can make these function identically.
Until
The until is great for a program that needs to sit in the back ground and check for something to happen. If I need a loop to run until it sees an event happen, such as an intrusion, then I could have an until loop repeating every 5 seconds until that intrusion happened and then execute whatever code I wanted to happen. For example:
until ($processed == 5) { shutdown now! } else { sleep for 5 seconds; }If/Then/Else/Elsif
If/then/else statements are the bread and butter of any programming. It works this way:
- If such and such is happening, then do this.
- But, instead, if this is happening, then do this.
- If all else fails, do this.
In programming jargon, this would like this:
if ($age>21) { Allow person to consume alcohol; } elsif ($age == 21) { Heckle the person; } elsif ($cute ==yes) { Flirt; } else { Kick the person out; }