Start New Discussion Reply to this Discussion count words and print the sentences in an ascending order
Please help me!I want the code for doing following task in perl. Task is that "Read the paragraph from a file and print it in another file in ascending(total number of words in a sentence) order." My sample task:
If the input "Hi! my name is Sarma. Can you help?"
Output should be "Hi! Can you hepl? my name is Sarma."
I could try this. It prints the number of words in a each sentence. My code is:
#!/usr/local/bin/perl
use strict;
use warnings;
my ($line,$inFile,$outFile,$para, $text, @sentences, @words, @length, $count, $i, $order); #defined variables
$inFile = "name.txt";
$outFile = "out.txt";
open (SOURCE, $inFile) || die "cannot open \"$inFile\": $!";
open (RESULT, ">$outFile") || die "cannot open \"$outFile\": $!";
while ($line = <SOURCE>) {
chomp($line);
$text = $text.$line;
}
close (SOURCE);
@sentences = split(/[!,?,.]/, $text); #split text into sentences
for ($i=0;$i<scalar(@sentences);$i++)
{
@words=split(/\s+/, @sentences[$i]); #split each sentence into words
@length = scalar(@words); #count words of the sentence
my @array = sort @length;
print RESULT "@array\n";
}
close (RESULT);
Related Article: print words from a line
is a solved Perl discussion thread by Gaiety that has 6 replies and was last updated 1 year ago.
DJSarma
Newbie Poster
1 post since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Looks like your logic has a few problems.
1. You are iterating through the sentences but you are storing the words and lengths in arrays that will only contain a single element. (That is, you are overwriting the values in @words and @length on each pass because you aren't either indexing the array when you store them or adding them to the end of the existing arrays.)
2. Plus, you are sorting and printing this single item array each time you process a sentence when it seems you'd want to sort and print the results after processing all sentences.
3. It also seems that since you're only sorting and printing the length, you won't get the actual text printed.
Consider using a multi-dimensional array so you can easily associate the counts with the text.
craigjh
Newbie Poster
4 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Try this.
Using a hash and sorting by value
#!/opt/local/bin/perl5
my $in="in";
my $out="out";
my $line;
my $text;
my @sentences;
my %seen;
open(SOURCE,"<in") or die $!."\n";
while ($line = <SOURCE>) {
chomp($line);
$text = $text.$line;
}
close(SOURCE);
@sentences = split(/[!,?,.]/, $text);
foreach(@sentences){
my $dummy=$_;
$dummy=~s/^\s//g;##removing any beginning space
$seen{$dummy}=scalar(split(/\s+/,$dummy));
}
#print hash as key value pair
#while(($key,$value)=each(%seen)){
# print "$key => $value\n";
#}
#sort hash by value
open(FOUT,">out");
foreach $value (sort {$seen{$a} <=> $seen{$b} }
keys %seen)
{
print FOUT "$value\n";
}
close(FOUT);
cat out
Hi
Can you help
my name is Sarma
voidyman
Newbie Poster
22 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
© 2013 DaniWeb® LLC
Page rendered in 0.0634 seconds
using 2.67MB