Download presentation
Presentation is loading. Please wait.
Published byMagdalen Hodge Modified over 9 years ago
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, GRAHAM GLASS, KING ABLES Slides partially adapted from Kumoh National University of Technology (Korea) and NYU
2
Perl Challenge Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49542 Exercises #1, #2 Input your name into a variable called $name and then print "Hello, ". my $name; print "Enter your name: "; chomp( $name = ); print "Hello, $name.\n"; Create a subroutine that takes a person's name and prints "Hello,." print "Enter your name: "; chomp( my $name = ); &printName( $name ); sub printName { my $name = $_[0]; print "Hello, $name.\n"; }
3
Perl Challenge Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49543 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] = ); } @grocerylist = sort( @grocerylist ); print "\nThe items you entered are:\n"; print join("\n", @grocerylist ), "\n";
4
Perl Challenge Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49544 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 = ); push( @grocerylist, $input ); } @grocerylist = sort( @grocerylist ); print "\nThe items you entered are:\n"; print join("\n", @grocerylist ), "\n";
5
Perl Challenge Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49545 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 = ); 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";
6
Perl Challenge Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49546 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() );
7
Perl Challenge Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49547 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 = ) { chomp( $line ); push( @groceries, $line ); } close( IN ); return @groceries; } print "Opening file grocerylist.txt\n"; print join( "\n", reverse( &read("grocerylist.txt") ) ), "\n";
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.