Académique Documents
Professionnel Documents
Culture Documents
$age = <>;
print "enter ur fav colour\";
$fav_colour = <>;
print "my age is $age and my fav colour is $fav_colour\n";
########################
#Arrays
########################
# DEFINE AN ARRAY
@coins = (Quarter,Dime,Nickel);
print "@coins";
print $coins[1];
print $coins[-2];
#############################
#Shortcuts
#############################
SHORTCUTS SAVE TIME
@10 = (1 .. 10);
@abc = (a .. z);
print "@10<br />";
print "@abc<br />";
################
#Scalar Function
################
@nums = (1 .. 20);
@alpha = ("a" .. "z");
SCALAR FUNCTION
#print scalar(@nums)."<br />";
#print scalar(@alpha)."<br />";
# REDEFINE TO SCALAR
$nums = @nums;
$alpha = @alpha;
print "There are $nums numerical elements<br />";
print "There are ".scalar(@alpha)." letters in the alphabet!";
##########################
# Define hash
##########################
%coins = ("Quarter", 25, "Dime", 10, "Nickel", 5);
print %coins;
#######################
# LOOP THROUGH IT
while (($key, $value) = each(%coins)){
print $key.", ".$value."<br />";
}
#######################
# SET UP THE TABLE
print "<table border='1'>";
print "<th>Keys</th><th>Values</th>";
###############################################
#
For each loop
###############################################
print "content-type: text/html \n\n"; #The header
# SET UP THE HTML TABLE
print "<table border='1'>";
# CREATE AN ARRAY
@names = qw(Steve Bill Connor Bradley);
# SET A COUNT VARIABLE
$count = 1;
# BEGIN THE LOOP
foreach $names(@names) {
print "<tr><td>$count</td><td>$names</td></tr>";
$count++;
}
print "</table>";
###############################################
#
WHILE
###############################################
# SET A VARIABLE
$count = 0;
# RUN A WHILE LOOP
while ($count <= 7) {
# PRINT THE VARIABLE AND AN HTML LINE BREAK
print "$count<br />";
# INCREMENT THE VARIABLE EACH TIME
$count ++;
}
print "Finished Counting!";
###############################################
#
While flow
###############################################
# SET A VARIABLE
$count = 0;
while ($count <= 7) {
# SET A CONDITIONAL STATEMENT TO INTERRUPT @ 4
if ($count == 4) {
print "Skip Four!<br />";
next;
}
# PRINT THE COUNTER
print $count."<br />";
}
continue {
$count++;
};
print "Loop Finished!";
###############################################
#
while array
###############################################
# SET UP AN HTML TABLE
print "<table border='1'>";
# DEFINE AN ARRAY
@names = qw(Steve Bill Connor Bradley);
# COUNTER - COUNTS EACH ROW
$count = 1;
# COUNTS EACH ELEMENT OF THE ARRAY
$n = 0;
# USE THE SCALAR FORM OF ARRAY
while ($names[$n]) {
print "<tr><td>$count</td><td>$names[$n]</td></tr>";
$n++;
$count++;
}
print "</table>";
#################################################
#
Do while
#################################################
$count = 10;
do {
print "$count ";
$count--;
} while ($count >= 1);
print "Blastoff.\n";
#################################################
#
Subroutine
#################################################
$x = Three(); ## call to Three() returns 3
print $x;
exit(0); ## exit the program normally
sub Three {
return (1 + 2);
}