On Monday, July 22, 2002, at 08:11 , Bob H wrote:

> I have a script that is *supposed* to search a file and make certain 
> words that I have in an array uppercase. My brain is not grokking it.
>
> Q1: What is wrong with my script (below)?

what sorts of error messages did you get?

> === SCRIPT ===
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> # variables
> my $word = undef;
>
> # setup the words to uppercase
> my @words = qw/ Mary John Joe /;
>
> open FILE, ">> NEW1 "  or die "can't open FILE:  $!";

so far so good...

> while (<>) {

why this outer loop? what if nothing is passed
at the command line - hence

        <> evaluates to nothing

and the script closes.

>     foreach $word (@words) {
>         $word = ~ tr /a-z/A-Z/;
>         print FILE;

It might help to sort out what you really
wanted to do here....

I am a fan of localizing scope so I

so I would have gone with say:

     foreach my $word (@words) {
         $word =~ tr /a-z/A-Z/;
         print " $word \n";

        }

since I really only want to use 'my $word' here
for each of the tokens in the @words list.

then there is the bit about

        "=~" vice "= ~"

the former is an operator that binds the variable on the
left of the "=" to the 'pattern play' on the right.

then, rather than hope that you did something with $_
it some times helps to just expressly Print the thing you
want - in my case - I just sent it to stdout, in your case
you want to write to the file FILE...

since you would have wanted a loop like:

     foreach (@words) {
         tr/a-z/A-Z/;
                print ;
        }

to use the implicit play with $_ - but I think your
original idea of naming it was the better idea, you
just needed to get that $word to the print command....

[..]

> Q2: Can I update a file in place?

yes, cf

        perldoc IPC::Open2

> Q3: How do I run multiple things against one file in one script?

make sure they are all done between the open() and the close....


ciao
drieux

---


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to