Patrick Bateman wrote:

> Hi everyone
>
> 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....
>
> sub FIND {
> open (FIND, "prac1.txt") or die "$! file error"

>
> #open file
>
> while (<FIND>) {
>   ($name, $phone, $count) = split(/:/, $_);
> #separate values
>   if ($name =~ $compare[1])
> {                                                            #compare name
> value with input
>     print "$phone\n";
> #display phone number for that name
>     close FIND;
>     open (APPEND, ">>prac1.txt") or die "$! file error";
> #increment count value for that name,
>     $count++;
> #and substitute it with original count.
>     substitute APPEND $count;

    This will write Peter:......:1 to the end of the file not in it's original
location. You will end up with
     John:....:0
     Peter:......:0
     Jane:.....:0
     Peter:......:1

     You will have to open a temporary file, write the modified contents to it
and rename it back
     to the original file name. You will subroutine will end up like

    open (FIND, "prac1.txt") or die "Error Message";
    open (TMPFIND, ">prac1.txt.tmp") or die "Error Message";

    while (<FIND>) {
      chomp;
      my ($name, $phone, $count) = split (/:/);
      $count++ if ($name eq $compare[1]); #Assuming $compare[1] contains the
input name
      print TMPFIND join (':', $name, $phone, $count), "\n";
     }
     close (FIND);
     close (TMPFIND);
     rename ("prac1.txt", "prac1.txt.old");
     rename ("prac1.txt.tmp", "prac1.txt");

>
>     close APPEND;
>  }
>  }
> }
>
> Thanks heaps
> Patrick Bateman
>
> --
> 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