On Jul 25, 2004, at 12:55 PM, <[EMAIL PROTECTED]> wrote:

Another question, so far i have this script:

#!/usr/bin/perl

use strict; use warnings;

The above two lines ask Perl to hold you to the laws of good programming. In return, you'll get better error messages and warnings about things that might not be what you intended. That's a good deal anytime, but critical in early Perl learning.

Because it forces you to write cleaner code, it also helps us read your code, which again, can only help you.

The biggest change these rules will hold you too is declaring your variables before you use them. In Perl, that generally looks like this:

my $some_scalar;        # declare a scalar

my $some_scalar = 'initial value'; # declare a scalar and initialize it with a string

You only have to change one line below to get the code to pass "strict". Can you see which one?

use IO::File;

die "Usage:  script OLD_FILE, SEARCH, REPLACE, NEW_FILE\n"
                unless @ARGV == 4;
my($old_file, $search, $replace, $new_file) = @ARGV;

 $fh = new IO::File "> $new_file";
     if (defined $fh) {
        while(<>){

The above line isn't doing what you want. It walks through @ARGV, opening those files, and reading from them line-by-line.


We didn't empty @ARGV with our test above, so first it will open $old_file, which is what you want. When it finishes going through that, it will try to open your $search pattern, as a file. As you can see, problems start here and you get the warning messages you mentioned.

The fix is to read from only what we intend. Instead of opening just one file, open two, one for reading and one for writing. Then, assuming you open the old file in $old, you just need to modify your while loop to:

while (<$old>) {

        s/$search/$replace/g;
        print;

You don't want to print to the screen, you want to print to the new file. Hint: You need and $fh in the above line somewhere.


Hopefully these tips will get you going. If they don't cry help again and I'll give more specific answers. Good luck.

James

        }
        $fh->close;
    }


But the first problem is that if i try to run the code like this:

perl script.pl old.txt , \n new.txt

it will remove all the commas but won't replace them with new line
characters it will just remove them.

Also when i try to run the script it gives me this error:

Can't open ,: No such file or directory at script.pl line 11, <> line 1.
Can't open \n: No such file or directory at script.pl line 11, <> line 1.
Can't open new.txt: No such file or directory at script.pl line 11, <> line
1.



Why am i getting these errors, and how can i fix this?


Thanks in advance.


-----Original Message----- From: James Edward Gray II [mailto:[EMAIL PROTECTED] Sent: Saturday, July 24, 2004 3:13 PM To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Subject: Re: Multiple Parameters


On Jul 24, 2004, at 11:38 AM, <[EMAIL PROTECTED]> wrote:


I am trying to write a search and replace script that can accept
multiple
arguments, but i want the first argument to be the filename to read,
the
next one to be the string to search for, the next one to be the
replacement
string, and the last one to be the name of the new file it creates
with the
new changes, but i can't figure out how to seperate each argument, can
some
one tell me how to do something like this. Any help is very much
appreciated.

Well, command line arguments come into the program by way of the array @ARGV. So first we should be sure you got the right number of arguments:

die "Usage:  script OLD_FILE, SEARCH, REPLACE, NEW_FILE\n"
                unless @ARGV == 4;

Then we can use it:

my($old_file, $search, $replace, $new_file) = @ARGV;

Finally, just FYI, you can do what you describe with a one-liner:

perl -pi.bak -e 's/search/replace/g' old_file

Hope that helps.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to