Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Programs

Status
Not open for further replies.

krishsays

IS-IT--Management
Dec 3, 2004
45
GB
Hi Guys,

Please can sombody help me in writing these two programs using perl ...

1. Write a complete Perl program which prompts the user to enter from the keyboard a sentence (consisting of at least three words, only the first word capitalized, sentence terminated with a period) and then echoes the sentence to the monitor but with the first letter of every word capitalized.

2. Write a complete Perl program that reads a text file (named "infile") containing one hundred Social Security numbers (9-digit positive integers), one per line; then writes an output file (named "outfile") containing the same data, but with the lines and the numbers in reverse order.


Any help will be highly appreciated ..

thanks,
krishan


 
Hi Krishsays

What do you have so far?



dmazzini
GSM System and Telecomm Consultant

 
sounds like school work, which is not supposed to be posted on this forum.
 
Hi ,

Its not school work...actually we are migrating some data in old repository all the names are in small caps ..and we want to put it in proper format ..with first letter in caps ...i dont know much about perl ...as I am a unix admin.. i was trying the below code ..but its giving some errors...

!/usr/local/bin/perl

print "Enter your Input : "; # Ask for input
$a = <STDIN>; # Get input
chop $a; # Remove the newline at end
if (length($a) >= 3) # While input is wrong...
{
awk '{sub(/./,toupper(substr($a,1,1)),$a)}1'
}else {
print "sorry. Minimum 3 characters should be provided"

Please help me in this...
krishan
 
Assuming you are converting existing data, I don't see why it needs to interact with the user. Surely you aren't going to type them all in? [smile]
Code:
use strict;
use warnings;

while (<DATA>) {
    chomp;
    my @sentence = split(/\s+/, $_);
    
    if (@sentence < 3) {
        print "Sentence must have three or more words: $_\n";
        next;
    }
    
    map {$_ = ucfirst($_)} @sentence;
    print join(" ", @sentence), "\n";
}
__DATA__
any sentence of three or more words
too short
one two three
Please note:[ol][li]It reduces multiple spaces between words to one.[/li][li]If you have awkward names like O'Reilly, McDermott, or de'Ath to deal with, you will need a more sophisticated upper-casing algorithm.[/li][li]Use chomp rather than chop; chomp will only remove newlines, while chop does exactly what it says...[/li][/ol]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top