├── .gitattributes ├── 9781484250549.jpg ├── Contributing.md ├── LICENSE.txt ├── README.md ├── errata.md ├── examples ├── chapter1 │ ├── 1_first.pl │ └── 1_second.pl ├── chapter10 │ ├── 10_grep.pl │ └── 10_sleep.pl ├── chapter11 │ └── no_files.txt ├── chapter12 │ ├── 12_scope1.pl │ ├── 12_scope2.pl │ ├── 12_scope3.pl │ ├── 12_scope4.pl │ └── 12_scope5.pl ├── chapter13 │ ├── 13_diag.pl │ └── 13_subs.pl ├── chapter14 │ └── 14_w.pl ├── chapter2 │ └── 2_defined.pl ├── chapter3 │ └── no_files.txt ├── chapter4 │ ├── 4_argv.pl │ └── 4_env.pl ├── chapter5 │ ├── 5_age1.pl │ ├── 5_age2.pl │ ├── 5_do.pl │ ├── 5_given.pl │ ├── 5_last1.pl │ ├── 5_last2.pl │ ├── 5_next.pl │ ├── 5_switch.pl │ ├── 5_until.pl │ ├── 5_while1.pl │ └── 5_while2.pl ├── chapter6 │ └── 6_dir.pl ├── chapter7 │ ├── 7_count.pl │ ├── 7_echo.pl │ └── 7_line.pl ├── chapter8 │ ├── 8_die.pl │ ├── 8_form1.pl │ ├── 8_form_2.pl │ ├── 8_form_3.pl │ ├── 8_form_num.pl │ ├── 8_form_trunc.pl │ ├── 8_here.pl │ ├── 8_more.pl │ └── 8_warn.pl └── chapter9 │ ├── 9_back1.pl │ ├── 9_back2.pl │ └── 9_back3.pl └── lab answers ├── bob.data ├── cb1.pl ├── cb10.pl ├── cb11.pl ├── cb12.pl ├── cb13.pl ├── cb2.pl ├── cb3.pl ├── cb5.pl ├── cb6.pl ├── cb8.pl ├── cb9.pl ├── paragraph.pl ├── phone.pl └── start.pl /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /9781484250549.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beginning-perl-programming/cae1ecc4215a21594699c74cd11827975ab8f03b/9781484250549.jpg -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2019 William "Bo" Rothwell 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Beginning Perl Programming*](https://www.apress.com/9781484250549) by William "Bo" Rothwell (Apress, 2019). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781484250549.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. -------------------------------------------------------------------------------- /errata.md: -------------------------------------------------------------------------------- 1 | # Errata for *Book Title* 2 | 3 | On **page xx** [Summary of error]: 4 | 5 | Details of error here. Highlight key pieces in **bold**. 6 | 7 | *** 8 | 9 | On **page xx** [Summary of error]: 10 | 11 | Details of error here. Highlight key pieces in **bold**. 12 | 13 | *** -------------------------------------------------------------------------------- /examples/chapter1/1_first.pl: -------------------------------------------------------------------------------- 1 | #1_first.pl 2 | print "This is my first Perl program\n "; 3 | -------------------------------------------------------------------------------- /examples/chapter1/1_second.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | # 1_second.pl 3 | print "This is my second Perl program\n "; 4 | -------------------------------------------------------------------------------- /examples/chapter10/10_grep.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #10_grep.pl 3 | 4 | @array=qw(Bob Bobby Ted Fred Sue Nick Sally); 5 | @b=grep (/^B/, @array); 6 | print "@b"; 7 | -------------------------------------------------------------------------------- /examples/chapter10/10_sleep.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #10_sleep.pl 3 | 4 | print "countdown!\n\n"; 5 | $|=1; 6 | for ($i=10;$i>0;$i--) { 7 | print "$i \r"; 8 | sleep 1; 9 | } 10 | $|=0; 11 | print "Blast off!\n"; 12 | -------------------------------------------------------------------------------- /examples/chapter11/no_files.txt: -------------------------------------------------------------------------------- 1 | No example files for this unit. -------------------------------------------------------------------------------- /examples/chapter12/12_scope1.pl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beginning-perl-programming/cae1ecc4215a21594699c74cd11827975ab8f03b/examples/chapter12/12_scope1.pl -------------------------------------------------------------------------------- /examples/chapter12/12_scope2.pl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beginning-perl-programming/cae1ecc4215a21594699c74cd11827975ab8f03b/examples/chapter12/12_scope2.pl -------------------------------------------------------------------------------- /examples/chapter12/12_scope3.pl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beginning-perl-programming/cae1ecc4215a21594699c74cd11827975ab8f03b/examples/chapter12/12_scope3.pl -------------------------------------------------------------------------------- /examples/chapter12/12_scope4.pl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beginning-perl-programming/cae1ecc4215a21594699c74cd11827975ab8f03b/examples/chapter12/12_scope4.pl -------------------------------------------------------------------------------- /examples/chapter12/12_scope5.pl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beginning-perl-programming/cae1ecc4215a21594699c74cd11827975ab8f03b/examples/chapter12/12_scope5.pl -------------------------------------------------------------------------------- /examples/chapter13/13_diag.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #13_diag.pl 3 | 4 | use diagnostics; 5 | print "this is only a test; 6 | -------------------------------------------------------------------------------- /examples/chapter13/13_subs.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #13_subs.pl 3 | 4 | use strict 'subs'; 5 | 6 | sub jesttest { 7 | print "This is just a test"; 8 | } 9 | 10 | sub hello { 11 | print "hello\n"; 12 | } 13 | 14 | hello; #Calls a valid subroutine, no problem 15 | justatest; #Bareword that isn't a subroutine. 16 | -------------------------------------------------------------------------------- /examples/chapter14/14_w.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #14_w.pl 3 | 4 | if ($var == 0) { 5 | print "yes\n"; 6 | } 7 | 8 | print GROUP "hello there\n"; 9 | 10 | $name="Bob"; 11 | 12 | if ($name == "Ted") { 13 | print "yes\n"; 14 | } 15 | -------------------------------------------------------------------------------- /examples/chapter2/2_defined.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #2_defined.pl 3 | 4 | if (defined ($total)) { 5 | print "The value is: ", $total +8, "\n"; 6 | } 7 | else 8 | { 9 | print 'The variable $total is not defined', "\n"; 10 | } 11 | -------------------------------------------------------------------------------- /examples/chapter3/no_files.txt: -------------------------------------------------------------------------------- 1 | No example files for this unit. -------------------------------------------------------------------------------- /examples/chapter4/4_argv.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #4_argv.pl 3 | #This script adds up to three arguments and prints the result on the screen 4 | $total=$ARGV[0] + $ARGV[1] + $ARGV[2]; 5 | print "$total\n"; 6 | 7 | print "This command is called $0\n"; 8 | -------------------------------------------------------------------------------- /examples/chapter4/4_env.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #4_env.pl 3 | foreach $var (keys %ENV) { 4 | print "Var: $var Set to: $ENV{$var}\n"; 5 | } 6 | -------------------------------------------------------------------------------- /examples/chapter5/5_age1.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #5_age1.pl 3 | print "Please input your age "; 4 | $age=; 5 | if ($age > 15) { 6 | print "You are old enough to drive\n"; 7 | } 8 | else { 9 | print "You can't drive for ", 16 - $age, " more years\n"; 10 | } 11 | -------------------------------------------------------------------------------- /examples/chapter5/5_age2.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #5_age2.pl 3 | print "Please input your age "; 4 | $age=; 5 | if ($age > 15) { 6 | print "You are old enough to drive\n"; 7 | } 8 | elsif ($age == 15) { 9 | print "You are old enough for a permit\n"; 10 | } 11 | else { 12 | print "You can't drive for ", 16 - $age, " more years\n"; 13 | } 14 | -------------------------------------------------------------------------------- /examples/chapter5/5_do.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #5_do.pl 3 | #Verify number entered is greater than 100 4 | 5 | do { 6 | print "Please enter a number greater than 100: "; 7 | $number=; 8 | } while ($number <= 100); 9 | 10 | print "Thanks, $number is greater than 100\n"; 11 | -------------------------------------------------------------------------------- /examples/chapter5/5_given.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #5_given.pl 3 | 4 | use feature "switch"; #Provides access to the given statement 5 | 6 | print "Please enter 'yes' or 'no': "; 7 | $response=; 8 | chomp $response; 9 | 10 | given ($response) { 11 | when ("yes") {print "You agree!\n"; } 12 | when ("no") {print "Bummer, you don't agree\n"; } 13 | default {print "Maybe next time\n"; } 14 | } 15 | -------------------------------------------------------------------------------- /examples/chapter5/5_last1.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #5_last1.pl 3 | 4 | while (true) { 5 | print "Please enter a grade(enter -1 to finish): "; 6 | chomp($score=); 7 | if ($score == -1) { 8 | last; 9 | } 10 | push(@grades, $score); 11 | } 12 | 13 | print "finished\n"; 14 | print "@grades"; 15 | print "\n"; 16 | -------------------------------------------------------------------------------- /examples/chapter5/5_last2.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #5_last2.pl 3 | #Example of breaking out of nested loops 4 | 5 | @classes=qw(math science history); 6 | 7 | #Create a label: 8 | JUMP: foreach $subject (@classes) { 9 | print "Enter grades for $subject (-2 to finish)\n"; 10 | 11 | while (true) { 12 | print "Please enter a grade(enter -1 to finish): "; 13 | chomp($score=); 14 | if ($score == -1) { 15 | last; #last to while loop 16 | } 17 | if ($score == -2) { 18 | last JUMP; #last to the label's loop 19 | } 20 | push(@grades, "$subject:$score"); 21 | } 22 | } 23 | 24 | foreach $i (@grades) { 25 | print "$i\n"; 26 | } 27 | -------------------------------------------------------------------------------- /examples/chapter5/5_next.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #5_next.pl 3 | 4 | while (true) { 5 | print "Please enter a grade (enter -1 to finish): "; 6 | chomp($score=); 7 | if ($score == -1) { 8 | last; 9 | } 10 | if ($score < 0) { 11 | print "Bad input, try again\n"; 12 | next; 13 | } 14 | push(@grades, $score); 15 | } 16 | 17 | 18 | print "finished\n"; 19 | print "@grades"; 20 | print "\n"; 21 | -------------------------------------------------------------------------------- /examples/chapter5/5_switch.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #5_switch.pl 3 | 4 | use Switch; #Loads the switch module 5 | 6 | print "Please enter 'yes' or 'no':"; 7 | $response=; 8 | chomp $response; 9 | 10 | switch ($response) { 11 | case "yes" {print "You agree!\n"; } 12 | case "no" {print "Bummer, you don't agree\n"; } 13 | else {print "Maybe next time\n"; } 14 | } 15 | -------------------------------------------------------------------------------- /examples/chapter5/5_until.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | # 5_until.pl 3 | # Verify number entered is greater than 100 4 | 5 | print "Please enter a number greater than 100: "; 6 | $number=; 7 | 8 | until ($number > 100) { 9 | print "That is not greater than 100\n"; 10 | print "Please enter a number greater than 100: "; 11 | $number=; 12 | } 13 | 14 | print "Thanks, $number is greater than 100\n"; 15 | -------------------------------------------------------------------------------- /examples/chapter5/5_while1.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #5_while1.pl 3 | #Verify number entered is greater than 100 4 | 5 | print "Please enter a number greater than 100:"; 6 | $number=; 7 | 8 | while ($number <= 100) { 9 | print "That is not greater than 100\n"; 10 | print "Please enter a number greater than 100: "; 11 | $number=; 12 | } 13 | 14 | print "Thanks, $number is greater than 100\n"; 15 | -------------------------------------------------------------------------------- /examples/chapter5/5_while2.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #5_while2.pl 3 | #Verify number entered is greater than 100 4 | 5 | while ($number <= 100) { 6 | print "Please enter a number greater than 100: "; 7 | $number=; 8 | } 9 | -------------------------------------------------------------------------------- /examples/chapter6/6_dir.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #6_dir.pl 3 | 4 | print "Please enter the name of a file or directory: "; 5 | $name=; 6 | chomp $name; 7 | if (-d $name) { 8 | print "$name is a directory\n"; 9 | } else { 10 | print "$name is not a directory\n"; 11 | } 12 | -------------------------------------------------------------------------------- /examples/chapter7/7_count.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #7_count.pl 3 | # This program will count the number of lines that contain the word "echo" 4 | 5 | $total=0; #not required, just good style 6 | 7 | while ($line=<>) { 8 | if ($line =~ /echo/) { 9 | $total++; 10 | } 11 | } 12 | print "Number of lines that contain 'echo': $total\n"; 13 | -------------------------------------------------------------------------------- /examples/chapter7/7_echo.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #7_echo.pl 3 | # This program will count the number of lines that contain the word "echo" 4 | 5 | $total=0; #not required, just good style 6 | 7 | while (<>) { 8 | if (/echo/) { #Assumes to look in $_ for "echo" 9 | $total++; 10 | } 11 | } 12 | print "Number of lines that contain 'echo': $total\n"; 13 | -------------------------------------------------------------------------------- /examples/chapter7/7_line.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #7_line.pl 3 | 4 | while ($line=) { #reads a line at a time from standard input 5 | print "$. $line"; 6 | } 7 | -------------------------------------------------------------------------------- /examples/chapter8/8_die.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #8_die.pl 3 | 4 | if (!(-r "/etc/junkfile")) { 5 | die "can't find file" 6 | } 7 | 8 | print "go on from here with more code"; 9 | -------------------------------------------------------------------------------- /examples/chapter8/8_form1.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #8_form1.pl 3 | 4 | format STDOUT = 5 | @|||||||||||| 6 | $title 7 | Name: @<<<<< Age: @<< 8 | $name, $age 9 | code: @>>>>>>>> 10 | $code 11 | Sale #1: @####.## 12 | $sale1 13 | Sale #2: @####.## 14 | $sale2 15 | Sale #3: @####.## 16 | $sale3 17 | . 18 | 19 | $title="Status"; 20 | $name="bob smith"; 21 | $age=25; 22 | $code="674AR3"; 23 | $sale1=123; $sale2=9.99; $sale3=45.8; 24 | 25 | write STDOUT; 26 | -------------------------------------------------------------------------------- /examples/chapter8/8_form_2.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #8_form_2.pl 3 | 4 | format STDOUT = 5 | Comment: @* 6 | $comment 7 | Keywords: ^<<<<<< 8 | $keywords 9 | ^<<<<<< 10 | $keywords 11 | . 12 | 13 | $comment="Displays good tact\nworks hard\nsometimes is late"; 14 | $keywords="work effort"; 15 | 16 | write STDOUT; 17 | -------------------------------------------------------------------------------- /examples/chapter8/8_form_3.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #8_form_3.pl 3 | 4 | format STDOUT = 5 | Keywords: ^<<<<<< 6 | $keywords 7 | ~~ ^<<<<<< 8 | $keywords 9 | . 10 | 11 | $keywords="work effort late raise"; 12 | 13 | write STDOUT; 14 | -------------------------------------------------------------------------------- /examples/chapter8/8_form_num.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #8_form_num.pl 3 | 4 | format STDOUT = 5 | Sale #1: @0###.## 6 | $sale1 7 | Sale #2: @####.## 8 | $sale2 9 | Sale #3: @####.## 10 | $sale3 11 | . 12 | 13 | $sale1=123; 14 | $sale2=9.4587; 15 | $sale3=4444445.8; 16 | 17 | write STDOUT; 18 | -------------------------------------------------------------------------------- /examples/chapter8/8_form_trunc.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #8_form_trunc.pl 3 | 4 | format STDOUT = 5 | Name: @<<<<<<<... 6 | $name 7 | . 8 | 9 | $name="Mr. My"; 10 | 11 | write STDOUT; 12 | 13 | $name="Mr. My Name Is Too Long"; 14 | 15 | write STDOUT; 16 | -------------------------------------------------------------------------------- /examples/chapter8/8_here.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #8_here.pl 3 | 4 | #Using a here document: 5 | print <<'EOF'; 6 | these are the lines of text to send to bob 7 | This service costs $0 8 | EOF 9 | 10 | print "\n\n"; 11 | 12 | #Using a print statement, option #1: 13 | print "\nthese are the lines of text to send to bob\nThis service costs \$0\n"; 14 | 15 | print "\n\n"; 16 | 17 | #Using a print statement, option #2: 18 | print ' 19 | these are the lines of text to send to bob 20 | This service costs $0'; 21 | 22 | print "\n\n"; 23 | -------------------------------------------------------------------------------- /examples/chapter8/8_more.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #8_more.pl 3 | 4 | open (MORE, "| more") || die "can't do this"; 5 | 6 | for ($i=1;$i < 100 ; $i++) { 7 | print MORE "$i\n"; 8 | } 9 | 10 | close MORE 11 | -------------------------------------------------------------------------------- /examples/chapter8/8_warn.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #8_warn.pl 3 | 4 | if (!(-r "/etc/junkfile")) {warn "can't find file"}; 5 | if (!(-r "/etc/junkfile")) {warn "can't find file\n"}; 6 | if (!(-r "/etc/junkfile")) {print STDERR "can't find file\n"}; 7 | -------------------------------------------------------------------------------- /examples/chapter9/9_back1.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #9_back1.pl 3 | 4 | print "Please enter your first and last name: "; 5 | $_=; 6 | 7 | if (m/(.*) (.*)/) #ex: "Bob Smith" 8 | { 9 | print "$2, $1\n"; 10 | } 11 | -------------------------------------------------------------------------------- /examples/chapter9/9_back2.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #9_back2.pl 3 | 4 | open (GROUP, ") { 7 | m/(.*):(.*):(.*):(.*)/; 8 | $total += $3; 9 | } 10 | 11 | print "Total: $total\n"; 12 | -------------------------------------------------------------------------------- /examples/chapter9/9_back3.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #9_back3.pl 3 | 4 | print "Please enter a line: "; 5 | $_=; 6 | chomp $_; 7 | 8 | if (/^(...).*\1$/) {print "$1\n";} 9 | 10 | $junk="whatever"; 11 | 12 | if ($junk =~ /what/) {print "yes\n";} 13 | 14 | print "$1\n"; 15 | -------------------------------------------------------------------------------- /lab answers/bob.data: -------------------------------------------------------------------------------- 1 | DEP:12/12/1999:Beginning Balance:100 2 | DEP:12/12/1999:payday:111 3 | DEP:02/28/2999:wowza:111 4 | 1111:12/12/1999:check from grandma:100 5 | DEP:12/12/1999:12312:123 6 | -------------------------------------------------------------------------------- /lab answers/cb1.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | 3 | # cb1.pl 4 | # Author: Your Name 5 | # Date created: 00-00-00 6 | # Descripton: This program will allow the user to keep track 7 | # of a personal checkbook including: Deposits, 8 | # withdrawals, checks writen, looking up checks 9 | # by check number or date writen, and printing a 10 | # statement. 11 | 12 | print "Welcome to Checkbook!\n"; 13 | print "Please enter your name:"; -------------------------------------------------------------------------------- /lab answers/cb10.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | 3 | # cb10.pl 4 | # Author: Your Name 5 | # Date created: 00-00-00 6 | # Descripton: This program will allow the user to keep track 7 | # of a personal checkbook including: Deposits, 8 | # withdrawals, checks writen, looking up checks 9 | # by check number or date writen, and printing a 10 | # statement. 11 | 12 | format STDOUT = 13 | @<<<<<<<<< @<<<<<<<<< @<<<<<<<<<<<<<<<<<<<< @########.## 14 | $1, $2, $3, $4 15 | . 16 | 17 | print "Welcome to Checkbook!\n"; 18 | print "Please enter your name:"; 19 | chomp ($name=); 20 | 21 | #Check for valid name: 22 | while ($name eq "") 23 | { 24 | warn "Sorry, did you enter your name\n"; 25 | print "Please enter your name:"; 26 | chomp ($name=); 27 | } 28 | 29 | $file="${name}.data"; 30 | print "Note: the data you enter will be stored in a plain text file.\nThis file will be called $file. \n\n"; 31 | 32 | if (-e $file) 33 | { 34 | 35 | print "The file $file already exists\n"; 36 | print "Please press to continue\n"; 37 | $dummy = ; 38 | 39 | open (RFILE, $file) || die "Could not open file"; 40 | @book = ; 41 | close (RFILE); 42 | 43 | foreach $data (@book) 44 | { 45 | # Using Split instead: 46 | @fields = split (/:/, $data); 47 | # $data =~ /(.*):.*:.*:(.*)/; 48 | if ($fields[0] eq "DEP") 49 | { 50 | $balance += $fields[3]; 51 | } 52 | else 53 | { 54 | $balance -= $fields[3]; 55 | } 56 | } 57 | } 58 | else 59 | { 60 | print "The file $file does not exist, so one will be made for you\n\n"; 61 | print "Please enter a beginning balance for this account: "; 62 | $balance=; 63 | 64 | while ($balance <= 0) 65 | { 66 | warn "The amount must be greater than 0\n"; 67 | print "Please enter the amount of the deposit (ex: 1234.56): "; 68 | $balance = ; 69 | } 70 | 71 | do 72 | { 73 | print "Please enter the today's date: "; 74 | chomp ($date = ); 75 | if (!($date =~ /^(10|11|12|0[1-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 76 | { 77 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 78 | } 79 | } 80 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 81 | 82 | $book[0] = "DEP:$date:Beginning Balance:$balance"; 83 | } 84 | 85 | while ($choice != 7) 86 | { 87 | print "Your current balance is: $balance\n\n"; 88 | 89 | print "Please enter your choice:\n"; 90 | print "1. Enter a deposit\n"; 91 | print "2. Enter a withdrawal\n"; 92 | print "3. Enter a check\n"; 93 | print "4. Lookup a check by #\n"; 94 | print "5. Lookup a check by date\n"; 95 | print "6. Print a statement\n"; 96 | print "7. Exit program\n"; 97 | 98 | print "Please enter your choice> "; 99 | 100 | chomp ($choice = ); 101 | 102 | if ($choice == 1) 103 | { 104 | print "Please enter the amount of the deposit (ex: 1234.56): "; 105 | $amount = ; 106 | 107 | while ($amount <= 0) 108 | { 109 | warn "The amount must be greater than 0\n"; 110 | print "Please enter the amount of the deposit (ex: 1234.56): "; 111 | $amount = ; 112 | } 113 | 114 | do 115 | { 116 | print "Please enter the today's date: "; 117 | chomp ($date = ); 118 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 119 | { 120 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 121 | } 122 | } 123 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 124 | 125 | print "Please enter a comment for this transaction: "; 126 | chomp ($comment = ); 127 | 128 | push (@book, "DEP:${date}:${comment}:${amount}"); 129 | 130 | $balance += $amount; 131 | 132 | } 133 | elsif ($choice == 2) 134 | { 135 | print "Please enter the amount of the withdrawal (ex: 1234.56): "; 136 | $amount = ; 137 | 138 | while ($amount <= 0) 139 | { 140 | warn "The amount must be greater than 0\n"; 141 | print "Please enter the amount of the deposit (ex: 1234.56): "; 142 | $amount = ; 143 | } 144 | 145 | do 146 | { 147 | print "Please enter the today's date: "; 148 | chomp ($date = ); 149 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 150 | { 151 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 152 | } 153 | } 154 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 155 | 156 | print "Please enter a comment for this transaction: "; 157 | chomp ($comment = ); 158 | 159 | push (@book, "WD:${date}:${comment}:${amount}"); 160 | 161 | $balance -= $amount; 162 | } 163 | elsif ($choice == 3) 164 | { 165 | print "Please enter the amount of the deposit (ex: 1234.56): "; 166 | $amount = ; 167 | 168 | while ($amount <= 0) 169 | { 170 | warn "The amount must be greater than 0\n"; 171 | print "Please enter the amount of the deposit (ex: 1234.56): "; 172 | $amount = ; 173 | } 174 | 175 | do 176 | { 177 | print "Please enter the today's date: "; 178 | chomp ($date = ); 179 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 180 | { 181 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 182 | } 183 | } 184 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 185 | 186 | print "Please enter a comment for this transaction: "; 187 | chomp ($comment = ); 188 | 189 | print "Please enter the check number: "; 190 | chomp ($number = ); 191 | push (@book, "${number}:${date}:${comment}:${amount}"); 192 | 193 | $balance += $amount; 194 | } 195 | elsif ($choice == 4) 196 | { 197 | 198 | $match="false"; 199 | do 200 | { 201 | print "Please enter the check number you wish to look up: "; 202 | chomp ($number=); 203 | if (!($number =~ /^[0-9]+$/)) 204 | { 205 | print "Invalid number, please try again.\n"; 206 | } 207 | } 208 | until ($number =~ /^[0-9]+$/); 209 | 210 | foreach $data (@book) 211 | { 212 | $data =~ /^(.*):(.*):(.*):(.*)$/; 213 | if ($number eq $1) 214 | { 215 | print "Check number $number was written to $3 on $2 in the amount of $4\n"; 216 | $match = "true"; 217 | last; 218 | } 219 | } 220 | 221 | if ($match ne "true") 222 | { 223 | print "Check number $number was not found\n"; 224 | } 225 | print "Please press to continue"; 226 | $dummy = ; 227 | 228 | } 229 | elsif ($choice == 5) 230 | { 231 | 232 | 233 | } 234 | elsif ($choice == 6) 235 | { 236 | 237 | 238 | print "\n" x 5; 239 | 240 | print "Item Date Comment Amount\n"; 241 | print "--------------------------------------------------------\n"; 242 | 243 | foreach $data (@book) 244 | { 245 | $data =~ /^(.*):(.*):(.*):(.*)$/; 246 | write STDOUT; 247 | } 248 | 249 | print "--------------------------------------------------------\n"; 250 | print "Total $balance\n"; 251 | print "Press to continue:"; 252 | $dummy=; 253 | 254 | } 255 | elsif ($choice != 7) 256 | { 257 | warn"bad choice, must be between 1-7\n" 258 | } 259 | } 260 | 261 | #Exit program 262 | print "\nThanks for using Check_book!\n"; 263 | open (WFILE, ">$file"); 264 | print WFILE @book; 265 | close WFILE -------------------------------------------------------------------------------- /lab answers/cb11.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | 3 | # cb11.pl 4 | # Author: Your Name 5 | # Date created: 00-00-00 6 | # Descripton: This program will allow the user to keep track 7 | # of a personal checkbook including: Deposits, 8 | # withdrawals, checks writen, looking up checks 9 | # by check number or date writen, and printing a 10 | # statement. 11 | 12 | format STDOUT = 13 | @<<<<<<<<< @<<<<<<<<< @<<<<<<<<<<<<<<<<<<<< @########.## 14 | $1, $2, $3, $4 15 | . 16 | 17 | print "Welcome to Checkbook!\n"; 18 | print "Please enter your name:"; 19 | chomp ($name=); 20 | 21 | #Check for valid name: 22 | while ($name eq "") 23 | { 24 | warn "Sorry, did you enter your name\n"; 25 | print "Please enter your name:"; 26 | chomp ($name=); 27 | } 28 | 29 | $file="${name}.data"; 30 | print "Note: the data you enter will be stored in a plain text file.\nThis file will be called $file. \n\n"; 31 | 32 | if (-e $file) 33 | { 34 | 35 | print "The file $file already exists\n"; 36 | print "Please press to continue\n"; 37 | $dummy = ; 38 | 39 | open (RFILE, $file) || die "Could not open file"; 40 | @book = ; 41 | close (RFILE); 42 | 43 | foreach $data (@book) 44 | { 45 | # Using Split instead: 46 | @fields = split (/:/, $data); 47 | # $data =~ /(.*):.*:.*:(.*)/; 48 | if ($fields[0] eq "DEP") 49 | { 50 | $balance += $fields[3]; 51 | } 52 | else 53 | { 54 | $balance -= $fields[3]; 55 | } 56 | } 57 | } 58 | else 59 | { 60 | print "The file $file does not exist, so one will be made for you\n\n"; 61 | print "Please enter a beginning balance for this account: "; 62 | $balance=; 63 | 64 | while ($balance <= 0) 65 | { 66 | warn "The amount must be greater than 0\n"; 67 | print "Please enter the amount of the deposit (ex: 1234.56): "; 68 | $balance = ; 69 | } 70 | 71 | do 72 | { 73 | print "Please enter the today's date: "; 74 | chomp ($date = ); 75 | if (!($date =~ /^(10|11|12|0[1-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 76 | { 77 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 78 | } 79 | } 80 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 81 | 82 | $book[0] = "DEP:$date:Beginning Balance:$balance"; 83 | } 84 | 85 | while ($choice != 7) 86 | { 87 | system "cls"; #For Windows 88 | #system "clear"; #For Linux or Unix 89 | print "Your current balance is: $balance\n\n"; 90 | 91 | print "Please enter your choice:\n"; 92 | print "1. Enter a deposit\n"; 93 | print "2. Enter a withdrawal\n"; 94 | print "3. Enter a check\n"; 95 | print "4. Lookup a check by #\n"; 96 | print "5. Lookup a check by date\n"; 97 | print "6. Print a statement\n"; 98 | print "7. Exit program\n"; 99 | 100 | print "Please enter your choice> "; 101 | 102 | chomp ($choice = ); 103 | system "cls"; #For Windows 104 | #system "clear"; #For Linux or Unix 105 | 106 | if ($choice == 1) 107 | { 108 | print "Please enter the amount of the deposit (ex: 1234.56): "; 109 | $amount = ; 110 | 111 | while ($amount <= 0) 112 | { 113 | warn "The amount must be greater than 0\n"; 114 | print "Please enter the amount of the deposit (ex: 1234.56): "; 115 | $amount = ; 116 | } 117 | 118 | do 119 | { 120 | print "Please enter the today's date: "; 121 | chomp ($date = ); 122 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 123 | { 124 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 125 | } 126 | } 127 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 128 | 129 | print "Please enter a comment for this transaction: "; 130 | chomp ($comment = ); 131 | 132 | push (@book, "DEP:${date}:${comment}:${amount}"); 133 | 134 | $balance += $amount; 135 | 136 | } 137 | elsif ($choice == 2) 138 | { 139 | print "Please enter the amount of the withdrawal (ex: 1234.56): "; 140 | $amount = ; 141 | 142 | while ($amount <= 0) 143 | { 144 | warn "The amount must be greater than 0\n"; 145 | print "Please enter the amount of the deposit (ex: 1234.56): "; 146 | $amount = ; 147 | } 148 | 149 | do 150 | { 151 | print "Please enter the today's date: "; 152 | chomp ($date = ); 153 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 154 | { 155 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 156 | } 157 | } 158 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 159 | 160 | print "Please enter a comment for this transaction: "; 161 | chomp ($comment = ); 162 | 163 | push (@book, "WD:${date}:${comment}:${amount}"); 164 | 165 | $balance -= $amount; 166 | } 167 | elsif ($choice == 3) 168 | { 169 | print "Please enter the amount of the deposit (ex: 1234.56): "; 170 | $amount = ; 171 | 172 | while ($amount <= 0) 173 | { 174 | warn "The amount must be greater than 0\n"; 175 | print "Please enter the amount of the deposit (ex: 1234.56): "; 176 | $amount = ; 177 | } 178 | 179 | do 180 | { 181 | print "Please enter the today's date: "; 182 | chomp ($date = ); 183 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 184 | { 185 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 186 | } 187 | } 188 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 189 | 190 | print "Please enter a comment for this transaction: "; 191 | chomp ($comment = ); 192 | 193 | print "Please enter the check number: "; 194 | chomp ($number = ); 195 | push (@book, "${number}:${date}:${comment}:${amount}"); 196 | 197 | $balance += $amount; 198 | } 199 | elsif ($choice == 4) 200 | { 201 | 202 | $match="false"; 203 | do 204 | { 205 | print "Please enter the check number you wish to look up: "; 206 | chomp ($number=); 207 | if (!($number =~ /^[0-9]+$/)) 208 | { 209 | print "Invalid number, please try again.\n"; 210 | } 211 | } 212 | until ($number =~ /^[0-9]+$/); 213 | 214 | foreach $data (@book) 215 | { 216 | $data =~ /^(.*):(.*):(.*):(.*)$/; 217 | if ($number eq $1) 218 | { 219 | print "Check number $number was written to $3 on $2 in the amount of $4\n"; 220 | $match = "true"; 221 | last; 222 | } 223 | } 224 | 225 | if ($match ne "true") 226 | { 227 | print "Check number $number was not found\n"; 228 | } 229 | print "Please press to continue"; 230 | $dummy = ; 231 | 232 | } 233 | elsif ($choice == 5) 234 | { 235 | $match="false"; 236 | do 237 | { 238 | print "Please enter the date of the check you wish to look up (ex: 02/28/1999): "; 239 | chomp ($date=); 240 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 241 | { 242 | print "Invalid date, please try again.\n"; 243 | } 244 | } 245 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 246 | 247 | foreach $data (@book) 248 | { 249 | $data =~ /^(.*):(.*):(.*):(.*)$/; 250 | $amount = $4; 251 | $comment = $3; 252 | $number = $1; 253 | if (($date eq $2) && ($1 =~ /^[0-9]+$/)) 254 | { 255 | print "Check number $number was written to $comment on $date in the amount of $amount\n"; 256 | $match = "true"; 257 | } 258 | } 259 | 260 | if ($match eq "false") 261 | { 262 | print "No check were writen on $date\n"; 263 | } 264 | print "Please press to continue"; 265 | $dummy = ; 266 | 267 | } 268 | elsif ($choice == 6) 269 | { 270 | 271 | 272 | print "\n" x 5; 273 | 274 | print "Item Date Comment Amount\n"; 275 | print "--------------------------------------------------------\n"; 276 | 277 | foreach $data (@book) 278 | { 279 | $data =~ /^(.*):(.*):(.*):(.*)$/; 280 | write STDOUT; 281 | } 282 | 283 | print "--------------------------------------------------------\n"; 284 | print "Total $balance\n"; 285 | print "Press to continue:"; 286 | $dummy=; 287 | 288 | } 289 | elsif ($choice != 7) 290 | { 291 | warn "bad choice, must be between 1-7\n"; 292 | print "Press to continue:"; 293 | $dummy=; 294 | } 295 | } 296 | 297 | #Exit program 298 | print "\nThanks for using Check_book!\n"; 299 | open (WFILE, ">$file"); 300 | print WFILE @book; 301 | close WFILE; 302 | chmod (0600, "$file"); -------------------------------------------------------------------------------- /lab answers/cb12.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | 3 | # cb12.pl 4 | # Author: Your Name 5 | # Date created: 00-00-00 6 | # Descripton: This program will allow the user to keep track 7 | # of a personal checkbook including: Deposits, 8 | # withdrawals, checks writen, looking up checks 9 | # by check number or date writen, and printing a 10 | # statement. 11 | 12 | format STDOUT = 13 | @<<<<<<<<< @<<<<<<<<< @<<<<<<<<<<<<<<<<<<<< @########.## 14 | $1, $2, $3, $4 15 | . 16 | 17 | sub open_data { 18 | my $name; 19 | print "Welcome to Checkbook!\n"; 20 | print "Please enter your name:"; 21 | chomp ($name=); 22 | 23 | #Check for valid name: 24 | while ($name eq "") 25 | { 26 | warn "Sorry, did you enter your name\n"; 27 | print "Please enter your name:"; 28 | chomp ($name=); 29 | } 30 | 31 | $file="${name}.data"; 32 | print "Note: the data you enter will be stored in a plain text file.\nThis file will be called $file. \n\n"; 33 | 34 | if (-e $file) 35 | { 36 | 37 | print "The file $file already exists\n"; 38 | print "Please press to continue\n"; 39 | $dummy = ; 40 | 41 | open (RFILE, $file) || die "Could not open file"; 42 | @book = ; 43 | close (RFILE); 44 | 45 | foreach $data (@book) 46 | { 47 | # Using Split instead: 48 | @fields = split (/:/, $data); 49 | # $data =~ /(.*):.*:.*:(.*)/; 50 | if ($fields[0] eq "DEP") 51 | { 52 | $balance += $fields[3]; 53 | } 54 | else 55 | { 56 | $balance -= $fields[3]; 57 | } 58 | } 59 | } 60 | else 61 | { 62 | print "The file $file does not exist, so one will be made for you\n\n"; 63 | print "Please enter a beginning balance for this account: "; 64 | $balance=; 65 | 66 | while ($balance <= 0) 67 | { 68 | warn "The amount must be greater than 0\n"; 69 | print "Please enter the amount of the deposit (ex: 1234.56): "; 70 | $balance = ; 71 | } 72 | 73 | do 74 | { 75 | print "Please enter the today's date: "; 76 | chomp ($date = ); 77 | if (!($date =~ /^(10|11|12|0[1-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 78 | { 79 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 80 | } 81 | } 82 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 83 | 84 | $book[0] = "DEP:$date:Beginning Balance:$balance"; 85 | } 86 | } 87 | 88 | sub menu { 89 | my $pick; 90 | system "cls"; #For Windows 91 | #system "clear"; #For Linux or Unix 92 | print "Your current balance is: $balance\n\n"; 93 | 94 | print "Please enter your choice:\n"; 95 | print "1. Enter a deposit\n"; 96 | print "2. Enter a withdrawal\n"; 97 | print "3. Enter a check\n"; 98 | print "4. Lookup a check by #\n"; 99 | print "5. Lookup a check by date\n"; 100 | print "6. Print a statement\n"; 101 | print "7. Exit program\n"; 102 | 103 | print "Please enter your choice> "; 104 | 105 | chomp ($pick = ); 106 | return ($pick); 107 | } 108 | 109 | sub deposit { 110 | print "Please enter the amount of the deposit (ex: 1234.56): "; 111 | $amount = ; 112 | 113 | while ($amount <= 0) 114 | { 115 | warn "The amount must be greater than 0\n"; 116 | print "Please enter the amount of the deposit (ex: 1234.56): "; 117 | $amount = ; 118 | } 119 | 120 | do 121 | { 122 | print "Please enter the today's date: "; 123 | chomp ($date = ); 124 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 125 | { 126 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 127 | } 128 | } 129 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 130 | 131 | print "Please enter a comment for this transaction: "; 132 | chomp ($comment = ); 133 | 134 | push (@book, "DEP:${date}:${comment}:${amount}"); 135 | 136 | $balance += $amount; 137 | } 138 | 139 | sub withdrawal { 140 | print "Please enter the amount of the withdrawal (ex: 1234.56): "; 141 | $amount = ; 142 | 143 | while ($amount <= 0) 144 | { 145 | warn "The amount must be greater than 0\n"; 146 | print "Please enter the amount of the deposit (ex: 1234.56): "; 147 | $amount = ; 148 | } 149 | 150 | do 151 | { 152 | print "Please enter the today's date: "; 153 | chomp ($date = ); 154 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 155 | { 156 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 157 | } 158 | } 159 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 160 | 161 | print "Please enter a comment for this transaction: "; 162 | chomp ($comment = ); 163 | 164 | push (@book, "WD:${date}:${comment}:${amount}"); 165 | 166 | $balance -= $amount; 167 | } 168 | 169 | sub check { 170 | 171 | print "Please enter the amount of the deposit (ex: 1234.56): "; 172 | $amount = ; 173 | 174 | while ($amount <= 0) 175 | { 176 | warn "The amount must be greater than 0\n"; 177 | print "Please enter the amount of the deposit (ex: 1234.56): "; 178 | $amount = ; 179 | } 180 | 181 | do 182 | { 183 | print "Please enter the today's date: "; 184 | chomp ($date = ); 185 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 186 | { 187 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 188 | } 189 | } 190 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 191 | 192 | print "Please enter a comment for this transaction: "; 193 | chomp ($comment = ); 194 | 195 | print "Please enter the check number: "; 196 | chomp ($number = ); 197 | push (@book, "${number}:${date}:${comment}:${amount}"); 198 | 199 | $balance += $amount; 200 | 201 | } 202 | 203 | sub by_number { 204 | $match="false"; 205 | do 206 | { 207 | print "Please enter the check number you wish to look up: "; 208 | chomp ($number=); 209 | if (!($number =~ /^[0-9]+$/)) 210 | { 211 | print "Invalid number, please try again.\n"; 212 | } 213 | } 214 | until ($number =~ /^[0-9]+$/); 215 | 216 | foreach $data (@book) 217 | { 218 | $data =~ /^(.*):(.*):(.*):(.*)$/; 219 | if ($number eq $1) 220 | { 221 | print "Check number $number was written to $3 on $2 in the amount of $4\n"; 222 | $match = "true"; 223 | last; 224 | } 225 | } 226 | 227 | if ($match ne "true") 228 | { 229 | print "Check number $number was not found\n"; 230 | } 231 | print "Please press to continue"; 232 | $dummy = ; 233 | 234 | } 235 | 236 | sub by_date { 237 | $match="false"; 238 | do 239 | { 240 | print "Please enter the date of the check you wish to look up (ex: 02/28/1999): "; 241 | chomp ($date=); 242 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 243 | { 244 | print "Invalid date, please try again.\n"; 245 | } 246 | } 247 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 248 | 249 | foreach $data (@book) 250 | { 251 | $data =~ /^(.*):(.*):(.*):(.*)$/; 252 | $amount = $4; 253 | $comment = $3; 254 | $number = $1; 255 | if (($date eq $2) && ($1 =~ /^[0-9]+$/)) 256 | { 257 | print "Check number $number was written to $comment on $date in the amount of $amount\n"; 258 | $match = "true"; 259 | } 260 | } 261 | 262 | if ($match eq "false") 263 | { 264 | print "No check were writen on $date\n"; 265 | } 266 | print "Please press to continue"; 267 | $dummy = ; 268 | 269 | } 270 | 271 | sub statement { 272 | print "\n" x 5; 273 | 274 | print "Item Date Comment Amount\n"; 275 | print "--------------------------------------------------------\n"; 276 | 277 | foreach $data (@book) 278 | { 279 | $data =~ /^(.*):(.*):(.*):(.*)$/; 280 | write STDOUT; 281 | } 282 | 283 | print "--------------------------------------------------------\n"; 284 | print "Total $balance\n"; 285 | print "Press to continue:"; 286 | $dummy=; 287 | 288 | } 289 | 290 | &open_data; 291 | 292 | while ($choice != 7) 293 | { 294 | $choice=&menu; 295 | system "cls"; #For Windows 296 | #system "clear"; #For Linux or Unix 297 | 298 | if ($choice == 1) 299 | { 300 | &deposit; 301 | } 302 | elsif ($choice == 2) 303 | { 304 | &withdrawal; 305 | } 306 | elsif ($choice == 3) 307 | { 308 | ✓ 309 | } 310 | elsif ($choice == 4) 311 | { 312 | &by_number; 313 | } 314 | elsif ($choice == 5) 315 | { 316 | &by_date; 317 | } 318 | elsif ($choice == 6) 319 | { 320 | &statement; 321 | } 322 | elsif ($choice != 7) 323 | { 324 | warn "bad choice, must be between 1-7\n"; 325 | print "Press to continue:"; 326 | $dummy=; 327 | } 328 | } 329 | 330 | #Exit program 331 | print "\nThanks for using Check_book!\n"; 332 | open (WFILE, ">$file"); 333 | print WFILE @book; 334 | close WFILE; 335 | chmod (0600, "$file"); -------------------------------------------------------------------------------- /lab answers/cb13.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | 3 | # cb13.pl 4 | # Author: Your Name 5 | # Date created: 00-00-00 6 | # Descripton: This program will allow the user to keep track 7 | # of a personal checkbook including: Deposits, 8 | # withdrawals, checks writen, looking up checks 9 | # by check number or date writen, and printing a 10 | # statement. 11 | 12 | #use diagnostics; 13 | use strict "subs"; 14 | use File::Copy; 15 | 16 | format STDOUT = 17 | @<<<<<<<<< @<<<<<<<<< @<<<<<<<<<<<<<<<<<<<< @########.## 18 | $1, $2, $3, $4 19 | . 20 | 21 | sub open_data { 22 | my $name, $backup; 23 | print "Welcome to Checkbook!\n"; 24 | print "Please enter your name:"; 25 | chomp ($name=); 26 | 27 | #Check for valid name: 28 | while ($name eq "") 29 | { 30 | warn "Sorry, did you enter your name\n"; 31 | print "Please enter your name:"; 32 | chomp ($name=); 33 | } 34 | 35 | $file="${name}.data"; 36 | $backup="${file}.bak"; 37 | 38 | print "Note: the data you enter will be stored in a plain text file.\nThis file will be called $file. \n"; 39 | 40 | 41 | if (-e $file) 42 | { 43 | 44 | print "The file $file already exists\n"; 45 | print "Please press to continue\n"; 46 | $dummy = ; 47 | 48 | print "A backup of your original data will be stores in a plain text file named $backup \n\n"; 49 | copy ($file, $backup); 50 | 51 | open (RFILE, $file) || die "Could not open file"; 52 | @book = ; 53 | close (RFILE); 54 | 55 | foreach $data (@book) 56 | { 57 | # Using Split instead: 58 | @fields = split (/:/, $data); 59 | # $data =~ /(.*):.*:.*:(.*)/; 60 | if ($fields[0] eq "DEP") 61 | { 62 | $balance += $fields[3]; 63 | } 64 | else 65 | { 66 | $balance -= $fields[3]; 67 | } 68 | } 69 | } 70 | else 71 | { 72 | print "The file $file does not exist, so one will be made for you\n\n"; 73 | print "Please enter a beginning balance for this account: "; 74 | $balance=; 75 | 76 | while ($balance <= 0) 77 | { 78 | warn "The amount must be greater than 0\n"; 79 | print "Please enter the amount of the deposit (ex: 1234.56): "; 80 | $balance = ; 81 | } 82 | 83 | do 84 | { 85 | print "Please enter the today's date: "; 86 | chomp ($date = ); 87 | if (!($date =~ /^(10|11|12|0[1-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 88 | { 89 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 90 | } 91 | } 92 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 93 | 94 | $book[0] = "DEP:$date:Beginning Balance:$balance"; 95 | } 96 | } 97 | 98 | sub menu { 99 | my $pick; 100 | system "cls"; #For Windows 101 | #system "clear"; #For Linux or Unix 102 | print "Your current balance is: $balance\n\n"; 103 | 104 | print "Please enter your choice:\n"; 105 | print "1. Enter a deposit\n"; 106 | print "2. Enter a withdrawal\n"; 107 | print "3. Enter a check\n"; 108 | print "4. Lookup a check by #\n"; 109 | print "5. Lookup a check by date\n"; 110 | print "6. Print a statement\n"; 111 | print "7. Exit program\n"; 112 | 113 | print "Please enter your choice> "; 114 | 115 | chomp ($pick = ); 116 | return ($pick); 117 | } 118 | 119 | sub deposit { 120 | print "Please enter the amount of the deposit (ex: 1234.56): "; 121 | $amount = ; 122 | 123 | while ($amount <= 0) 124 | { 125 | warn "The amount must be greater than 0\n"; 126 | print "Please enter the amount of the deposit (ex: 1234.56): "; 127 | $amount = ; 128 | } 129 | 130 | do 131 | { 132 | print "Please enter the today's date: "; 133 | chomp ($date = ); 134 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 135 | { 136 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 137 | } 138 | } 139 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 140 | 141 | print "Please enter a comment for this transaction: "; 142 | chomp ($comment = ); 143 | 144 | push (@book, "DEP:${date}:${comment}:${amount}"); 145 | 146 | $balance += $amount; 147 | } 148 | 149 | sub withdrawal { 150 | print "Please enter the amount of the withdrawal (ex: 1234.56): "; 151 | $amount = ; 152 | 153 | while ($amount <= 0) 154 | { 155 | warn "The amount must be greater than 0\n"; 156 | print "Please enter the amount of the deposit (ex: 1234.56): "; 157 | $amount = ; 158 | } 159 | 160 | do 161 | { 162 | print "Please enter the today's date: "; 163 | chomp ($date = ); 164 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 165 | { 166 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 167 | } 168 | } 169 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 170 | 171 | print "Please enter a comment for this transaction: "; 172 | chomp ($comment = ); 173 | 174 | push (@book, "WD:${date}:${comment}:${amount}"); 175 | 176 | $balance -= $amount; 177 | } 178 | 179 | sub check { 180 | 181 | print "Please enter the amount of the deposit (ex: 1234.56): "; 182 | $amount = ; 183 | 184 | while ($amount <= 0) 185 | { 186 | warn "The amount must be greater than 0\n"; 187 | print "Please enter the amount of the deposit (ex: 1234.56): "; 188 | $amount = ; 189 | } 190 | 191 | do 192 | { 193 | print "Please enter the today's date: "; 194 | chomp ($date = ); 195 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 196 | { 197 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 198 | } 199 | } 200 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 201 | 202 | print "Please enter a comment for this transaction: "; 203 | chomp ($comment = ); 204 | 205 | print "Please enter the check number: "; 206 | chomp ($number = ); 207 | push (@book, "${number}:${date}:${comment}:${amount}"); 208 | 209 | $balance += $amount; 210 | 211 | } 212 | 213 | sub by_number { 214 | $match="false"; 215 | do 216 | { 217 | print "Please enter the check number you wish to look up: "; 218 | chomp ($number=); 219 | if (!($number =~ /^[0-9]+$/)) 220 | { 221 | print "Invalid number, please try again.\n"; 222 | } 223 | } 224 | until ($number =~ /^[0-9]+$/); 225 | 226 | foreach $data (@book) 227 | { 228 | $data =~ /^(.*):(.*):(.*):(.*)$/; 229 | if ($number eq $1) 230 | { 231 | print "Check number $number was written to $3 on $2 in the amount of $4\n"; 232 | $match = "true"; 233 | last; 234 | } 235 | } 236 | 237 | if ($match ne "true") 238 | { 239 | print "Check number $number was not found\n"; 240 | } 241 | print "Please press to continue"; 242 | $dummy = ; 243 | 244 | } 245 | 246 | sub by_date { 247 | $match="false"; 248 | do 249 | { 250 | print "Please enter the date of the check you wish to look up (ex: 02/28/1999): "; 251 | chomp ($date=); 252 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 253 | { 254 | print "Invalid date, please try again.\n"; 255 | } 256 | } 257 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 258 | 259 | foreach $data (@book) 260 | { 261 | $data =~ /^(.*):(.*):(.*):(.*)$/; 262 | $amount = $4; 263 | $comment = $3; 264 | $number = $1; 265 | if (($date eq $2) && ($1 =~ /^[0-9]+$/)) 266 | { 267 | print "Check number $number was written to $comment on $date in the amount of $amount\n"; 268 | $match = "true"; 269 | } 270 | } 271 | 272 | if ($match eq "false") 273 | { 274 | print "No check were writen on $date\n"; 275 | } 276 | print "Please press to continue"; 277 | $dummy = ; 278 | 279 | } 280 | 281 | sub statement { 282 | print "\n" x 5; 283 | 284 | print "Item Date Comment Amount\n"; 285 | print "--------------------------------------------------------\n"; 286 | 287 | foreach $data (@book) 288 | { 289 | $data =~ /^(.*):(.*):(.*):(.*)$/; 290 | write STDOUT; 291 | } 292 | 293 | print "--------------------------------------------------------\n"; 294 | print "Total $balance\n"; 295 | print "Press to continue:"; 296 | $dummy=; 297 | 298 | } 299 | 300 | &open_data; 301 | 302 | while ($choice != 7) 303 | { 304 | $choice=&menu; 305 | system "cls"; #For Windows 306 | #system "clear"; #For Linux or Unix 307 | 308 | if ($choice == 1) 309 | { 310 | &deposit; 311 | } 312 | elsif ($choice == 2) 313 | { 314 | &withdrawal; 315 | } 316 | elsif ($choice == 3) 317 | { 318 | ✓ 319 | } 320 | elsif ($choice == 4) 321 | { 322 | &by_number; 323 | } 324 | elsif ($choice == 5) 325 | { 326 | &by_date; 327 | } 328 | elsif ($choice == 6) 329 | { 330 | &statement; 331 | } 332 | elsif ($choice != 7) 333 | { 334 | warn "bad choice, must be between 1-7\n"; 335 | print "Press to continue:"; 336 | $dummy=; 337 | } 338 | } 339 | 340 | #Exit program 341 | print "\nThanks for using Check_book!\n"; 342 | open (WFILE, ">$file"); 343 | print WFILE @book; 344 | close WFILE; 345 | chmod (0600, "$file"); -------------------------------------------------------------------------------- /lab answers/cb2.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | 3 | # cb2.pl 4 | # Author: Your Name 5 | # Date created: 00-00-00 6 | # Descripton: This program will allow the user to keep track 7 | # of a personal checkbook including: Deposits, 8 | # withdrawals, checks writen, looking up checks 9 | # by check number or date writen, and printing a 10 | # statement. 11 | 12 | print "Welcome to Checkbook!\n"; 13 | print "Please enter your name:"; 14 | chomp ($name=); 15 | 16 | print "Your current balance is: 100\n\n"; 17 | 18 | print "Please enter your choice:\n"; 19 | print "1. Enter a deposit\n"; 20 | print "2. Enter a withdrawal\n"; 21 | print "3. Enter a check\n"; 22 | print "4. Lookup a check by #\n"; 23 | print "5. Lookup a check by date\n"; 24 | print "6. Print a statement\n"; 25 | print "7. Exit program\n"; 26 | 27 | print "Please enter your choice> "; 28 | 29 | chomp ($choice = ); -------------------------------------------------------------------------------- /lab answers/cb3.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | 3 | # cb3.pl 4 | # Author: Your Name 5 | # Date created: 00-00-00 6 | # Descripton: This program will allow the user to keep track 7 | # of a personal checkbook including: Deposits, 8 | # withdrawals, checks writen, looking up checks 9 | # by check number or date writen, and printing a 10 | # statement. 11 | 12 | print "Welcome to Checkbook!\n"; 13 | print "Please enter your name:"; 14 | chomp ($name=); 15 | 16 | @book=("DEP:12/12/1999:Beginning Balance:100"); 17 | 18 | print "Your current balance is: 100\n\n"; 19 | 20 | print "Please enter your choice:\n"; 21 | print "1. Enter a deposit\n"; 22 | print "2. Enter a withdrawal\n"; 23 | print "3. Enter a check\n"; 24 | print "4. Lookup a check by #\n"; 25 | print "5. Lookup a check by date\n"; 26 | print "6. Print a statement\n"; 27 | print "7. Exit program\n"; 28 | 29 | print "Please enter your choice> "; 30 | 31 | chomp ($choice = ); -------------------------------------------------------------------------------- /lab answers/cb5.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | 3 | # cb5.pl 4 | # Author: Your Name 5 | # Date created: 00-00-00 6 | # Descripton: This program will allow the user to keep track 7 | # of a personal checkbook including: Deposits, 8 | # withdrawals, checks writen, looking up checks 9 | # by check number or date writen, and printing a 10 | # statement. 11 | 12 | print "Welcome to Checkbook!\n"; 13 | print "Please enter your name:"; 14 | chomp ($name=); 15 | 16 | @book=("DEP:12/12/1999:Beginning Balance:100"); 17 | 18 | while ($choice != 7) #until ($choice == 7) 19 | { 20 | print "Your current balance is: 100\n\n"; 21 | 22 | print "Please enter your choice:\n"; 23 | print "1. Enter a deposit\n"; 24 | print "2. Enter a withdrawal\n"; 25 | print "3. Enter a check\n"; 26 | print "4. Lookup a check by #\n"; 27 | print "5. Lookup a check by date\n"; 28 | print "6. Print a statement\n"; 29 | print "7. Exit program\n"; 30 | 31 | print "Please enter your choice> "; 32 | 33 | chomp ($choice = ); 34 | 35 | if ($choice == 1) 36 | { 37 | print "Please enter the amount of the deposit (ex: 1234.56): "; 38 | $amount = ; 39 | 40 | print "Please enter the date of the deposit: "; 41 | chomp ($date = ); 42 | 43 | print "Please enter a comment for this transaction: "; 44 | chomp ($comment = ); 45 | 46 | push (@book, "DEP:${date}:${comment}:${amount}"); 47 | 48 | 49 | } 50 | elsif ($choice == 2) 51 | { 52 | print "Please enter the amount of the withdrawal (ex: 1234.56): "; 53 | $amount = ; 54 | 55 | print "Please enter the date of the withdrawal: "; 56 | chomp ($date = ); 57 | 58 | print "Please enter a comment for this transaction: "; 59 | chomp ($comment = ); 60 | 61 | push (@book, "WD:${date}:${comment}:${amount}"); 62 | 63 | } 64 | elsif ($choice == 3) 65 | { 66 | print "Please enter the amount of the check (ex: 1234.56): "; 67 | $amount = ; 68 | 69 | print "Please enter the date of the check: "; 70 | chomp ($date = ); 71 | 72 | print "Please enter a comment for this transaction: "; 73 | chomp ($comment = ); 74 | 75 | print "Please enter the check number: "; 76 | chomp ($number = ); 77 | 78 | push (@book, "${number}:${date}:${comment}:${amount}"); 79 | 80 | } 81 | elsif ($choice == 4) 82 | { 83 | print "You choose 4"; 84 | } 85 | elsif ($choice == 5) 86 | { 87 | print "You choose 5"; 88 | } 89 | elsif ($choice == 6) 90 | { 91 | #This was not a lab requirement, but it makes it easier 92 | #to see if the program is working: 93 | foreach $element (@book) { 94 | print "$element\n";} 95 | print "\n"x3; 96 | } 97 | } 98 | 99 | #Exit program 100 | print "\nThanks for using Check_book!\n"; 101 | -------------------------------------------------------------------------------- /lab answers/cb6.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | 3 | # cb6.pl 4 | # Author: Your Name 5 | # Date created: 00-00-00 6 | # Descripton: This program will allow the user to keep track 7 | # of a personal checkbook including: Deposits, 8 | # withdrawals, checks writen, looking up checks 9 | # by check number or date writen, and printing a 10 | # statement. 11 | 12 | print "Welcome to Checkbook!\n"; 13 | print "Please enter your name:"; 14 | chomp ($name=); 15 | 16 | #Check for valid name: 17 | while ($name eq "") 18 | { 19 | print "Sorry, did you enter your name\n"; 20 | print "Please enter your name:"; 21 | chomp ($name=); 22 | } 23 | 24 | @book="DEP:12/12/1999:Beginning Balance:100"; 25 | 26 | while ($choice != 7) 27 | { 28 | print "Your current balance is: 100\n\n"; 29 | 30 | print "Please enter your choice:\n"; 31 | print "1. Enter a deposit\n"; 32 | print "2. Enter a withdrawal\n"; 33 | print "3. Enter a check\n"; 34 | print "4. Lookup a check by #\n"; 35 | print "5. Lookup a check by date\n"; 36 | print "6. Print a statement\n"; 37 | print "7. Exit program\n"; 38 | 39 | print "Please enter your choice> "; 40 | 41 | chomp ($choice = ); 42 | 43 | if ($choice == 1) 44 | { 45 | print "Please enter the amount of the deposit (ex: 1234.56): "; 46 | $amount = ; 47 | 48 | #Check for valid amount: 49 | while ($amount <= 0) 50 | { 51 | print "The amount must be greater than 0\n"; 52 | print "Please enter the amount of the deposit (ex: 1234.56): "; 53 | $amount = ; 54 | } 55 | 56 | print "Please enter the date of the deposit: "; 57 | chomp ($date = ); 58 | 59 | print "Please enter a comment for this transaction: "; 60 | chomp ($comment = ); 61 | 62 | push (@book, "DEP:${date}:${comment}:${amount}"); 63 | 64 | 65 | } 66 | elsif ($choice == 2) 67 | { 68 | print "Please enter the amount of the withdrawal (ex: 1234.56): "; 69 | $amount = ; 70 | 71 | while ($amount <= 0) 72 | { 73 | print "The amount must be greater than 0\n"; 74 | print "Please enter the amount of the deposit (ex: 1234.56): "; 75 | $amount = ; 76 | } 77 | 78 | print "Please enter the date of the withdrawal: "; 79 | chomp ($date = ); 80 | 81 | print "Please enter a comment for this transaction: "; 82 | chomp ($comment = ); 83 | 84 | push (@book, "WD:${date}:${comment}:${amount}"); 85 | 86 | } 87 | elsif ($choice == 3) 88 | { 89 | print "Please enter the amount of the deposit (ex: 1234.56): "; 90 | $amount = ; 91 | 92 | while ($amount <= 0) 93 | { 94 | print "The amount must be greater than 0\n"; 95 | print "Please enter the amount of the deposit (ex: 1234.56): "; 96 | $amount = ; 97 | } 98 | 99 | print "Please enter the date of the check: "; 100 | chomp ($date = ); 101 | 102 | print "Please enter a comment for this transaction: "; 103 | chomp ($comment = ); 104 | 105 | print "Please enter the check number: "; 106 | chomp ($number = ); 107 | push (@book, "${number}:${date}:${comment}:${amount}"); 108 | 109 | } 110 | elsif ($choice == 4) 111 | { 112 | print "You choose 4"; 113 | } 114 | elsif ($choice == 5) 115 | { 116 | print "You choose 5"; 117 | } 118 | elsif ($choice == 6) 119 | { 120 | #This was not a lab requirement, but it makes it easier 121 | #to see if the program is working: 122 | foreach $element (@book) { 123 | print "$element\n";} 124 | print "\n"x3; 125 | } 126 | #Check for valid choice: 127 | elsif ($choice != 7) 128 | { 129 | print "bad choice, must be between 1-7\n" 130 | } 131 | } 132 | 133 | #Exit program 134 | print "\nThanks for using Check_book!\n" 135 | -------------------------------------------------------------------------------- /lab answers/cb8.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | 3 | # cb8.pl 4 | # Author: Your Name 5 | # Date created: 00-00-00 6 | # Descripton: This program will allow the user to keep track 7 | # of a personal checkbook including: Deposits, 8 | # withdrawals, checks writen, looking up checks 9 | # by check number or date writen, and printing a 10 | # statement. 11 | 12 | print "Welcome to Checkbook!\n"; 13 | print "Please enter your name:"; 14 | chomp ($name=); 15 | 16 | #Check for valid name: 17 | while ($name eq "") 18 | { 19 | warn "Sorry, did you enter your name\n"; 20 | print "Please enter your name:"; 21 | chomp ($name=); 22 | } 23 | 24 | $file="${name}.data"; 25 | print "Note: the data you enter will be stored in a plain text file.\nThis file will be called $file. \n\n"; 26 | 27 | if (-e $file) 28 | { 29 | 30 | print "The file $file already exists\n"; 31 | print "Please press to continue\n"; 32 | $dummy = ; 33 | 34 | open (RFILE, $file) || die "Could not open file"; 35 | @book = ; 36 | close (RFILE); 37 | } 38 | else 39 | { 40 | print "The file $file does not exist, so one will be made for you\n\n"; 41 | print "Please enter a beginning balance for this account: "; 42 | $balance=; 43 | 44 | while ($balance <= 0) 45 | { 46 | warn "The amount must be greater than 0\n"; 47 | print "Please enter the amount of the deposit (ex: 1234.56): "; 48 | $balance = ; 49 | } 50 | 51 | print "Please enter today's date: "; 52 | chomp ($date=); 53 | @book = ("DEP:$date:Beginning Balance:$balance"); 54 | } 55 | 56 | while ($choice != 7) 57 | { 58 | print "Your current balance is: 100\n\n"; 59 | 60 | print "Please enter your choice:\n"; 61 | print "1. Enter a deposit\n"; 62 | print "2. Enter a withdrawal\n"; 63 | print "3. Enter a check\n"; 64 | print "4. Lookup a check by #\n"; 65 | print "5. Lookup a check by date\n"; 66 | print "6. Print a statement\n"; 67 | print "7. Exit program\n"; 68 | 69 | print "Please enter your choice> "; 70 | 71 | chomp ($choice = ); 72 | 73 | if ($choice == 1) 74 | { 75 | print "Please enter the amount of the deposit (ex: 1234.56): "; 76 | $amount = ; 77 | 78 | #Check for valid amount: 79 | while ($amount <= 0) 80 | { 81 | warn "The amount must be greater than 0\n"; 82 | print "Please enter the amount of the deposit (ex: 1234.56): "; 83 | $amount = ; 84 | } 85 | 86 | print "Please enter the date of the deposit: "; 87 | chomp ($date = ); 88 | 89 | print "Please enter a comment for this transaction: "; 90 | chomp ($comment = ); 91 | 92 | push (@book, "DEP:${date}:${comment}:${amount}"); 93 | 94 | 95 | } 96 | elsif ($choice == 2) 97 | { 98 | print "Please enter the amount of the withdrawal (ex: 1234.56): "; 99 | $amount = ; 100 | 101 | while ($amount <= 0) 102 | { 103 | warn "The amount must be greater than 0\n"; 104 | print "Please enter the amount of the deposit (ex: 1234.56): "; 105 | $amount = ; 106 | } 107 | 108 | print "Please enter the date of the withdrawal: "; 109 | chomp ($date = ); 110 | 111 | print "Please enter a comment for this transaction: "; 112 | chomp ($comment = ); 113 | 114 | push (@book, "WD:${date}:${comment}:${amount}"); 115 | 116 | } 117 | elsif ($choice == 3) 118 | { 119 | print "Please enter the amount of the deposit (ex: 1234.56): "; 120 | $amount = ; 121 | 122 | while ($amount <= 0) 123 | { 124 | warn "The amount must be greater than 0\n"; 125 | print "Please enter the amount of the deposit (ex: 1234.56): "; 126 | $amount = ; 127 | } 128 | 129 | print "Please enter the date of the check: "; 130 | chomp ($date = ); 131 | 132 | print "Please enter a comment for this transaction: "; 133 | chomp ($comment = ); 134 | 135 | print "Please enter the check number: "; 136 | chomp ($number = ); 137 | push (@book, "${number}:${date}:${comment}:${amount}"); 138 | 139 | } 140 | elsif ($choice == 4) 141 | { 142 | print "You choose 4"; 143 | } 144 | elsif ($choice == 5) 145 | { 146 | print "You choose 5"; 147 | } 148 | elsif ($choice == 6) 149 | { 150 | #This was not a lab requirement, but it makes it easier 151 | #to see if the program is working: 152 | foreach $element (@book) { 153 | print "$element\n";} 154 | print "\n"x3; 155 | } 156 | #Check for valid choice: 157 | elsif ($choice != 7) 158 | { 159 | warn "bad choice, must be between 1-7\n" 160 | } 161 | } 162 | 163 | #Exit program 164 | print "\nThanks for using Check_book!\n"; 165 | 166 | open (WFILE, ">$file"); 167 | print WFILE @book; 168 | #or: foreach $element (@book) {print WFILE "$element\n"} 169 | close WFILE; 170 | -------------------------------------------------------------------------------- /lab answers/cb9.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | 3 | # cb9.pl 4 | # Author: Your Name 5 | # Date created: 00-00-00 6 | # Descripton: This program will allow the user to keep track 7 | # of a personal checkbook including: Deposits, 8 | # withdrawals, checks writen, looking up checks 9 | # by check number or date writen, and printing a 10 | # statement. 11 | 12 | format STDOUT = 13 | @<<<<<<<<< @<<<<<<<<< @<<<<<<<<<<<<<<<<<<<< @########.## 14 | $1, $2, $3, $4 15 | . 16 | 17 | print "Welcome to Checkbook!\n"; 18 | print "Please enter your name:"; 19 | chomp ($name=); 20 | 21 | #Check for valid name: 22 | while ($name eq "") 23 | { 24 | warn "Sorry, did you enter your name\n"; 25 | print "Please enter your name:"; 26 | chomp ($name=); 27 | } 28 | 29 | $file="${name}.data"; 30 | print "Note: the data you enter will be stored in a plain text file.\nThis file will be called $file. \n\n"; 31 | 32 | if (-e $file) 33 | { 34 | 35 | print "The file $file already exists\n"; 36 | print "Please press to continue\n"; 37 | $dummy = ; 38 | 39 | open (RFILE, $file) || die "Could not open file"; 40 | @book = ; 41 | close (RFILE); 42 | 43 | foreach $data (@book) 44 | { 45 | $data =~ /(.*):.*:.*:(.*)/; 46 | if ($1 eq "DEP") 47 | { 48 | $balance += $2; 49 | } 50 | else 51 | { 52 | $balance -= $2; 53 | } 54 | } 55 | } 56 | else 57 | { 58 | print "The file $file does not exist, so one will be made for you\n\n"; 59 | print "Please enter a beginning balance for this account: "; 60 | $balance=; 61 | 62 | while ($balance <= 0) 63 | { 64 | warn "The amount must be greater than 0\n"; 65 | print "Please enter the amount of the deposit (ex: 1234.56): "; 66 | $balance = ; 67 | } 68 | 69 | do 70 | { 71 | print "Please enter the today's date: "; 72 | chomp ($date = ); 73 | if (!($date =~ /^(10|11|12|0[1-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 74 | { 75 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 76 | } 77 | } 78 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 79 | 80 | $book[0] = "DEP:$date:Beginning Balance:$balance"; 81 | } 82 | 83 | while ($choice != 7) 84 | { 85 | print "Your current balance is: $balance\n\n"; 86 | 87 | print "Please enter your choice:\n"; 88 | print "1. Enter a deposit\n"; 89 | print "2. Enter a withdrawal\n"; 90 | print "3. Enter a check\n"; 91 | print "4. Lookup a check by #\n"; 92 | print "5. Lookup a check by date\n"; 93 | print "6. Print a statement\n"; 94 | print "7. Exit program\n"; 95 | 96 | print "Please enter your choice> "; 97 | 98 | chomp ($choice = ); 99 | 100 | if ($choice == 1) 101 | { 102 | print "Please enter the amount of the deposit (ex: 1234.56): "; 103 | $amount = ; 104 | 105 | while ($amount <= 0) 106 | { 107 | warn "The amount must be greater than 0\n"; 108 | print "Please enter the amount of the deposit (ex: 1234.56): "; 109 | $amount = ; 110 | } 111 | 112 | do 113 | { 114 | print "Please enter the today's date: "; 115 | chomp ($date = ); 116 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 117 | { 118 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 119 | } 120 | } 121 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 122 | 123 | print "Please enter a comment for this transaction: "; 124 | chomp ($comment = ); 125 | 126 | push (@book, "DEP:${date}:${comment}:${amount}"); 127 | 128 | $balance += $amount; 129 | 130 | } 131 | elsif ($choice == 2) 132 | { 133 | print "Please enter the amount of the withdrawal (ex: 1234.56): "; 134 | $amount = ; 135 | 136 | while ($amount <= 0) 137 | { 138 | warn "The amount must be greater than 0\n"; 139 | print "Please enter the amount of the deposit (ex: 1234.56): "; 140 | $amount = ; 141 | } 142 | 143 | do 144 | { 145 | print "Please enter the today's date: "; 146 | chomp ($date = ); 147 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 148 | { 149 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 150 | } 151 | } 152 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 153 | 154 | print "Please enter a comment for this transaction: "; 155 | chomp ($comment = ); 156 | 157 | push (@book, "WD:${date}:${comment}:${amount}"); 158 | 159 | $balance -= $amount; 160 | } 161 | elsif ($choice == 3) 162 | { 163 | print "Please enter the amount of the deposit (ex: 1234.56): "; 164 | $amount = ; 165 | 166 | while ($amount <= 0) 167 | { 168 | warn "The amount must be greater than 0\n"; 169 | print "Please enter the amount of the deposit (ex: 1234.56): "; 170 | $amount = ; 171 | } 172 | 173 | do 174 | { 175 | print "Please enter the today's date: "; 176 | chomp ($date = ); 177 | if (!($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/)) 178 | { 179 | print "Invalid format, please retype (ex: 02/28/1999):\n"; 180 | } 181 | } 182 | until ($date =~ /^(10|11|12|0[0-9])\/(0[1-9]|[12][0-9]|30|31)\/[0-9]{4}$/); 183 | 184 | print "Please enter a comment for this transaction: "; 185 | chomp ($comment = ); 186 | 187 | print "Please enter the check number: "; 188 | chomp ($number = ); 189 | push (@book, "${number}:${date}:${comment}:${amount}"); 190 | 191 | $balance += $amount; 192 | } 193 | elsif ($choice == 4) 194 | { 195 | 196 | 197 | } 198 | elsif ($choice == 5) 199 | { 200 | 201 | 202 | } 203 | elsif ($choice == 6) 204 | { 205 | 206 | 207 | print "\n" x 5; 208 | 209 | print "Item Date Comment Amount\n"; 210 | print "--------------------------------------------------------\n"; 211 | 212 | foreach $data (@book) 213 | { 214 | $data =~ /^(.*):(.*):(.*):(.*)$/; 215 | write STDOUT; 216 | } 217 | 218 | print "--------------------------------------------------------\n"; 219 | print "Total $balance\n"; 220 | print "Press to continue:"; 221 | $dummy=; 222 | 223 | } 224 | elsif ($choice != 7) 225 | { 226 | warn"bad choice, must be between 1-7\n" 227 | } 228 | } 229 | 230 | #Exit program 231 | print "\nThanks for using Check_book!\n"; 232 | open (WFILE, ">$file"); 233 | print WFILE @book; 234 | close WFILE -------------------------------------------------------------------------------- /lab answers/paragraph.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #paragraph.pl 3 | 4 | print "Enter one sentence at a time, pressing the key after each sentence. On Linux/Unix, press Control-d to finish. On Windows, press Control-z + the key: \n"; 5 | 6 | while () { 7 | chomp; 8 | $total .= $_; 9 | $total .= " "; 10 | } 11 | 12 | print "Your paragraph is:\n$total\n"; -------------------------------------------------------------------------------- /lab answers/phone.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #start.pl 3 | 4 | #General note: Normally we would do more error checking. That has been omitted for this lab 5 | 6 | # The following will open a data file and read the information into a hash. The key will be the person's 7 | # name and the value will be the person's phone number. This code will be explained in more detail 8 | # in class and in later units of this course. Initally this file doesn't exist, so the following code will 9 | # initially do nothing 10 | 11 | open (DATA, "); 13 | close DATA; 14 | 15 | $choice=0; 16 | 17 | while ($choice != 6) { 18 | print "Enter a choice from the following options:\n\n"; 19 | print "#1. Add a phone number\n"; 20 | print "#2. Lookup a phone number\n"; 21 | print "#3. Delete a phone number\n"; 22 | print "#4. Print all phone numbers\n"; 23 | print "#5. Check if a person has a phone number\n"; 24 | print "#6. Exit the program\n"; 25 | print "\nEnter your choice: "; 26 | chomp ($choice=); 27 | 28 | if ($choice == 1) { 29 | print "Enter the name to add: "; 30 | chomp ($name=); 31 | print "Enter ${name}'s phone number: "; 32 | chomp ($number=); 33 | $phone{$name}=$number; 34 | } elsif ($choice == 2) { 35 | print "Enter the name to look up: "; 36 | chomp ($name=); 37 | print "\n\nEnter ${name}'s phone number is $phone{$name}\n\n"; 38 | print "Press ENTER to continue"; 39 | $dummy=; 40 | print "\n\n"; 41 | } elsif ($choice == 3) { 42 | print "Enter the name to delete: "; 43 | chomp ($name=); 44 | if (exists $phone{$name}) { 45 | delete $phone{$name}; 46 | print "${name}'s phone number has been deleted\n"; 47 | } else { 48 | print "$name has no entry in the database\n"; 49 | } 50 | print "Press ENTER to continue"; 51 | $dummy=; 52 | print "\n\n"; 53 | } elsif ($choice == 4) { 54 | print "\n"; 55 | while (($name, $number) = each (%phone)) { 56 | print "${name}'s number is $phone{$name}\n"; 57 | } 58 | print "Press ENTER to continue"; 59 | $dummy=; 60 | print "\n\n"; 61 | } elsif ($choice == 5) { 62 | print "Enter the name to check: "; 63 | chomp ($name=); 64 | if (exists $phone{$name}) { 65 | print "$name is in the database\n"; 66 | } else { 67 | print "$name has no entry in the database\n"; 68 | } 69 | print "Press ENTER to continue"; 70 | $dummy=; 71 | print "\n\n"; 72 | } 73 | 74 | } 75 | 76 | #This part saves the data to the file 77 | open (DATA, ">phone.txt"); 78 | $output=join (":", %phone); 79 | print DATA $output; 80 | close DATA; 81 | 82 | -------------------------------------------------------------------------------- /lab answers/start.pl: -------------------------------------------------------------------------------- 1 | #!perl 2 | #start.pl 3 | 4 | #General note: Normally we would do more error checking. That has been omitted for this lab 5 | 6 | # The following will open a data file and read the information into a hash. The key will be the person's 7 | # name and the value will be the person's phone number. This code will be explained in more detail 8 | # in class and in later units of this course. Initally this file doesn't exist, so the following code will 9 | # initially do nothing 10 | 11 | open (DATA, "); 13 | close DATA; 14 | 15 | $choice=0; 16 | 17 | while ($choice != 6) { 18 | print "Enter a choice from the following options:\n\n"; 19 | print "#1 Add a phone number\n"; 20 | print "#2 Lookup a phone number\n"; 21 | print "#3 Delete a phone number\n"; 22 | print "#4 Print all phone numbers\n"; 23 | print "#5. Check if a person has a phone number\n"; 24 | print "#6. Exit the program\n"; 25 | print "\nEnter your choice: "; 26 | chomp ($choice=); 27 | 28 | if ($choice == 1) { 29 | #Enter your code here for "Add a phone number"; 30 | } elsif ($choice == 2) { 31 | #Enter your code here for Lookup a phone number"; 32 | } elsif ($choice == 3) { 33 | #Enter your code here for Delete a phone number"; 34 | } elsif ($choice == 4) { 35 | #Enter your code here for Print all phone numbers"; 36 | } elsif ($choice == 5) { 37 | #Enter your code here for Check if a person has a phone number"; 38 | } 39 | 40 | } 41 | 42 | #This part saves the data to the file 43 | open (DATA, ">phone.txt"); 44 | $output=join (":", %phone); 45 | print DATA $output; 46 | close DATA; 47 | 48 | --------------------------------------------------------------------------------