Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //lab 1
- #!/usr/bin/perl
- #
- $file = $ARGV[0]; #takes in the command line argument
- open(FH, $file) || die "Error - Could not open $file"; #opens the file or dies
- @contents = <FH>;
- @words = format_input(@contents);
- %wordcount = create_hash(@words);
- @sorted_words = sort_words(%wordcount);
- $average_length = average_length(@sorted_words);
- output(@sorted_words, $average_length, $file);
- #format_input
- #
- #splits the input file into different lines, converts the words to lowercase
- #and returns a list of the words
- sub format_input{
- foreach $L(@contents){
- chomp($L); #perldoc -f chomp
- $L = lc($L); #converts all the word to lower case for comparison
- $L =~ s/[[:punct:]]//g; #removes punctuation from the words
- }
- $text = "@contents"; #changes the list of lines into one big string
- @words = split(/ /, $text); #splits the string up by each word
- return @words;
- }
- #create_hash
- #
- #associates the list of words with a list of counts that counts how many times
- #each word appears.
- #
- #
- sub create_hash{
- foreach $L(@words){ #for each word determines if it is unique or not
- if($wordcount{$L}==0){ #counts each word
- $wordcount{$L}++;
- }
- else{$wordcount{$L}++;}
- }
- $wordcount{""}=0; #changes the count of the blankspaces to zero
- return %wordcount; #quick fix for not counting blankspaces in the hash
- #could just use regex here...
- }
- sub sort_words{ #uses perl's built in sort function to sort the hash
- @sorted_words = sort { $wordcount{$b} <=> $wordcount{$a} } keys %wordcount;
- return @sorted_words; #sorts the hash by value (count of each word)
- }
- #average_length
- #
- #finds the average length of each word by taking the length of each
- #element of the list and then dividing by the total length of the list
- #
- sub average_length{
- foreach $L(@sorted_words){
- $count += length $L;
- }
- $average_length = ($count/(scalar @sorted_words));
- return $average_length;
- }
- #Nicely formatted output for the assignment
- #
- #
- sub output{
- print "An analysis of $file:\n------------------------------------------\n";
- print "There are ".scalar(@sorted_words)." unique words in the file.\n\n";
- print "The average length of each word is $average_length\n\n";
- print "The 5 most common words are:\n";
- for($i=0;$i<5;$i++){
- print "\"@sorted_words[$i]\" occured $wordcount{@sorted_words[$i]} times.\n";
- }
- print "-------------------------------------------\n";
- }
- close(FH);
- #!/usr/bin/perl
- #
- #game_guess.pl
- #
- #check_guess
- #
- #makes sure that the guess is a valid guess
- #i.e. that the guess is between 0..10
- sub check_guess{
- @bin = (0 .. 10); #bin of possible guesses
- $guessd = @_[0];
- $flag = ($guessd ~~ @bin);
- while($flag < 1){ #asks the user for another guess
- print "I don't have that many fingers...guess again:\n";
- $guessd = <>; #won't count guesses out of range in total guess
- $flag = ($guessd ~~ @bin);
- }
- $flag = 0;
- return $guessd;
- }
- $secret = int(rand(11)); #randomly picks the number of fingers
- $count = 0; #behind back
- do {print "How many fingers am I holding behind my back?\n";
- $guess = <>;
- $guess = check_guess($guess);
- if($guess > $secret){
- print "Your guess is too high\n";
- }
- elsif($guess < $secret){
- print "Your guess is too low\n";
- }
- $count++;
- if(($count % 5)==0){ #after every 5 guesses, randomly picks a new integer
- $secret = int(rand(11));
- }
- } while($guess != $secret);
- #congratulatory message after you are right
- print "Correct! I was holding $secret fingers behind my back, it took you $count tries to guess though!\n";
- #!/usr/bin/perl
- #Takes in your name as a command line argument. Prints out your name
- #and counts the number of characters in your name
- #
- #Reverses all the characters in your name
- #Prints your initials
- #And prints your name backwards
- #Takes in the command line argument as your name
- $name = join(" ", @ARGV); #takes the list of command line arguments as string
- $namecount = join("", @ARGV);
- @initials = @ARGV;
- for(@ARGV){ #reverses each part of your name
- $_ = reverse $_;
- }
- $reversed = join(" ", @ARGV); #joins together reversed name
- for(@initials){ #generates your initials from the string
- $_ = uc(substr($_,0,1));
- }
- $initials = join(".", @initials);
- $backwards = reverse($name); #generates your name backwards
- #output commands...
- #subtracts 1 for the \n character. alternatively, could use the perl
- #command chomp() (perldoc -f chomp) to remove this character...
- print "Hello $name\nYou have ".(length($namecount))." characters in your name\n";
- print "Reversed: $reversed\n";
- print "Your initials are $initials.\n";
- print "Your name backwards is $backwards\n";
- #!/usr/bin/perl
- #
- #calculates how many days it will be until your next birthday
- #
- $bmonth = 11; #default birthday month, November
- $bday = 27; #default birthday day
- @current_time = localtime(); # ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
- @daysinmonth = (31,28,31,30,31,30,31,31,30,31,30,31); #defines the number of days months
- $total_bday = num_birthday($bmonth, $bday, $daysinmonth); #enumerates birthday
- $total_day = num_today(@current_time, $daysinmonth); #enumerates current day
- $days_until_birthday = has_birthday_passed($total_bday, $total_day, @current_time); #has your birthday already passed this year?
- $days_until_birthday = leap_year($days_until_birthday, @current_time, $bmonth); #is there a leap day in between today and your birthday?
- output($bmonth, $bday, $days_until_birthday, @current_time); #prints information
- #num_birthday
- #
- #counts the number of days from 1/1 to your birthday
- sub num_birthday{
- for($i = 0; $i< ($bmonth-1); $i++){
- $total_bday += $daysinmonth[$i];
- }
- $total_bday += $bday;
- return $total_bday;
- }
- #num_today
- #
- #counts the number of days since 1/1
- sub num_today{
- for($i = 0; $i< ($current_time[4])-1; $i++){
- $total_day +=$daysinmonth[$i];
- }
- $total_day +=$current_time[3];
- return $total_day;
- }
- #has_birthday_passed
- #
- #checks if your birthday has already occurred this year
- #if it has, we use 365-(difference), otherwise we can just
- #use the different in days
- sub has_birthday_passed{
- if($total_bday < $total_day){
- $days_until_birthday = 365 - abs($total_bday - $total_day);
- }
- else{
- $days_until_birthday = abs($total_bday - $total_day);
- }
- return $days_until_birthday;
- }
- #leap_year
- #
- #every leap year is defined as a year which is divisible by 4 but not 100
- #if your birthday hasn't occurred yet this year, checks if the current year is a
- #leap year.
- #
- #if your birthday has already occurred this year, checks if the next year is a leap
- #year
- sub leap_year{
- if($bmonth > $current_time[4]){
- if((@current_time[5] % 4 == 0) && (@current_time[5] % 100 != 0)){
- $days_until_birthday++;
- }
- }
- elsif(((@current_time[5]+1) % 4 == 0) && ((@current_time[5]+1) % 100 != 0)){
- $days_until_birthday++;
- }
- return $days_until_birthday;
- }
- #output
- #
- #formats the output nicely. if today is your birthday, it prints happy birthday
- #otherwise it will print the number of days until your birthday
- sub output{
- if(($bmonth-1 == @current_time[4]) && ($bday == @current_time[3])){
- print "Happy Birthday!\n";
- }
- else{
- print "If your birthday is on $bmonth/$bday, it is $days_until_birthday days until your birthday\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment