In general, what you want to do is this;

1.  open the file for reading
2.  open a second file for writing
3.  read in each line, doing the substitution
    (although technically using the tr/// operator might be quicker)
4.  after each substitution, print the result to the second file
5.  once all lines are done, overwrite your original file with the temp
file.

>From the code you've shown I'm confident that you can figure out how to do
this on your own.

This might seem like a lot of work, but in reality it takes very little time
and is the way probably 99% of the programs out there do this kind of
operation.  There is possibly a way to use sysread, seek, etc. to try to
manipulate the file in place, but it doesn't work in all situations, and I
wouldn't start into that until you're much more comfortable with Perl.

-----Original Message-----
From: Mike Flannigan
To: [EMAIL PROTECTED]
Cc: Mike Flannigan at home
Sent: 7/12/03 12:43 PM
Subject: Search and Replace


OK, I'm a real newbie, but even I am ashamed to be asking this.
I want to search for a vertical mark ( | ) and replace it with
something else (h).  That's all - nothing fancy.  What am I doing
wrong here:

use strict;
use warnings;
my $listelem = "52101.txt";
#Open file in listelem if it exists
if (-e $listelem) {
    open (HANDLE, $listelem);
}
else {
    print "$listelem does not exist.\n";
    system('pause');
}

#Grab main file contents
chomp (my @filecontents = <HANDLE>);

while (<HANDLE>) {
    my $filecontents =~ s/\|/h/;
}

__END__


It's probably in the     my $filecontents =~ s/\|/h/;
statement.  I'm guessing the $filecontents is not the
correct thing to use.  Or maybe I have to open the
file with append (>>).  I assume I have to
escape the "|" in the search expression above.

I've tried various combinations of the g m and s
modifiers for the search, but none work.

I know I don't have to suck the entire file contents
into @filecontents, but I do want to do that for other
reasons.


The file.txt file contains:

1.13589
 30
0.0
 40
0.065
  1
FSC|M NO.
100
AcDthYext
  0
TET
  5
DPUYC5D
330
DDYED26
100

I get the digest version of this list, so if you need immediate
response, you might want to cc me directly
mailto:[EMAIL PROTECTED]



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

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

Reply via email to