Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hi Perlmonks

I am a beginner in perl programming. I have written the following perl script  count.pl to count the difference between the total number of letters between two sentences, in a set of sentences. I have used a do-until loop 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.

#!/usr/bin/perl # To calculate the difference of total letters between pairs of senten +ces use strict; use warnings; # An array, initialized to the empty list, to store the new sentence i +n 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 2 +8 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 comparis +ons @all_sentences=(my $sentence); # Line 34 # 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=$dif +f_letters\n"; print $fh "\n Difference of letters between sentence2 & sentence3=$dif +f_letters\n"; print $fh "\n Difference of letters between sentence1 & sentence3=$dif +f_letters\n\n"; close $output; # Line 47 # exit the program exit;

Input file s1.txt:

I have a dog.

Input file s2.txt:

 Most kids like computer games.

Input file s3.txt:

But I like to play with my dog.

Output in cmd screen appears as follows as I could not write the code at line 38 & 34:

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>

In reply to How can I store a scalar variable, coming out of a do-until loop, as an element in an array for further operations? by supriyoch_2008

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (13)
    As of 2014-07-30 17:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (237 votes), past polls