
davorg
Thaumaturge
/ Moderator
Jun 12, 2002, 12:02 AM
Post #2 of 2
(326 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; print; } 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 http://www.dave.org.uk/ Get more help at Perl Monks
|