perl challenge lecturer: prof. andrzej (aj) bieszczad email: [email protected] phone: 818-677-4954...

7
Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun . edu Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition, Prentice-Hall, GRAHAM GLASS, KING ABLES Slides partially adapted from Kumoh National University of Technology (Korea) and NYU

Upload: magdalen-hodge

Post on 03-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition, Prentice-Hall,

Perl Challenge

Lecturer: Prof. Andrzej (AJ) BieszczadEmail: [email protected]

Phone: 818-677-4954

“UNIX for Programmers and Users”Third Edition, Prentice-Hall, GRAHAM GLASS, KING ABLES

Slides partially adapted from Kumoh National University of Technology (Korea) and NYU

Page 2: Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition, Prentice-Hall,

Perl Challenge

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954 2

Exercises #1, #2• Input your name into a variable called $name and then print "Hello, <your name here>".

my $name;print "Enter your name: ";chomp( $name = <STDIN> );print "Hello, $name.\n";

• Create a subroutine that takes a person's name and prints "Hello, <person's name>."

print "Enter your name: ";chomp( my $name = <STDIN> );&printName( $name );

sub printName { my $name = $_[0]; print "Hello, $name.\n";}

Page 3: Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition, Prentice-Hall,

Perl Challenge

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954 3

Exercise #3• Write a program that asks a user for five groceries, and then store them (using random access operations) in an array called @grocerylist and prints them out in alphabetical order.

print "Enter five items from a grocery list:\n";my @grocerylist;

for( my $i = 0; $i < 5; $i++ ) { print "Enter item $i: "; chomp( $grocerylist[$i] = <STDIN> );}

@grocerylist = sort( @grocerylist );print "\nThe items you entered are:\n";print join("\n", @grocerylist ), "\n";

Page 4: Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition, Prentice-Hall,

Perl Challenge

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954 4

Exercise #4• Write a program that asks a user for five groceries, but store the groceries into an array using one of the stack operations. (Either push or unshift.)

print "Enter five items from a grocery list:\n";my (@grocerylist, $input);

foreach( 1..5 ) { print "Enter item $_: "; chomp( $input = <STDIN> ); push( @grocerylist, $input );}

@grocerylist = sort( @grocerylist );print "\nThe items you entered are:\n";print join("\n", @grocerylist ), "\n";

Page 5: Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition, Prentice-Hall,

Perl Challenge

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954 5

Exercise #5• Write a program that uses a subroutine that asks a user for five groceries. After obtaining the list, the program should print the list in alphabetical order.

sub input { my $list; for( my $i = 1; $i <=5; $i++ ) { print "Enter grocery $i: "; chomp( my $grocery = <STDIN> ); push( @list, $grocery ); } return @list;}

print "Enter five groceries to be sorted and stored in the file \"grocerylist.txt\":\n";my @grocerylist = sort( &input() );print "\nThe items you entered are:\n";print join("\n", @grocerylist ), "\n";

Page 6: Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition, Prentice-Hall,

Perl Challenge

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954 6

Exercise #6• Amend program #5 with a subroutine that writes the list of groceries to a file, and then write them to a file in alphabetical order. Modify the input subroutine, so it sorts the list before returning.

sub write { my $to_write = join( "\n", sort( @_ ) ); open( OUT, ">grocerylist.txt" ); select OUT; print $to_write; close( OUT ); select STDOUT;}

print "Enter five groceries to be sorted and stored in the file \"grocerylist.txt\":\n";&write( &input() );

Page 7: Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition, Prentice-Hall,

Perl Challenge

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954 7

Exercise #7• Write a program that reads the file written in the last exercise, and then prints the contents of the file to the screen in the opposite order that they appeared in the file.

sub read { open( IN, $_[0] ); my @groceries; while(my $line = <IN>) {

chomp( $line );push( @groceries, $line );

} close( IN ); return @groceries;}

print "Opening file grocerylist.txt\n";print join( "\n", reverse( &read("grocerylist.txt") ) ), "\n";