1.
Hello, World! ^
# This
is my first Perl program, called p1_1.pl
# It's just a simple perl program.
# It's made on Nov. 24, 1996 by Tony Zhang.
$mystring = "Hello, World!";
print ($mystring); # print a string to screen
Några påpekanden:
De tre första raderna utgörs av
kommentarer. #-tecknet talar om för perlkompilatorn att
efterföljande tecken fram till radslutet skall ignoreras
vid programkörningen. Du kan använda kommentarer för
att förtydliga programmen för dig själv och andra. Man
kan även lägga in kommentarer i slutet av rader som
innehåller vanliga perlsatser. Ett exempel på detta ser
du på rad 5.
På rad 4 tilldelas variabeln
$mystring
2.
Standard input ^
# This is my second Perl program, called p1_2.pl
# It's just a simple perl program.
# It's made on Nov. 24, 1996 by Tony Zhang.
$mystring = <STDIN>;
print ($mystring); # print a string to screen
3.
Omvandla engelska mil till kilometrar och tvärtom ^
# p2_1.pl
print ("Enter the distance to be
converted:\n");
$originaldist = <STDIN>;
chop ($originaldist);
$miles = $originaldist * 0.6214;
$kilometers = $originaldist * 1.609;
print ($originaldist, " kilometers = ", $miles,
" miles\n");
print ($originaldist, " miles = ", $kilometers,
" kilometers\n");
4. Exempel på if-sats
# p2_2.pl
print ("Enter a number:\n");
$number = <STDIN>;
chop ($number);
if ($number) {
print ("The number is not zero.\n");
}
print ("This is the last line of the
program.\n");
5. Likhet mellan två tal
# p2_3.pl
print ("Enter a number:\n");
$number1 = <STDIN>;
chop ($number1);
print ("Enter another number:\n");
$number2 = <STDIN>;
chop ($number2);
if ($number1 == $number2) {
print ("The two numbers are equal.\n");
}
print ("This is the last line of the
program.\n");
6. Exempel med if
else
# p2_4.pl
print ("Enter a number:\n");
$number1 = <STDIN>;
chop ($number1);
print ("Enter another number:\n");
$number2 = <STDIN>;
chop ($number2);
if ($number1 == $number2) {
print ("The two numbers are equal.\n");
} else {
print ("The two numbers are not equal.\n");
}
print ("This is the last line of the
program.\n");
7. If
elsif
else
# p2_5.pl
print ("Enter a number:\n");
$number1 = <STDIN>;
chop ($number1);
print ("Enter another number:\n");
$number2 = <STDIN>;
chop ($number2);
if ($number1 == $number2) {
print ("The two numbers are equal.\n");
} elsif ($number1 == $number2 + 1) {
print ("The first number is greater by
one.\n");
} elsif ($number1 + 1 == $number2) {
print ("The second number is greater by
one.\n");
} else {
print ("The two numbers are not equal.\n");
}
print ("This is the last line of the
program.\n");
8. Exempel med while
# p2_6.pl
$done = 0;
$count = 1;
print ("This line is printed before the loop
starts.\n");
while ($done == 0) {
print ("The value of count is ", $count,
"\n");
if ($count == 3) {
$done = 1;
}
$count = $count + 1;
}
print ("End of loop.\n");
9. Exempel med until
# p2_7.pl
print ("What is 17 plus 26?\n");
$correct_answer = 43; # the correct answer
$input_answer = <STDIN>;
chop ($input_answer);
until ($input_answer == $correct_answer) {
print ("Wrong! Keep trying!\n");
$input_answer = <STDIN>;
chop ($input_answer);
}
print ("You've got it!\n");
10. Omvandla mellan versaler och gemener
# p3_4.pl
print ("Enter a line of input:\n");
$inputline = <STDIN>;
print ("uppercase: \U$inputline\E\n");
print ("lowercase: \L$inputline\E\n");
print ("as a sentence: \L\u$inputline\E\n");
11. Exempel med operatorn för potenser
# p4_1.pl
# this program asks for a number, n, and prints 2 to the
# exponent n
print ("Enter the exponent to use:\n");
$exponent = <STDIN>;
chop ($exponent);
print ("Two to the power $exponent is ",
2 ** $exponent, "\n");
12 .Inkrementering med ++
# p4_4.pl
$value = 0;
while ($value++ <= 5) {
print("value is now $value\n");
}
print("all done\n");
13 .Konkatenering av två strängar
# p4_5.pl
$resultstring = "";
print("Enter your input -- type an empty line to
quit\n");
$input = <STDIN>;
chop ($input);
while ($input ne "") {
$resultstring .= $input;
$input = <STDIN>;
chop ($input);
}
print ("Here is the final string:\n");
print ("$resultstring\n");
14. Testa ett password
# p4_6.pl
print ("Enter the secret password:\n");
$password = "bluejays";
$inputline = <STDIN>;
chop ($inputline);
$outputline = $inputline eq $password ?
"Yes, that is the correct password!\n" :
"No, that is not the correct password.\n";
print ($outputline);
15. Arrayer -utskrift av en listas element
# p5_1.pl
@array = (1, "chicken", 1.23,
"\"Having fun?\"", 9.33e+23);
$count = 1;
while ($count <= 5) {
print ("element $count is $array[$count-1]\n");
$count++;
}
16. Slumptalsfunktionen (med fast frö)
# p5_2.pl
# collect the random numbers
$count = 1;
while ($count <= 100) {
$randnum = int( rand(10) ) + 1;
$randtotal[$randnum] += 1;
$count++;
}
# print the total of each number
$count = 1;
print ("Total for each number:\n");
while ($count <= 10) {
print ("\tnumber $count:
$randtotal[$count]\n");
$count++;
}
17. En kort sätt att skriva in en heltal
i en följd till en array
# p5_3.pl
print ("Enter the start number:\n");
$start = <STDIN>;
chop ($start);
print ("Enter the end number:\n");
$end = <STDIN>;
chop ($end);
@list = ($start..$end);
$count = 0;
print ("Here is the list:\n");
while ($list[$count] != 0 || $list[$count-1] == -1 ||
$list[$count+1] == 1) {
print ("$list[$count]\n");
$count++;
}
18. Kopiering av en array till en annan
# p5_4.pl
@array1 = (14, "cheeseburger", 1.23, -7,
"toad");
@array2 = @array1;
$count = 1;
while ($count <= 5) {
print("element $count: $array1[$count-1] ");
print("$array2[$count-1]\n");
$count++;
}
19. En array tilldelas en annan array
# p5_5.pl
@innerlist = " never ";
@outerlist = ("I", @innerlist,
"fail!\n");
print @outerlist;
20. Utskrift av en array element för
element
# p5_6.pl
@array = (14, "cheeseburger", 1.23, -7,
"toad");
$count = 1;
while ($count <= @array) {
print("element $count: $array[$count-1]\n");
$count++;
}
21. Array slices
# p5_7.pl
@array = (1, 2, 3, 4);
@subarray = @array[1,2];
print ("The first element of subarray is
$subarray[0]\n");
print ("The second element of subarray is
$subarray[1]\n");
22. Inläsning från STDIN i array
# p5_12.pl
@array = <STDIN>;
print (@array);
23. Sortering av en array i omvänd
ordning
# p5_13.pl
@input = <STDIN>;
@input = reverse (sort (@input));
print (@input);
24. Innehållet i en array bakas ihop till
en sträng
# p5_14.pl
@input = <STDIN>;
chop (@input);
$string = join(" ", @input);
print ("$string\n");
25. Ett ordräkningsprogram
# p5_15.pl
$wordcount = 0;
$line = <STDIN>;
while ($line ne "") {
chop ($line);
@array = split(/ /, $line);
$wordcount += @array;
$line = <STDIN>;
}
print ("Total number of words: $wordcount\n");
26. Ett program som läser textrader från
en fil och skriver ut dem
# p6_1.pl
if (open(MYFILE, "file1")) {
$line = <MYFILE>;
while ($line ne "") {
print ($line);
$line = <MYFILE>;
}
}
27. die vid försök att öppna fil
# p6_2.pl
unless (open(MYFILE, "file1")) {
die ("cannot open input file file1\n");
}
# if the program gets this far, the file was
# opened successfully
$line = <MYFILE>;
while ($line ne "") {
print ($line);
$line = <MYFILE>;
}
28. Inläsning av fil i array
# p6_3.pl
unless (open(MYFILE, "file1")) {
die ("cannot open input file file1\n");
}
@input = <MYFILE>;
print (@input);
29. En fils innehåll kopieras till en
annan fil
# p6_4.pl
unless (open(INFILE, "file1")) {
die ("cannot open input file file1\n");
}
unless (open(OUTFILE, ">outfile")) {
die ("cannot open output file outfile\n");
}
$line = <INFILE>;
while ($line ne "") {
print OUTFILE ($line);
$line = <INFILE>;
}
30. Två filer skrivs ut växelvis
# p6_5.pl
open (INFILE1, "merge1") ||
die ("Cannot open input file merge1\n");
open (INFILE2, "merge2") ||
die ("Cannot open input file merge2\n");
$line1 = <INFILE1>;
$line2 = <INFILE2>;
while ($line1 ne "" || $line2 ne "")
{
if ($line1 ne "") {
print ($line1);
$line1 = <INFILE1>;
}
if ($line2 ne "") {
print ($line2);
$line2 = <INFILE2>;
}
}
31. Standard error file (vanligtvis
skärmen)
# p6_6.pl
open(MYFILE, "file1") ||
die ("Unable to open input file file1\n");
print STDERR ("File file1 opened
successfully.\n");
$line = <MYFILE>;
while ($line ne "") {
chop ($line);
print ("\U$line\E\n");
$line = <MYFILE>;
}
32. Ett program som testar om filen finns
innan den öppnas för skrivning
# p6_8.pl
unless (open(INFILE, "infile")) {
die ("Input file infile cannot be opened.\n");
}
if (-e "outfile") {
die ("Output file outfile already exists.\n");
}
unless (open(OUTFILE, ">outfile")) {
die ("Output file outfile cannot be
opened.\n");
}
$line = <INFILE>;
while ($line ne "") {
chop ($line);
print OUTFILE ("\U$line\E\n");
$line = <INFILE>;
}
33. Mönstermatchning
# p7_1.pl
print ("Ask me a question politely:\n");
$question = <STDIN>;
if ($question =~ /please/) {
print ("Thank you for being polite!\n");
} else {
print ("That was not very polite!\n");
}
34. Ordräkning med hantering av multipla
mellanslag
# p7_6.pl
$thecount = 0;
print ("Enter the input here:\n");
$line = <STDIN>;
while ($line ne "") {
if ($line =~ /\bthe\b/) {
$thecount++;
}
$line = <STDIN>;
}
print ("Number of lines containing 'the':
$thecount\n");
# p7_7.pl
print ("Enter the search pattern:\n");
$pattern = <STDIN>;
chop ($pattern);
$filename = $ARGV[0];
$linenum = $matchcount = 0;
print ("Matches found:\n");
while ($line = <>) {
$linenum += 1;
if ($line =~ /$pattern/) {
print ("$filename, line $linenum\n");
@words = split(/$pattern/, $line);
$matchcount += @words - 1;
}
if (eof) {
$linenum = 0;
$filename = $ARGV[0];
}
}
if ($matchcount == 0) {
print ("No matches found.\n");
} else {
print ("Total number of matches:
$matchcount\n");
}
|