Hi,
Please, format you codes properly.
If I understand your questions, you want to
- Store up each file read in an array, for later use
- Count the difference between the total number of letters between two sentences
One way of doing the first step is shown below, however, the second step you will figure it out! I know you can!:)
I commented out all the second part. Since, the first part gives you all your array elements, then you can find the difference between the total number of letters.
#!/usr/bin/perl
use strict;
use warnings;
my @all_sentences; ## initialized an array
my $sentence = "";
print "\n Type \"quit\" to quit or press ENTER to continue: ";
chomp( my $input = <STDIN> );
LOOP: while ( $input !~ m{^\bquit\b}i ) {
# Input a sentence
print "\n\n Please type the filename(.txt): ";
chomp( my $filename = <STDIN> );
next LOOP if $filename eq ""; # go back to LOOP if filename is em
+pty
# open the file or exit
open my $fh, '<', $filename
or die "Cannot open file $filename: $!";
my $sentence = do { local $/; <$fh> }; ## slurp a file
close $fh or die "can't close file: $!";
# To remove white space & fullstop
$sentence =~ s/\s|\.//g;
print "\n Sentence: $sentence\n";
# To count number of total letters
my $total_letters = 0;
$total_letters = length($sentence);
print "\n Total letters in the sentence= $total_letters\n";
# To store all sentences as elements in the array for further comp
+arisons
push @all_sentences, $sentence;
print "\n Type \"quit\" to quit or press ENTER to continue: ";
chomp( $input = <STDIN> );
}
print join "\n", @all_sentences; ## a test to show you got all the
+file content
# To find the difference of letters between pairs of sentences i.e.
# sentence1 & sentence2, sentence2 & sentence3,sentence1 & sentence3 e
+tc.
# Find the difference of letters between any two sentences # Line 37
#my $diff_letters =
# code
#? ??
# ? ?; # Line 38
#print"\n Difference of letters between sentence1 & sentence2=my $diff
+_letters\n
#Difference of letters between sentence2 & sentence3=my $diff_letters\
+n
#Difference of letters between sentence1 & sentence3=my $diff_letters\
+n\n\n";
# Print to a text file:
#my $output="Result .txt"; # Line 42
#open (my $fh,">",$output) or die"Can't open file '$output'.\n";# Line
+ 43
#print $fh "\n Difference of letters between sentence1 & sentence2=$di
+ff_letters\n";
#print $fh "\n Difference of letters between sentence2 & sentence3=$di
+ff_letters\n";
#print $fh "\n Difference of letters between sentence1 & sentence3=$di
+ff_letters\n\n";
#close $output; # Line 47
# exit the program
#exit;
I can only hope this helps.