perlquestion
supriyoch_2008
<p>Hi Perlmonks</p>
<p>I am a beginner in perl programming. I have written the following perl script <code> count.pl</code> to
count the difference between the total number of letters between two sentences, in a set of sentences.
I have used a <code>do-until loop</code> to insert a new sentence each time and finally to quit the loop by typing "quit".
Then my interest is to store each new sentence as a scalar variable in an array so that I can do some further
operations between the elements of the array (line number 34 marked).I donot know the code to be used at line
number 34 and 38. Can any one help me? I have given below three input files namely s1.txt, s2.txt and s3.txt, each with a sentence
and the results of cmd screen.</p>
<code>#!/usr/bin/perl
# To calculate the difference of total letters between pairs of sentences
use strict;
use warnings;
# An array, initialized to the empty list, to store the new sentence in
my @all_sentences=(); # Line 6
# Using a do-until loop to insert a new sentence
my $input;
do {
# Input a sentence
print "\n\n Please type the filename(.txt): "; # Line 11
my $filename= <STDIN>;
chomp $filename;
# open the file or exit
unless ( open(FILE, $filename) ) {
print "Cannot open file \"$filename\"\n\n"; # Line 16
exit;
}
my $sentence=<FILE>;
close FILE;
# To remove white space & fullstop # Line 21
$sentence=~ s/\s//g;
$sentence=~ 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";# Line 25 28
print"\n Type \"quit\" to quit or press ENTER to continue: ";
$input=<STDIN>;
}
until ($input=~ /^\s*q/i); # Line Line 32
# To store all sentences as elements in the array for further comparisons
@all_sentences=(my $sentence); # Line 34
# To find the difference of letters between pairs of sentences i.e.
# sentence1 & sentence2, sentence2 & sentence3,sentence1 & sentence3 etc.
# 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=$diff_letters\n";
print $fh "\n Difference of letters between sentence2 & sentence3=$diff_letters\n";
print $fh "\n Difference of letters between sentence1 & sentence3=$diff_letters\n\n";
close $output; # Line 47
# exit the program
exit;</code>
<p>Input file s1.txt:</p> <code>I have a dog.</code>
<p>Input file s2.txt:</p> <code> Most kids like computer games.</code>
<p>Input file s3.txt:</p> <code>But I like to play with my dog.</code>
<p>Output in cmd screen appears as follows as I could not write the code at line 38 & 34:</p>
<code>Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\x>cd desktop
C:\Users\x\Desktop>new2.pl
Please type the filename(.txt): s1.txt
Sentence: Ihaveadog
Total letters in the sentence= 9
Type "quit" to quit or press ENTER to continue:
Please type the filename(.txt): s2.txt
Sentence: Mostkidslikecomputergames
Total letters in the sentence= 25
Type "quit" to quit or press ENTER to continue:
Please type the filename(.txt): s3.txt
Sentence: ButIliketoplaywithmydog
Total letters in the sentence= 23
Type "quit" to quit or press ENTER to continue: quit
Difference of letters between sentence1 & sentence2=my code?????
Difference of letters between sentence2 & sentence3=my code?????
Difference of letters between sentence1 & sentence3=my code?????
C:\Users\x\Desktop></code>