Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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 (Scribe)
on Aug 31, 2012 at 06:38 UTC ( #990928=perlquestion: print w/ replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

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>

Comment on How can I store a scalar variable, coming out of a do-until loop, as an element in an array for further operations?
Select or Download Code
Re: How can I store a scalar variable, coming out of a do-until loop, as an element in an array for further operations?
by prashantktyagi (Scribe) on Aug 31, 2012 at 06:53 UTC

    update this line to my $diff_letters="code?????";

Re: How can I store a scalar variable, coming out of a do-until loop, as an element in an array for further operations?
by aitap (Deacon) on Aug 31, 2012 at 08:10 UTC

    print "Cannot open file \"$filename\"\n\n"; # Line 16 exit;
    You can use die instead of print and exit. Also, die throws exceptions, so they can be catched and handled outside the running code. This can be useful for generating backtraces and other debugging things.

     @all_sentences=(my $sentence);                 # Line 34
    Did you mean push? push @all_sentences,$sentence;

     my $diff_letters="code?????";             # Line 38  
    You can use ... when you don't know what to write, Perl will automatically throw "Unimplemented" exception at this point. Anyway, did you mean for (1..$#all_sentences) {print abs(length $all_sentences[$_-1] - length $all_sentences[$_]), "\n";}? See abs, length, perlsyn for more information.

    Sorry if my advice was wrong.
Re: How can I store a scalar variable, coming out of a do-until loop, as an element in an array for further operations?
by 2teez (Priest) on Aug 31, 2012 at 08:25 UTC
    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.

Re: How can I store a scalar variable, coming out of a do-until loop, as an element in an array for further operations?
by philiprbrenan (Monk) on Aug 31, 2012 at 11:00 UTC

    I think it might help if you replace

    @all_sentences=(my $sentence);

    with

    push @all_sentences, $sentence;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://990928]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-08-27 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (238 votes), past polls