Hi all;
I've to update a record for my hw.I'm trying to use seek function but when I use it, new information(phone number) is being written at the end of file...
Here is the function that I've tried...
Assume that the file's content like below and the input is :
1)Peter Parker
2)00000
peter parker 56372
eddie brock 23456
.....
.....
sub edit()
{
print "Enter the person's name and surname which you'll update the record\n";
chomp($dest = <STDIN>);
while($line = <FILE>)
{
@array1 = split(/ /,$dest);
@array = split(/ /,$line);
if($array1[0] eq $array[0] && $array1[1] eq $array[1]){
print "Enter new phone number\n";
chomp($newNum = <STDIN>);
seek(FILE,-1 * length($array[2]),1) || die("Couldn't seek back\n");
print(FILE "$newNum\n");
$a = 1;
}
}
if($a != 1){
print"Record doesn't exist!!!\n";
}
}
desired output:
peter parker 00000
eddie brock 23456
....
....
above code's output:
peter parker 56372
eddie brock 23456
....
....
00000