Link to home
Start Free TrialLog in
Avatar of kudak
kudak

asked on

delete a record

i want to delete a record in my text file, example:
a|b
d|e
g|h

let's say i want to delete d|e from the web, i have a code below but it seems that it will delete the entire record. anyone can correct this??

open(FILE, "file.txt");
my(@lines) = <FILE>;
close(FILE);
my($i) = 0;
foreach (@lines) {
$FILELINES{$_} = $i + 1;
$i++;
}
my($LTD) = $FORM{'name'} . "|" . $FORM{'address'} . "\n";
$LineToDelete = $FILELINES{$LTD};
$Numbs = @lines;
open(FILE, ">file.txt");
     for($i = 0; $i < $Numbs - 1; $i++) {
          print FILE @lines[$i] unless ($i = ($LineToDelete - 1));
     }
close(FILE);

Avatar of olthoff
olthoff

open(FILE, 'file.txt');
my (@lines) = <FILE>;
close(FILE);
$i = 1;

foreach (@lines) {
  $FILELINES{$_} = $i++;
};
my $LTD = $FORM{name} . '|' . $FORM{address} . "\n";
delete($FILELINES{$LTD});
open(FILE '>file.txt');
foreach (keys %FILELINES) {
  print(FILE $_);
};
close(FILE);
ASKER CERTIFIED SOLUTION
Avatar of guadalupe
guadalupe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kudak

ASKER

dear quadalupe,
i've tried yours but there's a small problem.

let's say i have a file contains:

a|b|c
d|e|f
g|h|i
k|l|a

if i try to run your program by key in
a,b,c ffrom the browser, i will delete the first and the last line.
it's supposed to delete only the first line. i do not know why it deletes the last one as well.
Avatar of ozo
print FILE unless /\Q$LTD/;
OR:

print FILE unless /^$LTD$/;  
But then $LTD has to be exactly the whole line...
Avatar of kudak

ASKER

i've tried what ozo suggested, but it could not even delete the record that i've keyed in.
Avatar of kudak

ASKER

i've also tried yours guadalupe,
i insert your new code,
 print FILE unless /^$LTD$/;  
does not delete at all.
the file remains the same.
Are you certain that $LTD contains the exact line in its entirety?  What about adding a new lione just to see if that is waht is being bothersome... like this before the matching:

$LTD .= "\n";

Also are you paying attention to caps and lower could you be mixing case between the value of $LTD and the actual line.  If you want to avoid such a problem edit the line again like this:

print FILE unless /^$LTD$/i;  


Avatar of kudak

ASKER

well guadalupe,
it's still has some errors. now the code is like you ask me to do. it will delete whenever the find the same record.
let's say when user key in a for the first input field, b for the second and so forth, and let's say i have this record in my xxx1.txt file,
a|b|c|d|e|f|g
1|2|3|4|5|6|7
waed|fs|ds|f|vf|ds|a

the first line and the last is deleted, perhaps because of the same record, a and f

open(FILE, "xxx1.txt");
my(@lines) = <FILE>;
close(FILE);
my($i) = 0;

$LTD .= "\n";
my($LTD) = $FORM{'masa'} . "|" . $FORM{'hari'} . "|" . $FORM{'bulan'} . "|" . $FORM{'sumber'} . "|" . $FORM{'nama'} . "|" . $FORM{'email'} . "|" . $FORM{'tujuan'} . "\n";

open(FILE, ">xxx1.txt");

foreach (@lines)
{
print FILE unless /^$LTD$/i;  
#print FILE unless /\Q$LTD/;
}


close(FILE);
Wait there is an error...

If you are explicitly adding the new line in this line:

my($LTD) = $FORM{'masa'} . "|" . $FORM{'hari'} . "|" . $FORM{'bulan'} . "|" . $FORM{'sumber'} . "|" . $FORM{'nama'} . "|" . $FORM{'email'} . "|" . $FORM{'tujuan'} . "\n";

Then you don't need my suggestion of $LTD .= "\n" ;

which by the way was meant to come after the the assigment of $LTD...




OPK I'm sorry Ididn't notice it before... Its you pipe.  You putting a sting of chars into $LTD serperated by a pipe but then you using that string as a reguklar expression in a pattern match.  In regex the pipe means or.  We're throwing the whole thing out of wack with all those ors.

Solution:

Change the field seperator to ":"  both in the xxx.txt file and in the scrpt line:

my($LTD) = $FORM{'masa'} . "|" . $FORM{'hari'} . "|" . $FORM{'bulan'} . "|" . $FORM{'sumber'} . "|" . $FORM{'nama'} . "|" . $FORM{'email'} . "|" . $FORM{'tujuan'} . "\n";

That should do it..