1 / 7

Perl Challenge

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. Exercises #1, #2.

milica
Download Presentation

Perl Challenge

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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"; } Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu 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"; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu 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"; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu 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"; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu 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() ); Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu 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"; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

More Related