> -----Original Message-----
> From: Patrick Bateman [mailto:[EMAIL PROTECTED]]
> Subject: File substitutions??
> 
> Hi everyone

 Hi!  I'm still pretty new myself, so forgive me for not doing this the
"Perl way".
> 
> Need help with this please, pretty please!!
> 
> I open a file containing this:
> e.g.
> John:1111111:0
> Peter:2222222:0
> Jane:3333333:0
> 
> Now I select a particular name from an input.
> Now comes the part I'm having trouble with.  For that name 
> I've selected how
> do I increment the count value.
> e.g.
> If I select Peter then file is modified to this:
> John:1111111:0
> Peter:2222222:1
> Jane:3333333:0
> 
> and so on....
> Heres the sub routine that needs to be fixed....

 <snipped comments for readability>

> 
> sub FIND {
>   open (FIND, "prac1.txt") or die "$! file error";
>   while (<FIND>) {
>     ($name, $phone, $count) = split(/:/, $_);
>     if ($name =~ $compare[1]) {

>       print "$phone\n";
>       close FIND;
>       open (APPEND, ">>prac1.txt") or die "$! file error";
>       $count++;
>       substitute APPEND $count;
>       close APPEND;
>     }
>   }
> }
> 
> Thanks heaps
> Patrick Bateman

--------------------
use warnings;
use strict;

print "What name are you looking for? ";
my $find_name = <STDIN>;
chomp ($find_name);
FIND ($find_name);

sub FIND {
  my $f_name = $_[0];
  open (FIND, "prac1.txt") or die "$! file error";
  open (TMPFIND, ">prac1.txt.tmp") or die "$! file error";
  while (<FIND>) {
    chomp;
    my ($name, $phone, $count) = split (/:/);
    if ($name =~ /\Q$f_name\E/i) {
      $count++;
      print "\nPhone number for \u$name is $phone\n";
    }
    print TMPFIND join (':', $name, $phone, $count), "\n";
  }
  close FIND || die "Can't close FIND";
  close TMPFIND || die "Can't close TMPFIND";
  rename ("prac1.txt", "prac1.txt.old");
  rename ("prac1.txt.tmp", "prac1.txt");
}

This uses the old "output to a new file and rename at the end" procedure,
but I'm still too new to know how to do it any other way...

Hope this helps...
Jason


CONFIDENTIALITY NOTICE:

************************************************************************

The information contained in this ELECTRONIC MAIL transmission
is confidential.  It may also be privileged work product or proprietary
information. This information is intended for the exclusive use of the
addressee(s).  If you are not the intended recipient, you are hereby
notified that any use, disclosure, dissemination, distribution [other
than to the addressee(s)], copying or taking of any action because
of this information is strictly prohibited.

************************************************************************

Reply via email to