On Sat, Jul 12, 2003 at 02:43:18PM -0500, Mike Flannigan wrote:
> 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');
> }

This should be:

  open (HANDLE, $listelem)  or die "open: $listelem: $!";

That way if the open fails for any reason you'll get a sensible
error message.  (And it avoids the race condition.)

> #Grab main file contents
> chomp (my @filecontents = <HANDLE>);
> 
> while (<HANDLE>) {
>     my $filecontents =~ s/\|/h/;
> }

And here you need to loop over the array and do the substitution
on each element:

  s/\|/h/g foreach @filecontents;

If it makes things clearer you can do the iteration and substitution
with an explicit variable (instead of the implicit $_).

  for my $line (@filecontents) {
      $line =~ s/\|/h/g;
  }

-- 
Steve

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

Reply via email to