Vous êtes sur la page 1sur 6

print "enter ur age\n";

$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>";

# EXECUTE THE WHILE LOOP


while (($key, $value) = each(%coins)){
print "<tr><td>".$key."</td>";
print "<td>".$value."</td></tr>";
}
print "</table>";
#########################
# FOREACH LOOP
foreach $key (sort keys %coins) {
print "$key: $coins{$key}<br />";
}
#########################
#Sort values
# DEFINE A HASH
%coins = ( "Quarter" , .25,
"Dime" , .10,
"Nickel", .05 );
# FOREACH LOOP
foreach $value (sort {$coins{$a} cmp $coins{$b} }
keys %coins)
{
print "$value $coins{$value}<br />";
}
#########################
#Add Elements
# BEGINNING HASH
%coins = ( "Quarter" , .25,
"Dime" ,
.10,
"Nickel", .05 );
# PRINT THE OLD HASH
while (($key, $value) = each(%coins)){
print $key.", ".$value."<br />";
}
# ADD NEW ELEMENT PAIRS
$coins{Penny} = .01;
$coins{HalfDollar} = .50;
# PRINT THE NEW HASH
print "<br />";
while (($key, $value) = each(%coins)){
print $key.", ".$value."<br />";
}
######################
# remove elements
# DEFINED HASH
%coins = ( "Quarter" , .25,
"HalfDollar" ,
.50,
"Penny" ,
.01,
"Dime" ,
.10,
"Nickel", .05 );
# PRINT OLD HASH

while (($key, $value) = each(%coins)){


print $key.", ".$value."<br />";
}
# DELETE THE ELEMENT PAIRS
delete($coins{Penny});
delete($coins{HalfDollar});
# PRINT THE NEW HASH
print "<br />";
while (($key, $value) = each(%coins)){
print $key.", ".$value."<br />";
}
#########################################
#
Logical Operators
#########################################
#PICK A NUMBER
$x = 81;
$add = $x + 9;
$sub = $x - 9;
$mul = $x * 10;
$div = $x / 9;
$exp = $x ** 5;
$mod = $x % 79;
print "$x plus 9 is $add<br />";
print "$x minus 9 is $sub<br />";
print "$x times 10 is $mul<br />";
print "$x divided by 9 is $div<br />";
print "$x to the 5th is $exp<br />";
print "$x modulus 79 is $mod<br />";
##########################################
#
Logical & Relational Operators
##########################################
#TWO STRINGS TO BE ADDED
$myvariable = "Hello,";
$Myvariable = " World";
#ADD TWO STRINGS TOGETHER
$string3 = "$myvariable $Myvariable";
print $string3;
#########################################
#
Conditional If statements
#########################################
# SOME VARIABLES
$x = 7;
$y = 7;
# TESTING...ONE, TWO...TESTING
if ($x == 7) {
print '$x is equal to 7!';
print "<br />";
}
if (($x == 7) || ($y == 7)) {

print '$x or $y is equal to 7!';


print "<br />";
}
if (($x == 7) && ($y == 7)) {
print '$x and $y are equal to 7!';
print "<br />";
}
#########################################
#
If else statements
#########################################
# SOME VARIABLES
$name = "Sarah";
$x = 5;
# IF/ELSE STATEMENTS
if ($x > 10) {
print "$x is greater than 10!";
} else {
print "$x is not greater than 10!";
}
print "<br />";
# STRINGS ARE A LITTLE DIFFERENT
if ($name eq "Sarah") {
print "Hello, $name!";
} else {
print "You are not $name!";
}
############################################
#
Else if
############################################
# SOME VARIABLES
$x = 5;
# PLAY THE GUESSING GAME
if ($x == 6) {
print "X must be 6.";
}
elsif ($x == 4) {
print "X must be 4.";
}
elsif ($x == 5) {
print "X must be 5!";
}
###############################################
#
FOR loop
###############################################
# SET UP THE HTML TABLE
print "<table border='1'>";
# START THE LOOP, $i is the most common counter name for a loop!
for($i = 1; $i < 5; $i++) {
# PRINT A NEW ROW EACH TIME THROUGH W/ INCREMENT
print "<tr><td>$i</td><td>This is row $i</td></tr>";
}
# FINISH THE TABLE
print "</table>";

###############################################
#
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);
}

Vous aimerez peut-être aussi