#!/usr/bin/perl # Tom Blum # May 2007 # IWA-HWG Homework/ Instructor M. Kowalski $file_name = "WebX.txt"; open(MUSIC, "<$file_name") or die "Problem opening the file $file_name: $!"; $char_total=0; while() { chomp; #eliminates new-line character s/^\s+|\s+$//g; #eliminates white space from either end of line # (p. 122 Learning Perl, Schwartz et al.) if($_ eq ""){next;} #skips blank lines #parse into tokens @tokens = split(":", $_); #strip off leading and trailing whitespace from tokens foreach $element (@tokens) { $element =~ s/^\s+|\s+$//g; $char_total += length($element); } #add tokens to corresponding arrays push(@music_name, $tokens[0]); push(@music_address_1, $tokens[1]); push(@music_address_2, $tokens[2]); push(@music_city, $tokens[3]); push(@music_state, $tokens[4]); push(@music_zip, $tokens[5]); push(@music_email, $tokens[6]); push(@music_quote, $tokens[7]); } close MUSIC; print "\nThe total number of characters is $char_total\n"; print "(this does include spaces between words but\n"; print "does not include leading or trailing white space\n"; print "in the tokens (data), colon delimiters, new line \n"; print "characters, etc.)\n"; #validate state format my $i=0; foreach $item (@music_state) { $item = uc $item; if(!($item =~ m/^[A-Z]{2}$/)) { print "Musician $i, $music_name[$i], has a bad state ($item).\n"; } $i++; } #validate zip format $i=0; foreach $item (@music_zip) { if(!($item =~ m/\d{5}$/)) { print "Musician $i, $music_name[$i], has a bad zip code ($item).\n"; } $i++; } #validate email format $i=0; foreach $item (@music_email) { @tokens = split "@", $item; $user = $tokens[0]; $domain = $tokens[1]; @domain_parts = split /\./, $domain; if(!($user =~ m/^\w+$/)) { print "Musician $i, $music_name[$i], has a bad email address ($item).\n"; } else { foreach $part (@domain_parts) { if(!($part =~ m/^\w+$/)) { print "Musician $i, $music_name[$i], has a bad email address ($item).\n"; last; } if($part =~ m/^\s/) { print "Musician $i, $music_name[$i], has a bad email address ($item).\n"; last; } } } $i++; } print "\nWould you like to add a new record (yes or no)? "; chomp($response =); #accept any response beginning with a y (small or capital) while($response =~ /^y/i) { &add_record; print "\nWould you like to add another new record (yes or no)? "; chomp($response =); } print "\nWould you like to make a mailing label file (yes or no)? "; chomp($response =); #accept any response beginning with a y (small or capital) if($response =~ /^y/i) { &make_labels; } ############################################################################## #this subroutine writes a 3-column text file with names and address #as if one were going to print out mailing labels # sub make_labels { $file_name="music_mailing_labels.txt"; open(LABELS, ">$file_name") or die "Problem making the file $file_name: $!"; select LABELS; $|=1; #flushing my $j=0; printf "\n"; for($j=0;$j<=$#music_name;$j=$j+3) { printf "%-30s%-30s%-30s\n", $music_name[$j], $music_name[$j+1], $music_name[$j+2]; printf "%-30s%-30s%-30s\n", $music_address_1[$j], $music_address_1[$j+1], $music_address_1[$j+2]; printf "%-30s%-30s%-30s\n", $music_address_2[$j], $music_address_2[$j+1], $music_address_2[$j+2]; for($k=0;$k<3;$k++) { if($j+$k > $#music_name){last}; $last_line = $music_city[$j+$k] . ", " . $music_state[$j+$k] . " " . $music_zip[$j+$k]; printf "%-30s" , $last_line; } printf "\n\n\n\n"; } close LABELS; } ############################################################################## # this subroutine will allow the user to add another item to the file # sub add_record { print "Enter the name: "; chomp($new_name=); $new_name =~ s/^\s+|\s+$//g; print "Enter the street address: "; chomp($new_address_1=); $new_address_1 =~ s/^\s+|\s+$//g; print "Enter the second line of the street address\n"; print "(just return if there is no second line): "; chomp($new_address_2=); $new_address_2 =~ s/^\s+|\s+$//g; print "Enter the city: "; chomp($new_city=); $new_city =~ s/^\s+|\s+$//g; print "Enter the state code (e.g. CA for California): "; chomp($new_state=); $new_state =~ s/^\s+|\s+$//g; #vaildate state format $new_state = uc $new_state; if(!($new_state =~ m/^[A-Z]{2}$/)) { print "$new_state does not have the proper format for a state.\n"; print "\nWould you like to re-enter it (yes or no)? "; chomp($response =); if($response =~ /^y/i) { print "Enter the state code (e.g. CA for California): "; chomp($new_state=); $new_state =~ s/^\s+|\s+$//g; $new_state = uc $new_state; } } print "Enter the zip code: "; chomp($new_zip=); $new_zip =~ s/^\s+|\s+$//g; print "Enter the email address: "; chomp($new_email=); $new_email =~ s/^\s+|\s+$//g; print "Enter a quote: "; chomp($new_quote=); $new_quote =~ s/^\s+|\s+$//g; #add tokens to corresponding arrays push(@music_name, $new_name); push(@music_address_1, $new_address_1); push(@music_address_2, $new_address_2); push(@music_city, $new_city); push(@music_state, $new_state); push(@music_zip, $new_zip); push(@music_email, $new_email); push(@music_quote, $new_quote); $line = join ":", $new_name, $new_address_1, $new_address_2, $new_city, $new_state, $new_zip, $new_email, $new_quote; $file_name = "WebX.txt"; open(MUSIC, ">>$file_name") or die "Problem opening the file $file_name: $!"; select MUSIC; print "\n$line"; close MUSIC; select STDOUT; }