print "Enter non-zero digit: "; $number = <> chomp($number); $number = $number+1; $number = $number*9; $number = int($number/10)+$number%10; $number = $number*262; $number = $number-27; $number = $number/3; print "The result is ",$number,"\n";
Read my notes.
There are some difference between single quote and double quote. Please read my notes and the textbook for more differences.
My favorite hero is Batman. My favorite hero is $hero.\n
2 to the power 3, i.e. 8
You should understand how perl convert string to numerical value
1: helloworld 2: hellohello 3: 5 4: not smaller 5: smaller 6: 33
$count = 0;
while(<>) {
if(substr($_, 0, 1) eq 'a' || substr($_,0, 1) eq 'A') {
$count++;
}
}
print $count;
$sum = 0;
while(($line = <>) ne "end\n") {
chomp($line);
if($line =~ /\d|(\d*\.\d*)/) {
$sum+=$line;
print "The current sum is $sum\n";
}
}
1: 4 2: 6 3: 10 4: 11 5: -1 6: ing Perl 7: arn 8: er 9: Leaxyzing Perl
1:3:5:1:::10: 1:3:5:
$counter = 0;
open(NAME, "namelist.txt");
while(<NAME>) {
chomp;
$line[$counter] = $_;
$counter++;
}
@out = sort @line;
open(OUT, ">out.txt");
foreach(@out) {
print OUT "$_\n";
}
a b d e c k b d e c k 1 2
print &average(1.1,2.2,3.3);
sub average {
$length = @_; #same as $length = $#_+1;
$sum = 0;
foreach(@_) {
$sum+=$_;
}
$sum/$length;
}
Read my notes
$all = "abcdefghijklmnopqrstuvwxyz";
for($i=0; $i < length($all); $i++) {
$ch = substr($all, $i, 1);
$hash{$ch}=0;
}
while(<>) {
$_ = lc($_);
for($i = 0; $i < length($_); $i++) {
$ch = substr($_, $i, 1);
if(exists($hash{$ch})) {
$hash{$ch}++;
}
}
}
for($i=0; $i < length($all); $i++) {
$ch = substr($all, $i, 1);
print "$ch: ", $hash{$ch}, "\n";
}
/[ab]..[ab]//A[^xyz]/\d{5}/\$\w+//([a-zA-Z])\1//<(\w+)>.*<\/\1>/
while (<>) { # read input one line at a time
s/0/zero/g; # replace all "0"s with "zero"
s/1/one/g; # replace all "1"s with "one"
s/2/two/g; # replace all "2"s with "two"
s/3/zero/g; # replace all "3"s with "three"
s/4/one/g; # replace all "4"s with "four"
s/5/two/g; # replace all "5"s with "five"
s/6/zero/g; # replace all "6"s with "six"
s/7/one/g; # replace all "7"s with "seven"
s/8/two/g; # replace all "8"s with "eight"
s/9/nine/g; # replace all "9"s with "nine"
print $_;
}
while (<>) { # read input one line at a time
s/<h1>/<h3>/g; # replace all "<h1>" with "<h3>"
s/<\/h1>/<\/h3>/g; # replace all "</h1>" with "</h3>"
print;
}
while (<>) { # read input one line at a time
s/<[^>]*>//g; # remove anything that starts with "<"
# and ends with ">"
print;
}
while(<>) {
if(/(\d{3}-\d{3}-\d{4})/) {
print $1,"\n";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Calculator</title>
</head>
<body>
<form action="cal.cgi" method="get">
<p><input type="text" name="first" size="5" /> +
<input type="text" name="second" size="5" /> =</p>
<p><input type="submit" value="-- Do It! --" />
<button type="reset">Clean It Up!</button></p>
</form>
</body>
</html>
#!/usr/local/bin/perl -wT
use CGI qw(:standard);
@names = param();
foreach $name (@names) {
@item = param($name);
if ($#item > 0) {
$formdata{$name} = join(', ', @item);
}
else {
$formdata{$name} = $item[0];
}
}
&header("Calculator");
$sum = $formdata{"first"} + $formdata{"second"};
print qq(<b>The sum is $sum.</b>);
print qq(</body></html>);
sub header() {
print "Content-type: text/html\n\n";
print qq(<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n);
print qq(<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">);
print "<head>\n<title>\n";
print "$_[0]\n</title>\n</head>\n<body>\n";
}