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);
DJSarma 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.