Thank you very much for your help, but i have one more question, is this the
way that regex works or is it something in my code, every time i try to run
the script to search for a comma and replace it with a new line character
(\n) it just removes the commas but doesn't replace it with a new line
character.

-----Original Message-----
From: James Edward Gray II [mailto:[EMAIL PROTECTED]
Sent: Monday, July 26, 2004 9:22 AM
To: [EMAIL PROTECTED]
Cc: Perl Beginners
Subject: Re: Multiple Parameters


(Let's keep our discussion on the list so all can help and learn.)

On Jul 25, 2004, at 8:09 PM, <[EMAIL PROTECTED]> wrote:

> OK i still can't figure this out, i understand what you explained but i
> still can't figure out why it doesn't want to write to the new file
> and also
> why it only removes the commas and doesn't replace it with the new line
> character, i modified the code a little but now when i run it it gives
> me
> this weird message:
>
>
> --------weird message--------------------
> IO::File=GLOB(0x8132fe4)

You are printing an object.  See below.

> ---------new code--------------------------------------------
> #!/usr/bin/perl
>
> use IO::File;
> use strict;
> use warnings;
>
> die "Usage:  script OLD_FILE, SEARCH, REPLACE, NEW_FILE\n"
>                 unless @ARGV == 4;
>
> my($old_file, $search, $replace, $new_file) = @ARGV;
>
>
>
> my $open_file = new IO::File;
> my $fh = new IO::File "> $new_file";

You're not checking if this succeeds any more.  What if it fails?

>     if ($open_file->open("< $old_file")) {
>         while (<$open_file>){
>         s/$search/$replace/g;
>         print $fh;

Perl thinks you want to print $fh, not print TO $fh.  We need to
clarify.

print $fh $_;

# or...

$fh->print();

>         }
>         $fh->close;
>         $open_file->close;
>     }
> ---------------------------------------------------------------

Just out of curiosity, why are we using the module for this?  Don't get
me wrong, I love object oriented programming, but here the interface
seems to have bitten you a few times and you're not saving yourself any
work.  Here's the mundane version:

#!/usr/bin/perl

use strict;
use warnings;

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

open IN, '<', $old_file or die "File error:  $!";
open OUT, '>', $new_file or die "File error:  $!";

while (<IN>) {
        s/$search/$replace/g;
        print OUT $_;
}

close IN;
close OUT;

__END__

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>


Reply via email to