CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
a real beginner's code---What's the problem?


New User

Jun 11, 2002, 11:28 AM

Post #1 of 2 (610 views)
a real beginner's code---What's the problem? Can't Post

what I mean is to get a input file and upper case all the p,e,r and l letters.But doesn't work.Unsure

#!/usr/bin/perl -w
$TheFile = "csc5750_hm3_input.txt";
open(INFILE, $TheFile) or die "The file $TheFile could " .
"not be found.\n";
while(<INFILE>) {
$TheLine = $_;
if(($TheLine eq 'p')||($TheLine eq 'e')||($TheLine eq 'r')||($TheLine eq 'l')){
print(uc $TheLine);

Thaumaturge / Moderator

Jun 12, 2002, 12:02 AM

Post #2 of 2 (604 views)
Re: [ryanweiran] a real beginner's code---What's the problem? [In reply to] Can't Post

Something like this perhaps:

#!/usr/bin/perl -w 
use strict;
my $TheFile = "csc5750_hm3_input.txt";
open(INFILE, $TheFile)
or die "The file $TheFile could not be found: $!\n";

while (<INFILE>) {
s/([lerp])/uc $1/eg;

Notice that I've added the $! variable to your error message. This contains the operating system's explaination of why it couldn't open the file.

To do the upper-casing I've used a regular expression and the substitution operator. [lerp] means "match any single letter p, e, r or l". ([lerp) means "match any single letter p, e, r or l and capture it in $1". We then replace that with the upper case version of the matched letter. The /e option means "treat the replacement string as Perl code, execute it and use whatever is retrurned as the replacement". The /g option means "do this for every matched letter in the string".

Both s/// and print act on $_ be default, so it's pointless to use extra variables like $TheLine.

Note: I re-ordered the letters to "lerp" as "square bracket", "perl", "square bracket" is a markup instruction in this nasty bulletin board :(

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives