On Jul 11, Johnson, Shaunn said:

>* open each file
>* search and replace the old pattern to a new pattern
>(should only be one occurrence)
>* close file

You're not storing the results anywhere.  You can't expect Perl to modify
the file for you automagically.

>But nothing is happening (that I can see).
>What am I doing wrong?

You can use Perl's special in-place modification idiom:

>use diagnostics;

That's probably not needed in your program.  Why didn't you

  use strict;

though?

>for my $file(@list) {
>open  (FILE, $file ) or die "can nae open the file: $!";;
>while (<FILE>) {
>        s!$pattern!newpattern!g ;
>        } # end while loop
>} #end of for loop
>
>close (FILE);

You should place the close(FILE) INSIDE the for loop, not outside it.

Anyway, here's the nifty idiom:

  {
    local $^I = ".bak";   # to keep a backup of your files
                          # set to "" if you don't want any backups
    local @ARGV = @list;  # the files to work on

    while (<>) {
      s/$pattern/newpattern/g;
      print;
    }
  }

Poof!  Like magic, Perl takes care of everything for you.  Read up on
'perldoc perlvar' to find out more about $^I and magical in-place editing.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


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

Reply via email to