#! /usr/bin/perl
# Write a program that reads lines of input consisting of two words per line, such as
# bananas 16
# and creates an associative array from these lines. (The first word is to be the subscript and the second word the value.)
while($input=){
chop $input;
@input=split(/ /,$input);
$key=$input[0];
$record=$input[1];
$list{$key}=$record;
}
foreach $key (keys(%list)){
print "$key $list{$key}\n";
}
#! /usr/bin/perl
# Write a program that reads a file and searches for lines of the form
# index word
# where word is a word to be indexed. Each indexed word is to be stored in an associative array, along with the line
# number on which it first occurs. (Subsequent occurrences can be ignored.) Print the resulting index.
print "Please input the file name\n";
$file=;
open FILE,$file;
$line_num=0;
while($line=){
$line_num++;
# chop $line;
$line=~s/\s+/ /g;
@words=split(/ /,$line);
if(($words[0] eq "index")&&(@words>1)&&($list{$words[1]}==0)){
print "\$words[1] is $words[1]\n";
$list{$words[1]}=$line_num;
}
}
foreach $key (keys(%list)){
print "$key $list{$key}\n";
}
#! /usr/bin/perl
# Modify the program created in Exercise 2 to store every occurrence of each index line. (Hint: Try building the
# associative array subscripts using the indexed word, a non-printable character, and a number.) Print the resulting index.
print "Please enter the file name\n";
$line_num=0;
$key_num=0;
$file=;
open FILE,$file;
while($line=){
$line_num++;
$line=~s/\s+/ /g;
@words=split(/ /,$line);
if(($words[0] eq "index")&&(@words>1)){
$indexs{$words[1]}++;
$indexs{$words[1],"^$indexs{$words[1]}"}=$line_num;
if($indexs{$words[1]}==1){
$keys[$key_num++]=$words[1];
}
}
}
close FILE;
foreach $key (@keys){
print "$key totally occurs $indexs{$key} times at: ";
for($num=0;$num<$indexs{$key};$num++){
$temp=$num+1;
print " ",$indexs{$key,"^$temp"};
}
print "\n";
}
#! /usr/bin/perl
# Write a program that reads input lines consisting of a student name and five numbers representing the student's marks
# in English, history, mathematics, science, and geograph, as follows:
# Jones 61 67 75 80 72
# Use an associative array to store these numbers in a database, and then print out the names of all students with failing
# students (less than 50) along with the subjects they failed.
$num=0;
while($input=){
$input=~s/\s+/ /g;
@input=split / /,$input;
$database{$input[0],"^Eng"}=$input[1];
$database{$input[0],"^hist"}=$input[2];
$database{$input[0],"^math"}=$input[3];
$database{$input[0],"^sci"}=$input[4];
$database{$input[0],"^geo"}=$input[5];
$students[$num++]=$input[0];
}
&init;
foreach $student (@students){
$name=$student;
if($database{$student,"^math"}<50){
$math="math";
}
if($database{$student,"^Eng"}<50){
$Eng="Eng";
}
if($database{$student,"^hist"}<50){
$hist="hist";
}
if($database{$student,"^sci"}<50){
$sci="sci";
}
if($database{$student,"^geo"}<50){
$geo="geo";
}
print "$name $Eng $hist $math $sci $geo\n";
&init;
}
sub init{
$name="";
$math="";
$Eng="";
$hist="";
$sci="";
$geo="";
$count=0;
}
#! /usr/bin/perl
# Write a program that prints the powers of 2 from 2**1 to 2**10. Use Write
# and a print format to print them three to a line. Align the lines so that
# the right end of each number is lined up with the right end of the
# corresponding number on the previous line.
format TABLE=
=============================================
powers of 2
@>>>> @>>>> @>>>>
$powers[1],$powers[2],$powers[3]
@>>>> @>>>> @>>>>
$powers[4],$powers[5],$powers[6]
@>>>> @>>>> @>>>>
$powers[7],$powers[8],$powers[9]
@>>>>
$powers[10]
=============================================
.
for($num=1;$num<=10;$num++){
$powers[$num]=2**$num;
}
$~="TABLE";
write;
#! /usr/bin/perl
# Repeat 11-1.perl using printf.
for($num=1;$num<=10;$num++){
printf "%4d",2**$num;
if($num%3==0){
print "\n";
}
}
print "\n";
#! /usr/bin/perl
# Write a program that reads text and formats it into 40-character lines,
# left-justified. Put lines of asterisks above and below the text.
format LINES=
****************************************
~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$input
****************************************
.
@input=;
chop @input;
$input=join(" ",@input);
$~="LINES";
write;
#! /usr/bin/perl
# Write a program that reads a set of dollar values such as 71.43(one
# per line). Write out two values per line (the first and second on the first
# line, and so on). Total each of the resulting columns, and produce a grand
# total.
format HEADER=
=====================
.
format LINE=
@>>>>>> @>>>>>>
$num1,$num2
.
format TAILER=
=====================
@>>>>>> @>>>>>> grand total @>>>>>>
$sum1, $sum2,$tot_sum
.
$count=0;
$sum1=0;
$sum2=0;
@input=;
$~="HEADER";
write;
$~=LINE;
chop @input;
while($count<@input){
$count++;
if($count%2==0){
$num2=$input[$count-1];
$sum2+=$num2;
write;
} elsif($count%2==1){
$num1=$input[$count-1];
$sum1+=$num1;
}
}
if($count==0){
$num2="";
write;
}
$~="TAILER";
$tot_sum=$sum1+$sum2;
write;
#! /usr/bin/perl
# Write a program that reads the directory . and print out all file and
# directory names that start with a period. Ignore the special files .(one
# period) and .. (two periods).
opendir(DIR,".");
while($filename=readdir(DIR)){
next if($filename eq "."||$filename eq ".."||$filename!~ /^\..*/);
print "$filename\n";
}
close DIR;
#! /usr/bin/perl
# Write a program that lists all the files (not the subdirectories) in the
# directory "/u1/h2/ping/public_html/yang/compSci" and then lists the contents
# of any subdirectories, and their subdirectories, and so on.
$dirname="/u1/h2/ping/public_html/yang/compSci/algorithm";
&list_dir($dirname);
sub list_dir{
local($dirname)=@_;
local($list,$DIR);
opendir($DIR,$dirname)||die "can not open $dirname\n";
print "\n".$dirname."\n\n";
$list="";
while($filename=readdir $DIR){
$file=$dirname."/".$filename;
if(-f $file){
$list.=$filename."\n";
}
}
print $list;
rewinddir $DIR;
while($filename=readdir $DIR){
next if($filename=~/\.{1,2}/);
$newdir=$dirname."/".$filename;
if(-d $newdir){
&list_dir($newdir);
}
}
closedir DIR;
}
#! /usr/bin/perl
# Write a program that uses readdir and rewinddir to read a directory named
# /u1/h2/ping/public_html/yang/compSci and print a sorted list of the files
# and directories in alphabetical order. Ignore all names beginning with
# a period.
$dirname="/u1/h2/ping/public_html/yang/compSci/perl";
opendir($dir,$dirname)||die "can not open $dirname\n";
$list="";
while($file=readdir $dir){
if($file=~/^[^.]/){
$list.=$file." ";
}
}
@lists=sort split(/ /,$list);
closedir $dir;
for($count=0;$count<@lists;$count++){
print $lists[$count]."\n";
}
#! /usr/bin/perl
# Write a program that uses hot keys and does the following.
# Read single digits and prints out their English-language equivalents.
# Terminates if it reads the Esc (escape) character.
# Inores all other input
# Prints out one English word per line.
@words=("zero","one","two","three","four","five","six","seven","eight","nine","ten");
&start_hotkey;
while(1){
$char=getc(STDIN);
last if($char eq "\e");
if($char=~/[0-9]/){
print $words[$char]."\n";
}
}
&end_hotkey;
sub start_hotkey{
system("stty cbreak"); # tells the system to process input one character at a time.
system("stty -echo");
}
sub end_hotkey{
system("stty -cbreak");
system("stty echo");
}
#! /usr/bin/perl
# Write a program that reads the directory "." and grants global excute
# permissions for all files ending in .perl. Take away all other permissions,
# except user read, for every file in the directory. Skip over all
# subdirectories.
$dirname=".";
opendir($dir,$dirname)||die "can not open $dirname\n";
while($file=readdir $dir){
next if(-d $file);
if($file=~/\.perl$/){
@stat=stat($file);
chmod(($stat[2]|0700),$file);
}else{
chmod(0400,$file);
}
}
#! /usr/bin/perl
# Write a program that uses fork and waitpid to generate a total of three
# processes (including the program). Have each process print a line, and have
# the lines appear in a specified order.
$retval=fork();
if($retval==0){
$val=fork();
if($val==0){
print "I am the grandson\n";
}else{
waitpid($val,0);
print "$val died. I am parent of $val\n";
}
}else{
waitpid($retval,0);
print "$retval died. I am grandparent\n";
}
#! /usr/bin/perl
open(FILE,"temp");
@file=;
print @file;
close FILE;
#! /usr/bin/perl
# Write a program that reads input from a file named tmp and writes it to the
# standard output file. Write another program that reads input from the standard
# output file, writes it to temp, and uses exec to call the first program.
@input=;
open($FILE,">temp")||die "can not open the file temp\n";
select($FILE);
print @input;
close $FILE;
exec "13-2-2.perl";
#! /usr/bin/perl
# Write a program that prints the natural logarithm of the integers between 1
# and 100.
format STDOUT=
@>>> @>>>>>>
$num,$logval
.
for($num=1;$num<=100;$num++){
$logval=log($num);
write;
}
#! /usr/bin/perl
# Write a program that computes the sum of the numbers from 1 to 10**n for
# values of n from 1 to 6. For each computed value,use times o calculate the
# amount of time each computation takes. Print these calculation time.
for($num=1;$num<=6;$num++){
print "\$num=$num\n";
$retval=fork();
if($retval==0){
$sum=&calculate($num);
print "sum of 1 to 10**$num=$sum\n";
exit(0);
}else{
waitpid($retval,0);
@timelist=times;
$time=$timelist[2];
print "sum of 1 to 10**$num time used: $time\n";
}
}
sub calculate{
local($val)=@_;
local($sum);
$sum=0;
for($n=1;$n<=10**$val;$n++){
$sum+=$n;
}
$sum;
}
#! /usr/bin/perl
# Write a program that uses index to print out the locations of the letters
# a,e,i,o,and u in an input file.
$line=;
&get_pos("a");
&get_pos("i");
&get_pos("o");
&get_pos("u");
&get_pos("e");
&prt_pos("e");
&prt_pos("a");
&prt_pos("i");
&prt_pos("o");
&prt_pos("u");
sub get_pos{
local($letter)=@_;
local($pos);
$pos=-1;
while(1){
$pos=index($line,$letter,$pos+1);
last if($pos==-1);
$pos{$letter}++;
# print "\$pos{\$letter}=$pos{$letter}\n";
$pos{$letter.$pos{$letter}}=$pos;
}
}
sub prt_pos{
local($letter)=@_;
local($num);
print "Letter $letter occur at:";
for($num=1;$num<=$pos{$letter};$num++){
print "$pos{$letter.$num} ";
}
print "\n";
}
#! /usr/bin/perl
# Write a program that reads a number. If the number is a floating-point value
# print itin exponential and fixed-point form. If the number is an integer,
# print it in decimal, octal, and hexadecimal form.
$num=;
if($num=~/\.|[eE]/){
printf("in exponential form: %e\n",$num);
printf("in fixed-point form: %f\n",$num);
}else{
printf("in decimal form: %d\n",$num);
printf("in octal form: %o\n",$num);
printf("in hexadecimal form: %x\n",$num);
}
#! /usr/bin/perl
# Write a program that uses int to round a value to two decimal places.
print "Please enter a number\n";
$num=;
$num*=100;
$num=int($num+0.5);
$num/=100;
print $num."\n";
#! /usr/bin/perl
# Write a program that "flips" an associative array: that is, the subscript
# of the old array become the values of the new, and vice versa. Print an
# error message if the old array has two subscripts with identical values.
print "Please enter the associative array. Key and value in a line.\n";
while($line=){
chop $line;
@line=split(/ /,$line);
$array{$line[0]}=$line[1];
}
foreach $key(keys(%array)){
$newKey=$array{$key};
if(!defined($newArray{$newKey})){
$newArray{$newKey}=$key;
}else{
print "error, $newKey has two identical value\n";
exit(0);
}
}
foreach $key(keys(%newArray)){
print "$key:$newArray{$key}\n";
}
#! /usr/bin/perl
# Write a program that reads a file from standard input, breaks each line into
# words, uses grep to get rid of all words longer than five characters, and
# prints the file.
@input=;
for($num=0;$num<@input;$num++){
@words=split(/\s+/,$input[$num]);
print "@words\n";
@shortWords=grep(/^.{1,5}$/,@words);
print "@shortWords\n";
}
#! /usr/bin/perl
# Write a program that lists (by name) all the groups into which user ID are
# sorted on your machine. List all the user names in each group. Sort the
# groups, and the user names in each group, in alphabetical order.
while(($gname,$gpasswd,$gid,$gmembers)=getgrent){
$glist{$gname}=$gmembers;
}
@list=sort keys(%glist);
foreach $g(@list){
@users=sort split(/\s+/,$glist{$g});
print "$g:\n";
print " @users\n";
}
#! /usr/bin/perl
# Write a program that lists every user name on your machine and prints the
# home directory for each
while(@pwent=getpwent()){
$list{$pwent[0]}=$pwent[7];
}
foreach $user(sort %list){
print "$user: $list{$user}\n";
}
#! /usr/bin/perl
# Write a program that lists the shells used by users on your machine. List
# the number of users of each shell, and sort the list in descending order
# of use.
while(@user=getpwent()){
$list{$user[8]}++;
}
foreach $shell(sort count keys(%list)){
print "$shell: $list{$shell}\n";
}
sub count{
$list{$b}<=>$list{$a};
}
#! /usr/bin/perl
# Write a program that splits into two identical processes, and have each
# process print the process ID of the other.
$retval=fork();
if($retval==0){
print "The parent process is ",getppid(),"\n";
}else{
print "The child process is $retval\n";
}
#! /usr/bin/perl
# Write a program that sends a specific file, /u1/h2/ping/public_html/yang/compSci/perl/testfile to
# clients who request it. The program should send the file by creating a copy
# of itself using fork, and it should be able to send to five clients at once.
#! /usr/bin/perl
# Write a Perl program that uses the while statement to print out the first
# ten numbers(1-10) in asending order
# uses the until statement to print out the first ten numbers in descending
# order(10-1);
$num=1;
while($num<=10){
print $num;
print "\n";
$num++;
}
until($num<=1){
$num--;
print $num;
print "\n";
}
#! /usr/bin/perl
# Write a program that prints every number from 0 to 1 that has a single
# digit after the decimal place(that is, 0.1, 0.2, and so on).
$num=0;
until($num>0.9){
$num+=0.1;
print "num is $num\n";
}
#! /usr/bin/perl
# Write a program that reads a line of input and prints out the following:
# 1 if the line consists of a non-zero integer
# 0 if the line consists of 0 or a string.
$input=;
if ($input==0){
print "0\n";
}
else {
print "1\n";
}
#! /usr/bin/perl
# Write a program that asks for a number and keep trying until you
# enter the number 47. At that point, it prints correct! and rings a bell.
$require="Please enter a number:";
print $require;
$num=;
while($num!=47){
print $require;
$num=;
}
print "correct!\n\a\a\a\a\a";
print "\a\a\a";
#! /usr/bin/perl
# Write a program that reads two integers from standard input(one at a time), divides the first one
# by the second one, and prints out the quotient(the result) and the remainder.
$num1=;
chop($num1);
$num2=;
chop($num2);
print "The qutient of $num1/$num2 is ".($num1-$num1%$num2)/$num2.",the remainder is ".$num1%$num2."\n";
#! /usr/bin/perl
# Write a program that counts all occurrences of the word the in the standard input file.
@input=<>;
chop @input;
$input_str=join(" ",@input);
@input=split(" ",$input_str);
$num=@input;
$count=0;
for(;$num>0;$num--){
if($input[$num-1] eq "the"){
$count++;
}
}
print "\"the\" word occure $count times\n";
#! /usr/bin/perl
# Write a program that reads lines of input containing numbers, each of which is separated by exactly one space, and
# prints out the following:
# a. The total for each line
# b. The grand total
@input=;
$grand_total=0;
$tot_lines=@input;
for($num=0;$num<$tot_lines;$num++){
@line=split(" ",$input[$num]);
$tot_num=@line;
$total=0;
for(;$tot_num>0;$tot_num--){
$total+=$line[$tot_num-1];
}
print "line",$num+1," total is $total\n";
$grand_total+=$total;
}
print "\ngrand total is $grand_total\n\n";
#!/usr/bin/perl
# the same with 5-2-1.perl
@lines=<>;
chop(@lines);
$longline=join(" ",@lines);
@words=split(/ /,$longline);
@words=reverse sort (@words);
$index=0;
while($index<@words){
if($words[$index] ne $words[$index-1]){
print("$words[$index]\n");
}
$index++;
}
#! /usr/bin/perl
# Write a program that read all input from the standard input file and sorts all the words in reverse order,
# printing out one word per line with duplicates omitted.
$line=<>;
chop($line);
while($line ne ""){
@words=split(/ /,$line);
$num=0;
while($num<@words){
$words[$num]=reverse($words[$num]);
$num++;
}
@allWords=(@allWords,@words);
print "allWords are @allWords\n";
$line=<>;
chop($line);
}
@allWords=sort(@allWords);
$num=0;
$last_word="";
while($num<@allWords){
if($allWords[$num] ne $last_word){
print reverse($allWords[$num])."\n";
$num++;
}
}
#! /usr/bin/perl
# Write a program that takes the values on the command line, adds them together, and prints the result.
$sum=0;
$tot_num=@ARGV;
for(;$tot_num>0;$tot_num--){
$sum+=$ARGV[$tot_num-1];
}
print "total sum is $sum\n";
#! /usr/bin/perl
# Write a progam that takes a list of files from the command line and examines their size. If a file is bigger than 10,000
# bytes, rpint File name is a big file! where name is a placeholder for the name of the big file.
$file_num=@ARGV;
$num=0;
while($num<@ARGV){
# $size=-s @ARGV[$num];
# if($size>300){
if(-s @ARGV[$num]>300){
print "File $ARGV[$num] is a big file!\n";
}
$num++;
}
#! /usr/bin/perl
# Write a program that copies a file named file1 to file2, and then appends another copy of file1 to file2.
open SOURCE,"$ARGV[0]"||die "Can not open file $ARGV[0]\n";
open TARGET,">$ARGV[1]" ||die "Can not open file $ARGV[1] or $ARGV[1] already exists\n";
#open TARGET,">$ARGV[1]" && !( -e TARGET) ||die "Can not open file $ARGV[1] or $ARGV[1] already exists\n";
-r SOURCE||die "File $ARGV[0] can not be read\n";
# -w TARGET||die "File $ARGV[1] can not be written\n";
while(($line=