Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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 ( #990943=note: print w/ replies, xml ) Need Help??


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?

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.


Comment on Re: How can I store a scalar variable, coming out of a do-until loop, as an element in an array for further operations?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-12-26 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls