############################################################################### ##### Solution 01 ############################################################# ############################################################################### #! /usr/bin/perl -w print "Type data filename: "; $infilename = ; print "Type result filename: "; $outfilename = ; chomp($infilename); chomp($outfilename); #$filename = "sequence.txt"; open (SEQUENCE, '<', $infilename) or die "Can't open the file: $infilename\n"; open (RESULT, '>', $outfilename) or die "Can't open the file: $outfilename\n"; while (){ $line = $_; chomp($line); $length = length($line); $firstchar = substr($line,0,1); for ($i = 0; $i < ($length-1); $i++){ $char = substr($line, $i, 2); $charCount{$char}++; $totLength ++; } if(defined($lastchar)){ $char = $lastchar.$firstchar; $charCount{$char}++; $totLength ++; } $lastchar = substr($line,$i,1); } close(SEQUENCE); foreach $char (sort keys %charCount){ $frac = ($charCount{$char}/$totLength) *100; print "The two-character word $char occurs $charCount{$char} times, or $frac %.\n"; print RESULT "The two-character word $char occurs $charCount{$char} times, or $frac %.\n"; } print "The total number of characters is $totLength.\n"; print RESULT "The total number of characters is $totLength.\n"; close(RESULT); ############################################################################### ##### Solution 02 ############################################################# ############################################################################### #!/usr/bin/perl print "Type data filename: "; chomp($filename = ); print "Type result filename: "; chomp($filename2 = ); open (SEQUENCE, '<', $filename) or die "Can't open the file: $filename\n"; while (){ $line = substr ($line, -1, 1) . $_; chomp($line); $length = length($line); for ($i = 0; $i < $length - 1; $i++){ $pair = substr($line, $i, 2); $pairCount{$pair}++; $total++; } } close(SEQUENCE); open (RESULT, '>', $filename2) or die "Can't open the file: $filename2\n"; select RESULT; foreach $pair (sort keys %pairCount){ $percent = $pairCount{$pair}/$total*100; print "The two-character word $pair occurs $pairCount{$pair} times, or $percent %.\n"; } print "The total number of characters is $total.\n"; select STDOUT; print "Results sent to $filename2\n"; close RESULT; ############################################################################### ##### Solution 03 ############################################################# ############################################################################### #!/usr/bin/perl print "Type data filename: "; chomp($filename = ); open (SEQUENCE, '<', $filename) or die "Can't open the file: $filename\n"; print "Type result filename: "; chomp($filename2 = ); open (OUTPUT, '>', $filename2) or die "Can't open the file: $filename2\n"; $count = 0; $prev = ''; while (){ $line = $prev . $_; chomp($line); $length = length($line); for ($i = 0; $i < $length - 1; $i++){ $char = substr($line, $i, 2); $charCount{$char}++; $count++; $prev = substr($line, $i + 1, 1); } } close(SEQUENCE); foreach $char (sort keys %charCount){ $per = $charCount{$char} / $count * 100; print OUTPUT "The two-character word $char occurs $charCount{$char} times, or $per%.\n"; } print OUTPUT "The total number of characters is $count.\n"; close(OUTPUT); ############################################################################### ##### Solution 04 ############################################################# ############################################################################### #!/lusr/bin/perl -w print "Type data filename: "; $filename = ; chomp $filename; open (SEQUENCE, '<', $filename) or die "Can't open the file: $filename\n"; $total = 0; while (){ $line = $_; chomp($line); $length = length($line); for ($i = 0; $i < $length; $i++){ $last = $char; $char = substr($line, $i, 1); if ($total) { $charCount{($last.$char)}++; } $total++; } } close(SEQUENCE); print "Type result filename: "; $filename = ; chomp $filename; open (RESULT, '>', $filename) or die "Can't open the file: $filename\n"; foreach $char (sort keys %charCount){ $ratio = 100 * $charCount{$char} / $total; print RESULT "The two-character word $char occurs $charCount{$char} times, or $ratio %.\n"; } print RESULT "The total number of characters is $total - 1.\n"; print RESULT "End of file.\n"; close(RESULT); ############################################################################### ##### Solution 05 ############################################################# ############################################################################### #!/usr/bin/perl print "Type data filename: "; chomp($filename = ); open (SEQUENCE, '<', $filename) or die "Can't open the file: $filename\n"; print "Type result filename: "; chomp($outfilename = ); open(OUTFILE, ">" , $outfilename) or die "Can't create the file: $outfilename\n"; $longString = ""; while(){ chomp($_); $longString .= $_; } $fulllength = length($longString); $fulllength--; for($i=0; $i < $fulllength; $i++){ $char = substr($longString,$i,2); $charCount{$char}++; } close(SEQUENCE); foreach $char(sort keys %charCount){ print OUTFILE 'The character ',$char," occurs $charCount{$char} times, or ",$charCount{$char}/$fulllength*100, " %.\n"; } print OUTFILE "The total number of characters is ", $fulllength ,".\n"; close(OUTFILE); ############################################################################### ##### Solution 06 ############################################################# ############################################################################### #!/usr/bin/perl # Get and open the input file. print "Type data filename: "; $filename = ; chomp($filename); ###if ($filename == "") { $filename = "sequence.txt"; } if ($filename eq "") { $filename = "sequence.txt"; } open (SEQUENCE, '<', $filename) or die "Can't open the file: $filename\n"; # Get and open the output file print "Type result filename: "; $outfile = ; chomp($outfile); if ($outfile == "") { $outfile = "result2.txt"; open (RESULT, '>', $outfile) or die "Can't open result file: $outfile\n"; $out = RESULT; } elsif ($outfile == "-") { $outfile = "STDOUT"; $out = STDOUT; } else { open (RESULT, '>', $outfile) or die "Can't open result file: $outfile\n"; $out = RESULT; } # Processing time! $totalLength = 0; $prevChar = ''; while (){ $line = $_; chomp($line); $totalLength += length($line); $line = $prevChar . $line; $length = length($line); for ($i = 0; $i < $length - 1; $i++){ $word = substr($line, $i, 2); $wordCount{$word}++; } # grab the last character, so we can pop it onto the beginning # of the following string... $prevChar = substr($line, $i, 1); } close(SEQUENCE); # Print output. foreach $word (sort keys %wordCount){ print $out "The two-character word $word occurs $wordCount{$word} times, or "; print $out (($wordCount{$word} / $totalLength) * 100); print $out " \%.\n"; } print $out "The total number of characters is $totalLength - 1.\n"; print $out "End of file.\n" ############################################################################### ##### Solution 07 ############################################################# ############################################################################### #!/usr/bin/perl print "Type data filename: "; chomp($filename = ); print "Type result filename: "; chomp($outFile = ); open (SEQUENCE, '<', $filename) or die "Can't open the file: $filename\n"; $totalCount = 0; $prevChar = undef; while (){ $line = $_; chomp($line); $length = length($line); for ($i = 0; $i < $length; $i++){ if (!defined $prevChar) { $prevChar = substr($line, $i, 1); next; } $currChar = substr($line, $i, 1); $sequence = $prevChar . $currChar; $sequenceCount{$sequence}++; $totalCount++; $prevChar = $currChar; } } close(SEQUENCE); @sortedKeys = sort (keys %sequenceCount); open RES, ">$outFile"; foreach $sequence (@sortedKeys){ $currCount = $sequenceCount{$sequence}; $currFraction = 100 * ($currCount / $totalCount); print "The two-character word $sequence occurs $currCount times, or $currFraction %\n"; print RES "The two-character word $sequence occurs $currCount times, or $currFraction %\n"; } print "The total number of characters is $totalCount.\n"; print RES "The total number of characters is $totalCount.\nEnd of file."; ############################################################################### ##### Solution 08 ############################################################# ############################################################################### #!/usr/bin/perl print "Type data filename: "; chomp ($inputFilename = ); print "Type result filename: "; chomp ($outputFilename = ); open (SEQUENCE, '<', $inputFilename) or die "Can't open the file: $inputFilename\n"; open (RESULT, '>', $outputFilename) or die "Can't open the file: $outputFilename\n"; while (defined($line=)) { chomp($line); $length = length($line); $size += $length; for ($i = 0; $i < $length; $i++) { $char = substr($line, $i, 1); if (defined($prevChar)) { $word = $prevChar . $char; $wordCount{$word}++; } $prevChar = $char; } } $size--; close(SEQUENCE); foreach $word (sort keys %wordCount) { $wordFraction{$word} = $wordCount{$word} / $size * 100; print RESULT "The two-character word $word occurs $wordCount{$word} times, or $wordFraction{$word}%.\n"; } print RESULT "The total number of characters is $size.\n"; close(RESULT); ############################################################################### ##### Solution 09 ############################################################# ############################################################################### #! /usr/bin/perl print "Type data filename: "; $filename = ; chomp($filename); print "Type results filename: "; $resultsFilename = ; chomp($resultsFilename); open (SEQUENCE, '<', $filename) or die "Can't open the file: $filename\n"; $total = 0; $lastChar; while (){ $line = $_; chomp($line); $length = length($line); for ($i = 0; $i < $length; $i++){ $char = substr($line, $i, 1); if( !($lastChar eq "") ){ $charCount{($lastChar.$char)}++; $lastChar = $char; $total++; } else { $lastChar = $char; } } } close(SEQUENCE); open FILE, ">$resultsFilename" or die "unable to open $resultsFilename $!"; foreach $char (sort (keys %charCount)){ $percentage = ($charCount{$char} / $total) * 100; print FILE "The character $char occurs $charCount{$char} times, or $percentage %.\ n"; } print FILE "The total number of characters is $total.\n"; print FILE "End of file."; ############################################################################### ##### Solution 10 ############################################################# ############################################################################### #! /lusr/bin/perl print "Type data filename: "; $filename = ; chomp $filename; open(SEQUENCE, "<", $filename) or die "Can't open the file: $filename\n"; print "Type result filename: "; $ofile = ; chomp $ofile; open(RESULT, ">", $ofile) or die "Can't create the file: $ofile\n"; $total = 0; $char=''; while() { $line = $_; chomp $line; $length = length $line; if($length != 0) { if($char) { $char = $char . substr($line,0,1); $charCount{$char}++; $total++; } for($i = 0; $i < $length-1; $i++) { $char = substr($line, $i, 2); $charCount{$char}++; $total++; } $char = substr($line,$length-1,1); } } $total++; close SEQUENCE; foreach $char (sort keys %charCount) { $percentage = $charCount{$char} / $total * 100; print "The two-character word $char occurs $charCount{$char} times, or $percentage %.\n"; print RESULT "The two-character word $char occurs $charCount{$char} times, or $percentage %.\n"; } print "The total number of characters is $total.\n"; print RESULT "The total number of characters is $total.\n"; close RESULT;